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 2 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
16 changes: 16 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,22 @@ 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 `deploymentCircuitBreaker` to enable
deployment circuit breaker and `deploymentRollback` for automatically 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,
deploymentCircuitBreaker: true,
deploymentRollback: 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
26 changes: 26 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 @@ -161,6 +161,19 @@ export interface BaseServiceOptions {
* @default - Rolling update (ECS)
*/
readonly deploymentController?: DeploymentController;

/**
* Whether to enable the deplloyment circuit breaker
* @default false
*/
readonly deploymentCircuitBreaker?: boolean;

/**
* Whether to enable Amazon ECS to roll back the service if a service deployment fails. If rollback is enabled,
* when a service deployment fails, the service is rolled back to the last deployment that completed successfully.
* @default false
*/
readonly deploymentRollback?: boolean;
}

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

const deploymentConfiguration = {
DeploymentCircuitBreaker: {
Enable: props.deploymentCircuitBreaker,
Rollback: props.deploymentRollback,
},
};

// TODO: fix this when this property is available in CfnService
// we only override this property if any of the props is defined
if (props.deploymentCircuitBreaker || props.deploymentRollback) {
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
39 changes: 39 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,8 @@ export = {
deploymentController: {
type: ecs.DeploymentControllerType.CODE_DEPLOY,
},
deploymentCircuitBreaker: true,
deploymentRollback: true,
securityGroup: new ec2.SecurityGroup(stack, 'SecurityGroup1', {
allowAllOutbound: true,
description: 'Example',
Expand All @@ -235,6 +237,10 @@ export = {
DeploymentConfiguration: {
MaximumPercent: 150,
MinimumHealthyPercent: 55,
DeploymentCircuitBreaker: {
Enable: true,
Rollback: true,
},
},
DeploymentController: {
Type: ecs.DeploymentControllerType.CODE_DEPLOY,
Expand Down Expand Up @@ -1786,6 +1792,39 @@ 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,
deploymentCircuitBreaker: true,
deploymentRollback: 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