Skip to content

Commit

Permalink
fix(aws-ecs): don't emit DesiredCount in daemon mode (#1199)
Browse files Browse the repository at this point in the history
When configuring a service in Daemon mode, DesiredCount
should not be emitted.

Fixes #1197.
  • Loading branch information
rix0rrr authored and Elad Ben-Israel committed Nov 18, 2018
1 parent 4a4c5f4 commit 7908de4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-ecs/lib/base/base-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export abstract class BaseService extends cdk.Construct
this.taskDefinition = taskDefinition;

this.resource = new cloudformation.ServiceResource(this, "Service", {
desiredCount: props.desiredCount || 1,
desiredCount: props.desiredCount,
serviceName: props.serviceName,
loadBalancers: new cdk.Token(() => this.loadBalancers),
deploymentConfiguration: {
Expand Down
7 changes: 6 additions & 1 deletion packages/@aws-cdk/aws-ecs/lib/ec2/ec2-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ export class Ec2Service extends BaseService implements elb.ILoadBalancerTarget {
throw new Error('Supplied TaskDefinition is not configured for compatibility with EC2');
}

super(parent, name, props, {
super(parent, name, {
...props,
// If daemon, desiredCount must be undefined and that's what we want. Otherwise, default to 1.
desiredCount: props.daemon || props.desiredCount !== undefined ? props.desiredCount : 1,
},
{
cluster: props.cluster.clusterName,
taskDefinition: props.taskDefinition.taskDefinitionArn,
launchType: 'EC2',
Expand Down
5 changes: 4 additions & 1 deletion packages/@aws-cdk/aws-ecs/lib/fargate/fargate-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ export class FargateService extends BaseService {
throw new Error('Supplied TaskDefinition is not configured for compatibility with Fargate');
}

super(parent, name, props, {
super(parent, name, {
...props,
desiredCount: props.desiredCount !== undefined ? props.desiredCount : 1,
}, {
cluster: props.cluster.clusterName,
taskDefinition: props.taskDefinition.taskDefinitionArn,
launchType: 'FARGATE',
Expand Down
31 changes: 31 additions & 0 deletions packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ export = {
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
cluster.addDefaultAutoScalingGroupCapacity({ instanceType: new ec2.InstanceType('t2.micro') });
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef');
taskDefinition.addContainer('BaseContainer', {
image: ecs.ContainerImage.fromDockerHub('test'),
memoryReservationMiB: 10,
});

// THEN
test.throws(() => {
Expand All @@ -65,8 +69,35 @@ export = {
daemon: true,
desiredCount: 2
});
}, /Don't supply desiredCount/);

test.done();
},

'Output does not contain DesiredCount if daemon mode is set'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.VpcNetwork(stack, 'MyVpc', {});
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
cluster.addDefaultAutoScalingGroupCapacity({ instanceType: new ec2.InstanceType('t2.micro') });
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef');
taskDefinition.addContainer('BaseContainer', {
image: ecs.ContainerImage.fromDockerHub('test'),
memoryReservationMiB: 10,
});

// WHEN
new ecs.Ec2Service(stack, "Ec2Service", {
cluster,
taskDefinition,
daemon: true,
});

// THEN
expect(stack).to(haveResource('AWS::ECS::Service', (service: any) => {
return service.LaunchType === 'EC2' && service.DesiredCount === undefined;
}));

test.done();
},

Expand Down

0 comments on commit 7908de4

Please sign in to comment.