From 7908de477329b7cb04cca93d1c6b56e2aefaac0f Mon Sep 17 00:00:00 2001 From: Rico Huijbers Date: Sun, 18 Nov 2018 11:36:53 +0100 Subject: [PATCH] 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. --- .../@aws-cdk/aws-ecs/lib/base/base-service.ts | 2 +- .../@aws-cdk/aws-ecs/lib/ec2/ec2-service.ts | 7 ++++- .../aws-ecs/lib/fargate/fargate-service.ts | 5 ++- .../aws-ecs/test/ec2/test.ec2-service.ts | 31 +++++++++++++++++++ 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/packages/@aws-cdk/aws-ecs/lib/base/base-service.ts b/packages/@aws-cdk/aws-ecs/lib/base/base-service.ts index 19704b50b0b65..351c90592a240 100644 --- a/packages/@aws-cdk/aws-ecs/lib/base/base-service.ts +++ b/packages/@aws-cdk/aws-ecs/lib/base/base-service.ts @@ -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: { diff --git a/packages/@aws-cdk/aws-ecs/lib/ec2/ec2-service.ts b/packages/@aws-cdk/aws-ecs/lib/ec2/ec2-service.ts index 8625e5ac6ff91..36fe21501c959 100644 --- a/packages/@aws-cdk/aws-ecs/lib/ec2/ec2-service.ts +++ b/packages/@aws-cdk/aws-ecs/lib/ec2/ec2-service.ts @@ -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', diff --git a/packages/@aws-cdk/aws-ecs/lib/fargate/fargate-service.ts b/packages/@aws-cdk/aws-ecs/lib/fargate/fargate-service.ts index 44e451de48914..c3caf6ef09971 100644 --- a/packages/@aws-cdk/aws-ecs/lib/fargate/fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs/lib/fargate/fargate-service.ts @@ -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', diff --git a/packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-service.ts b/packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-service.ts index 236264838461c..e3eb27442a3db 100644 --- a/packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-service.ts +++ b/packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-service.ts @@ -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(() => { @@ -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(); },