Skip to content

Commit 56c9401

Browse files
committed
fix(ecs): prevent unnecessary updates when setting availabilityZoneRebalancing=DISABLED
1 parent 569f0c3 commit 56c9401

File tree

4 files changed

+142
-2
lines changed

4 files changed

+142
-2
lines changed

packages/aws-cdk-lib/aws-ecs/lib/ec2/ec2-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ export class Ec2Service extends BaseService implements IEc2Service {
214214
placementConstraints: Lazy.any({ produce: () => this.constraints }),
215215
placementStrategies: Lazy.any({ produce: () => this.strategies }, { omitEmptyArray: true }),
216216
schedulingStrategy: props.daemon ? 'DAEMON' : 'REPLICA',
217-
availabilityZoneRebalancing: props.availabilityZoneRebalancing,
217+
availabilityZoneRebalancing: props.availabilityZoneRebalancing === AvailabilityZoneRebalancing.ENABLED ? props.availabilityZoneRebalancing : undefined,
218218
}, props.taskDefinition);
219219
// Enhanced CDK Analytics Telemetry
220220
addConstructMetadata(this, props);

packages/aws-cdk-lib/aws-ecs/lib/fargate/fargate-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ export class FargateService extends BaseService implements IFargateService {
184184
cluster: props.cluster.clusterName,
185185
taskDefinition: props.deploymentController?.type === DeploymentControllerType.EXTERNAL ? undefined : props.taskDefinition.taskDefinitionArn,
186186
platformVersion: props.platformVersion,
187-
availabilityZoneRebalancing: props.availabilityZoneRebalancing,
187+
availabilityZoneRebalancing: props.availabilityZoneRebalancing === AvailabilityZoneRebalancing.ENABLED ? props.availabilityZoneRebalancing : undefined,
188188
}, props.taskDefinition);
189189

190190
// Enhanced CDK Analytics Telemetry

packages/aws-cdk-lib/aws-ecs/test/ec2/ec2-service.test.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4588,5 +4588,77 @@ describe('ec2 service', () => {
45884588
});
45894589
}).toThrow(/only specify either serviceArn or serviceName/);
45904590
});
4591+
4592+
test('does not include AvailabilityZoneRebalancing in CloudFormation when explicitly set to DISABLED', () => {
4593+
// GIVEN
4594+
const stack = new cdk.Stack();
4595+
const vpc = new ec2.Vpc(stack, 'MyVpc', {});
4596+
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
4597+
addDefaultCapacityProvider(cluster, stack, vpc);
4598+
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef');
4599+
4600+
taskDefinition.addContainer('web', {
4601+
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
4602+
memoryLimitMiB: 512,
4603+
});
4604+
4605+
// WHEN
4606+
new ecs.Ec2Service(stack, 'Ec2Service', {
4607+
cluster,
4608+
taskDefinition,
4609+
availabilityZoneRebalancing: ecs.AvailabilityZoneRebalancing.DISABLED,
4610+
});
4611+
4612+
// THEN
4613+
Template.fromStack(stack).hasResourceProperties('AWS::ECS::Service', {
4614+
TaskDefinition: {
4615+
Ref: 'Ec2TaskDef0226F28C',
4616+
},
4617+
Cluster: {
4618+
Ref: 'EcsCluster97242B84',
4619+
},
4620+
LaunchType: LaunchType.EC2,
4621+
SchedulingStrategy: 'REPLICA',
4622+
// AvailabilityZoneRebalancing should be absent when explicitly set to DISABLED
4623+
// This prevents unnecessary CloudFormation changes
4624+
AvailabilityZoneRebalancing: Match.absent(),
4625+
});
4626+
});
4627+
4628+
test('includes AvailabilityZoneRebalancing in CloudFormation when explicitly set to ENABLED', () => {
4629+
// GIVEN
4630+
const stack = new cdk.Stack();
4631+
const vpc = new ec2.Vpc(stack, 'MyVpc', {});
4632+
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
4633+
addDefaultCapacityProvider(cluster, stack, vpc);
4634+
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef');
4635+
4636+
taskDefinition.addContainer('web', {
4637+
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
4638+
memoryLimitMiB: 512,
4639+
});
4640+
4641+
// WHEN
4642+
new ecs.Ec2Service(stack, 'Ec2Service', {
4643+
cluster,
4644+
taskDefinition,
4645+
availabilityZoneRebalancing: ecs.AvailabilityZoneRebalancing.ENABLED,
4646+
maxHealthyPercent: 150, // Required when ENABLED
4647+
});
4648+
4649+
// THEN
4650+
Template.fromStack(stack).hasResourceProperties('AWS::ECS::Service', {
4651+
TaskDefinition: {
4652+
Ref: 'Ec2TaskDef0226F28C',
4653+
},
4654+
Cluster: {
4655+
Ref: 'EcsCluster97242B84',
4656+
},
4657+
LaunchType: LaunchType.EC2,
4658+
SchedulingStrategy: 'REPLICA',
4659+
// AvailabilityZoneRebalancing should be present when explicitly set to ENABLED
4660+
AvailabilityZoneRebalancing: 'ENABLED',
4661+
});
4662+
});
45914663
});
45924664
});

