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

fix(ecs): DeploymentAlarms property is specified for ECS service with CODE_DEPLOY and EXTERNAL deployment controller #26317

Merged
merged 4 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion packages/aws-cdk-lib/aws-ecs/lib/base/base-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,9 @@ export abstract class BaseService extends Resource
enable: true,
rollback: props.deploymentAlarms.behavior !== AlarmBehavior.FAIL_ON_ALARM,
};
} else if (this.deploymentAlarmsAvailableInRegion()) {
// CloudWatch alarms is only supported for Amazon ECS services that use the rolling update (ECS) deployment controller.
} else if (props.deploymentController?.type !== DeploymentControllerType.CODE_DEPLOY &&
props.deploymentController?.type !== DeploymentControllerType.EXTERNAL && this.deploymentAlarmsAvailableInRegion()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason you are checking for the negative here (not CODE_DEPLOY, not EXTERNAL) and not if the controller is ECS?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no particular reason. I fix this not to use negative condition.

this.deploymentAlarms = {
alarmNames: [],
enable: false,
Expand Down
125 changes: 125 additions & 0 deletions packages/aws-cdk-lib/aws-ecs/test/base-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,131 @@ describe('When import an ECS Service', () => {
});
});

describe('For alarm-based rollbacks', () => {
let stack: cdk.Stack;

beforeEach(() => {
stack = new cdk.Stack();
});

test('deploymentAlarms is set by default for ECS deployment controller', () => {
// GIVEN
const vpc = new ec2.Vpc(stack, 'Vpc');
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
const taskDefinition = new ecs.FargateTaskDefinition(stack, 'FargateTaskDef');
taskDefinition.addContainer('web', {
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
});

// WHEN
new ecs.FargateService(stack, 'FargateService', {
cluster,
taskDefinition,
deploymentController: {
type: ecs.DeploymentControllerType.ECS,
},
minHealthyPercent: 100,
maxHealthyPercent: 200,
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::ECS::Service', {
DeploymentConfiguration: {
Alarms: {
AlarmNames: [],
Enable: false,
Rollback: false,
},
},
});
});

test('deploymentAlarms is set by default when deployment controller is not specified', () => {
// GIVEN
const vpc = new ec2.Vpc(stack, 'Vpc');
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
const taskDefinition = new ecs.FargateTaskDefinition(stack, 'FargateTaskDef');
taskDefinition.addContainer('web', {
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
});

// WHEN
new ecs.FargateService(stack, 'FargateService', {
cluster,
taskDefinition,
minHealthyPercent: 100,
maxHealthyPercent: 200,
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::ECS::Service', {
DeploymentConfiguration: {
Alarms: {
AlarmNames: [],
Enable: false,
Rollback: false,
},
},
});
});

test('should omit deploymentAlarms for CodeDeploy deployment controller', () => {
// GIVEN
const vpc = new ec2.Vpc(stack, 'Vpc');
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
const taskDefinition = new ecs.FargateTaskDefinition(stack, 'FargateTaskDef');
taskDefinition.addContainer('web', {
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
});

// WHEN
new ecs.FargateService(stack, 'FargateService', {
cluster,
taskDefinition,
deploymentController: {
type: ecs.DeploymentControllerType.CODE_DEPLOY,
},
minHealthyPercent: 100,
maxHealthyPercent: 200,
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::ECS::Service', {
DeploymentConfiguration: {
Alarms: Match.absent(),
},
});
});

test('should omit deploymentAlarms for External deployment controller', () => {
// GIVEN
const vpc = new ec2.Vpc(stack, 'Vpc');
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
const taskDefinition = new ecs.FargateTaskDefinition(stack, 'FargateTaskDef');
taskDefinition.addContainer('web', {
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
});

// WHEN
new ecs.FargateService(stack, 'FargateService', {
cluster,
taskDefinition,
deploymentController: {
type: ecs.DeploymentControllerType.EXTERNAL,
},
minHealthyPercent: 100,
maxHealthyPercent: 200,
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::ECS::Service', {
DeploymentConfiguration: {
Alarms: Match.absent(),
},
});
});
});

test.each([
/* breaker, flag => controller in template */
/* Flag off => value present if circuitbreaker */
Expand Down