Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ecs): deployment circuit breaker support #12168

Merged
merged 8 commits into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/@aws-cdk/aws-ecs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,21 @@ const service = new ecs.FargateService(this, 'Service', {
`Services` by default will create a security group if not provided.
If you'd like to specify which security groups to use you can override the `securityGroups` property.

### Deployment circuit breaker and rollback

Amazon ECS [deployment circuit breaker](https://aws.amazon.com/tw/blogs/containers/announcing-amazon-ecs-deployment-circuit-breaker/)
automatically rolls back unhealthy service deployments without the need for manual intervention. Use `circuitBreaker` to enable
deployment circuit breaker and optionally enable `rollback` for automatic rollback. See [Using the deployment circuit breaker](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/deployment-type-ecs.html)
for more details.

```ts
const service = new ecs.FargateService(stack, 'Service', {
cluster,
taskDefinition,
circuitBreaker: { rollback: true },
});
```

### Include an application/network load balancer

`Services` are load balancing targets and can be added to a target group, which will be attached to an application/network load balancers:
Expand Down
28 changes: 28 additions & 0 deletions packages/@aws-cdk/aws-ecs/lib/base/base-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ export interface DeploymentController {
readonly type?: DeploymentControllerType;
}

/**
* The deployment circuit breaker to use for the service
*/
export interface DeploymentCircuitBreaker {
/**
* Whether to enable rollback on deployment failure
* @default false
*/
readonly rollback?: boolean;

}

export interface EcsTarget {
/**
* The name of the container.
Expand Down Expand Up @@ -161,6 +173,12 @@ export interface BaseServiceOptions {
* @default - Rolling update (ECS)
*/
readonly deploymentController?: DeploymentController;

/**
* Whether to enable the deployment circuit breaker
* @default - disabled
*/
readonly circuitBreaker?: DeploymentCircuitBreaker;
SoManyHs marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -356,6 +374,16 @@ export abstract class BaseService extends Resource
...additionalProps,
});

if (props.circuitBreaker) {
const deploymentConfiguration = {
DeploymentCircuitBreaker: {
Enable: true,
Rollback: props.circuitBreaker.rollback ?? false,
},
};
// TODO: fix this when this property is available in CfnService
this.resource.addPropertyOverride('DeploymentConfiguration', deploymentConfiguration);
};
if (props.deploymentController?.type === DeploymentControllerType.EXTERNAL) {
Annotations.of(this).addWarning('taskDefinition and launchType are blanked out when using external deployment controller.');
}
Expand Down
37 changes: 37 additions & 0 deletions packages/@aws-cdk/aws-ecs/test/fargate/test.fargate-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ export = {
deploymentController: {
type: ecs.DeploymentControllerType.CODE_DEPLOY,
},
circuitBreaker: { rollback: true },
securityGroup: new ec2.SecurityGroup(stack, 'SecurityGroup1', {
allowAllOutbound: true,
description: 'Example',
Expand All @@ -235,6 +236,10 @@ export = {
DeploymentConfiguration: {
MaximumPercent: 150,
MinimumHealthyPercent: 55,
DeploymentCircuitBreaker: {
Enable: true,
Rollback: true,
},
},
DeploymentController: {
Type: ecs.DeploymentControllerType.CODE_DEPLOY,
Expand Down Expand Up @@ -1786,6 +1791,38 @@ export = {
test.done();
},

'with circuit breaker'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const cluster = new ecs.Cluster(stack, 'EcsCluster');
const taskDefinition = new ecs.FargateTaskDefinition(stack, 'FargateTaskDef');

taskDefinition.addContainer('Container', {
image: ecs.ContainerImage.fromRegistry('hello'),
});

// WHEN
new ecs.FargateService(stack, 'EcsService', {
cluster,
taskDefinition,
circuitBreaker: { rollback: true },
});

// THEN
expect(stack).to(haveResource('AWS::ECS::Service', {
DeploymentConfiguration: {
MaximumPercent: 200,
MinimumHealthyPercent: 50,
DeploymentCircuitBreaker: {
Enable: true,
Rollback: true,
},
},
}));

test.done();
},

'throws an exception if both serviceArn and serviceName were provided for fromEc2ServiceAttributes'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down