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 be6b0df0cccc1..c9d602b28a9da 100644 --- a/packages/@aws-cdk/aws-ecs/lib/base/base-service.ts +++ b/packages/@aws-cdk/aws-ecs/lib/base/base-service.ts @@ -38,7 +38,7 @@ export interface BaseServiceProps { * service's DesiredCount value, that can run in a service during a * deployment. * - * @default 200 + * @default 100 if daemon, otherwise 200 */ readonly maximumPercent?: number; @@ -47,7 +47,7 @@ export interface BaseServiceProps { * the Amazon ECS service's DesiredCount value, that must * continue to run and remain healthy during a deployment. * - * @default 50 + * @default 0 if daemon, otherwise 50 */ readonly minimumHealthyPercent?: number; 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 324ef039cdc49..cbe39a53a5e44 100644 --- a/packages/@aws-cdk/aws-ecs/lib/ec2/ec2-service.ts +++ b/packages/@aws-cdk/aws-ecs/lib/ec2/ec2-service.ts @@ -70,6 +70,14 @@ export class Ec2Service extends BaseService implements elb.ILoadBalancerTarget { throw new Error('Daemon mode launches one task on every instance. Don\'t supply desiredCount.'); } + if (props.daemon && props.maximumPercent !== undefined && props.maximumPercent !== 100) { + throw new Error('Maximum percent must be 100 for daemon mode.'); + } + + if (props.daemon && props.minimumHealthyPercent !== undefined && props.minimumHealthyPercent !== 0) { + throw new Error('Minimum healthy percent must be 0 for daemon mode.'); + } + if (!isEc2Compatible(props.taskDefinition.compatibility)) { throw new Error('Supplied TaskDefinition is not configured for compatibility with EC2'); } @@ -78,6 +86,8 @@ export class Ec2Service extends BaseService implements elb.ILoadBalancerTarget { ...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, + maximumPercent: props.daemon && props.maximumPercent === undefined ? 100 : props.maximumPercent, + minimumHealthyPercent: props.daemon && props.minimumHealthyPercent === undefined ? 0 : props.minimumHealthyPercent , }, { cluster: props.cluster.clusterName, 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 c4d9f677837da..3e81ea33c5b5d 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 @@ -75,6 +75,56 @@ export = { test.done(); }, + "errors if daemon and maximumPercent not 100"(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.VpcNetwork(stack, 'MyVpc', {}); + const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc }); + cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') }); + const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef'); + taskDefinition.addContainer('BaseContainer', { + image: ecs.ContainerImage.fromRegistry('test'), + memoryReservationMiB: 10, + }); + + // THEN + test.throws(() => { + new ecs.Ec2Service(stack, "Ec2Service", { + cluster, + taskDefinition, + daemon: true, + maximumPercent: 300 + }); + }, /Maximum percent must be 100 for daemon mode./); + + test.done(); + }, + + "errors if daemon and minimum not 0"(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.VpcNetwork(stack, 'MyVpc', {}); + const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc }); + cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') }); + const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef'); + taskDefinition.addContainer('BaseContainer', { + image: ecs.ContainerImage.fromRegistry('test'), + memoryReservationMiB: 10, + }); + + // THEN + test.throws(() => { + new ecs.Ec2Service(stack, "Ec2Service", { + cluster, + taskDefinition, + daemon: true, + minimumHealthyPercent: 50 + }); + }, /Minimum healthy percent must be 0 for daemon mode./); + + test.done(); + }, + 'Output does not contain DesiredCount if daemon mode is set'(test: Test) { // GIVEN const stack = new cdk.Stack(); @@ -141,7 +191,11 @@ export = { // THEN expect(stack).to(haveResource("AWS::ECS::Service", { - SchedulingStrategy: "DAEMON" + SchedulingStrategy: "DAEMON", + DeploymentConfiguration: { + MaximumPercent: 100, + MinimumHealthyPercent: 0 + }, })); test.done();