packages/aws-cdk-lib/aws-ecs/test/fargate/fargate-service.test.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4985,5 +4985,73 @@ describe('fargate service', () => {
49854985
});
49864986
}).toThrow(/You can only specify either propagateTags or propagateTaskTagsFrom. Alternatively, you can leave both blank/);
49874987
});
4988+
4989+
test('does not include AvailabilityZoneRebalancing in CloudFormation when explicitly set to DISABLED', () => {
4990+
// GIVEN
4991+
const stack = new cdk.Stack();
4992+
const vpc = new ec2.Vpc(stack, 'MyVpc', {});
4993+
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
4994+
const taskDefinition = new ecs.FargateTaskDefinition(stack, 'FargateTaskDef');
4995+
4996+
taskDefinition.addContainer('web', {
4997+
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
4998+
memoryLimitMiB: 512,
4999+
});
5000+
5001+
// WHEN
5002+
new ecs.FargateService(stack, 'FargateService', {
5003+
cluster,
5004+
taskDefinition,
5005+
availabilityZoneRebalancing: ecs.AvailabilityZoneRebalancing.DISABLED,
5006+
});
5007+
5008+
// THEN
5009+
Template.fromStack(stack).hasResourceProperties('AWS::ECS::Service', {
5010+
TaskDefinition: {
5011+
Ref: 'FargateTaskDefC6FB60B4',
5012+
},
5013+
Cluster: {
5014+
Ref: 'EcsCluster97242B84',
5015+
},
5016+
LaunchType: LaunchType.FARGATE,
5017+
// AvailabilityZoneRebalancing should be absent when explicitly set to DISABLED
5018+
// This prevents unnecessary CloudFormation changes
5019+
AvailabilityZoneRebalancing: Match.absent(),
5020+
});
5021+
});
5022+
5023+
test('includes AvailabilityZoneRebalancing in CloudFormation when explicitly set to ENABLED', () => {
5024+
// GIVEN
5025+
const stack = new cdk.Stack();
5026+
const vpc = new ec2.Vpc(stack, 'MyVpc', {});
5027+
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
5028+
const taskDefinition = new ecs.FargateTaskDefinition(stack, 'FargateTaskDef');
5029+
5030+
taskDefinition.addContainer('web', {
5031+
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
5032+
memoryLimitMiB: 512,
5033+
});
5034+
5035+
// WHEN
5036+
new ecs.FargateService(stack, 'FargateService', {
5037+
cluster,
5038+
taskDefinition,
5039+
availabilityZoneRebalancing: ecs.AvailabilityZoneRebalancing.ENABLED,
5040+
maxHealthyPercent: 150, // Required when ENABLED
5041+
});
5042+
5043+
// THEN
5044+
Template.fromStack(stack).hasResourceProperties('AWS::ECS::Service', {
5045+
TaskDefinition: {
5046+
Ref: 'FargateTaskDefC6FB60B4',
5047+
},
5048+
Cluster: {
5049+
Ref: 'EcsCluster97242B84',
5050+
},
5051+
LaunchType: LaunchType.FARGATE,
5052+
// AvailabilityZoneRebalancing should be present when explicitly set to ENABLED
5053+
AvailabilityZoneRebalancing: 'ENABLED',
5054+
});
5055+
});
49885056
});
49895057
});

0 commit comments

Comments
 (0)