Skip to content

Commit 7908de4

Browse files
rix0rrrElad Ben-Israel
authored andcommitted
fix(aws-ecs): don't emit DesiredCount in daemon mode (#1199)
When configuring a service in Daemon mode, DesiredCount should not be emitted. Fixes #1197.
1 parent 4a4c5f4 commit 7908de4

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

packages/@aws-cdk/aws-ecs/lib/base/base-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export abstract class BaseService extends cdk.Construct
104104
this.taskDefinition = taskDefinition;
105105

106106
this.resource = new cloudformation.ServiceResource(this, "Service", {
107-
desiredCount: props.desiredCount || 1,
107+
desiredCount: props.desiredCount,
108108
serviceName: props.serviceName,
109109
loadBalancers: new cdk.Token(() => this.loadBalancers),
110110
deploymentConfiguration: {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,12 @@ export class Ec2Service extends BaseService implements elb.ILoadBalancerTarget {
8181
throw new Error('Supplied TaskDefinition is not configured for compatibility with EC2');
8282
}
8383

84-
super(parent, name, props, {
84+
super(parent, name, {
85+
...props,
86+
// If daemon, desiredCount must be undefined and that's what we want. Otherwise, default to 1.
87+
desiredCount: props.daemon || props.desiredCount !== undefined ? props.desiredCount : 1,
88+
},
89+
{
8590
cluster: props.cluster.clusterName,
8691
taskDefinition: props.taskDefinition.taskDefinitionArn,
8792
launchType: 'EC2',

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ export class FargateService extends BaseService {
6060
throw new Error('Supplied TaskDefinition is not configured for compatibility with Fargate');
6161
}
6262

63-
super(parent, name, props, {
63+
super(parent, name, {
64+
...props,
65+
desiredCount: props.desiredCount !== undefined ? props.desiredCount : 1,
66+
}, {
6467
cluster: props.cluster.clusterName,
6568
taskDefinition: props.taskDefinition.taskDefinitionArn,
6669
launchType: 'FARGATE',

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ export = {
5656
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
5757
cluster.addDefaultAutoScalingGroupCapacity({ instanceType: new ec2.InstanceType('t2.micro') });
5858
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef');
59+
taskDefinition.addContainer('BaseContainer', {
60+
image: ecs.ContainerImage.fromDockerHub('test'),
61+
memoryReservationMiB: 10,
62+
});
5963

6064
// THEN
6165
test.throws(() => {
@@ -65,8 +69,35 @@ export = {
6569
daemon: true,
6670
desiredCount: 2
6771
});
72+
}, /Don't supply desiredCount/);
73+
74+
test.done();
75+
},
76+
77+
'Output does not contain DesiredCount if daemon mode is set'(test: Test) {
78+
// GIVEN
79+
const stack = new cdk.Stack();
80+
const vpc = new ec2.VpcNetwork(stack, 'MyVpc', {});
81+
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
82+
cluster.addDefaultAutoScalingGroupCapacity({ instanceType: new ec2.InstanceType('t2.micro') });
83+
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef');
84+
taskDefinition.addContainer('BaseContainer', {
85+
image: ecs.ContainerImage.fromDockerHub('test'),
86+
memoryReservationMiB: 10,
6887
});
6988

89+
// WHEN
90+
new ecs.Ec2Service(stack, "Ec2Service", {
91+
cluster,
92+
taskDefinition,
93+
daemon: true,
94+
});
95+
96+
// THEN
97+
expect(stack).to(haveResource('AWS::ECS::Service', (service: any) => {
98+
return service.LaunchType === 'EC2' && service.DesiredCount === undefined;
99+
}));
100+
70101
test.done();
71102
},
72103

0 commit comments

Comments
 (0)