Skip to content

Commit

Permalink
feat(ecs): deployment circuit breaker support (#12168)
Browse files Browse the repository at this point in the history
feat(ecs): deployment circuit breaker support

This PR allows you to enable the `deployment circuit breaker` support for Amazon ECS.

Depends on 

- [ ] cfnspec v22 #12170


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
pahud authored Dec 23, 2020
1 parent 52a80bb commit e8801a0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
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
29 changes: 29 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,13 @@ export interface BaseServiceOptions {
* @default - Rolling update (ECS)
*/
readonly deploymentController?: DeploymentController;

/**
* Whether to enable the deployment circuit breaker. If this property is defined, circuit breaker will be implicitly
* enabled.
* @default - disabled
*/
readonly circuitBreaker?: DeploymentCircuitBreaker;
}

/**
Expand Down Expand Up @@ -356,6 +375,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

0 comments on commit e8801a0

Please sign in to comment.