From c852239936b79581dbcf0dc8d56e3bb76a52e2dc Mon Sep 17 00:00:00 2001 From: Peter Woodworth <44349620+peterwoodworth@users.noreply.github.com> Date: Tue, 5 Apr 2022 11:49:43 -0700 Subject: [PATCH] fix(ecs): 'desiredCount' and 'ephemeralStorageGiB' cannot be tokens (#19453) fixes #16648 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- .../application-load-balanced-service-base.ts | 2 +- .../load-balanced-fargate-service.test.ts | 26 +++++++++++++++++++ .../lib/fargate/fargate-task-definition.ts | 5 ++-- .../fargate/fargate-task-definition.test.ts | 19 ++++++++++++++ 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/base/application-load-balanced-service-base.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/base/application-load-balanced-service-base.ts index b4c41ed5aa990..755683a62787e 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/base/application-load-balanced-service-base.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/base/application-load-balanced-service-base.ts @@ -413,7 +413,7 @@ export abstract class ApplicationLoadBalancedServiceBase extends CoreConstruct { } this.cluster = props.cluster || this.getDefaultCluster(this, props.vpc); - if (props.desiredCount !== undefined && props.desiredCount < 1) { + if (props.desiredCount !== undefined && !cdk.Token.isUnresolved(props.desiredCount) && props.desiredCount < 1) { throw new Error('You must specify a desiredCount greater than 0'); } diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/load-balanced-fargate-service.test.ts b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/load-balanced-fargate-service.test.ts index 70763a1bc2277..a00b676894c83 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/load-balanced-fargate-service.test.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/load-balanced-fargate-service.test.ts @@ -1047,3 +1047,29 @@ test('test Network load balanced service with docker labels defined', () => { ], }); }); + +test('Passing in token for desiredCount will not throw error', () => { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.Vpc(stack, 'VPC'); + const cluster = new ecs.Cluster(stack, 'Cluster', { vpc }); + const param = new cdk.CfnParameter(stack, 'prammm', { + type: 'Number', + default: 1, + }); + + // WHEN + const service = new ecsPatterns.ApplicationLoadBalancedFargateService(stack, 'Service', { + cluster, + taskImageOptions: { + image: ecs.ContainerImage.fromRegistry('/aws/aws-example-app'), + dockerLabels: { label1: 'labelValue1', label2: 'labelValue2' }, + }, + desiredCount: param.valueAsNumber, + }); + + // THEN + expect(() => { + service.internalDesiredCount; + }).toBeTruthy; +}); diff --git a/packages/@aws-cdk/aws-ecs/lib/fargate/fargate-task-definition.ts b/packages/@aws-cdk/aws-ecs/lib/fargate/fargate-task-definition.ts index a1e957ed7a21c..c4e0831fbb4f7 100644 --- a/packages/@aws-cdk/aws-ecs/lib/fargate/fargate-task-definition.ts +++ b/packages/@aws-cdk/aws-ecs/lib/fargate/fargate-task-definition.ts @@ -1,4 +1,4 @@ -import { Tokenization } from '@aws-cdk/core'; +import { Tokenization, Token } from '@aws-cdk/core'; import { Construct } from 'constructs'; import { ImportedTaskDefinition } from '../base/_imported-task-definition'; import { @@ -140,7 +140,8 @@ export class FargateTaskDefinition extends TaskDefinition implements IFargateTas networkMode: NetworkMode.AWS_VPC, }); - if (props.ephemeralStorageGiB && (props.ephemeralStorageGiB < 21 || props.ephemeralStorageGiB > 200)) { + // eslint-disable-next-line max-len + if (props.ephemeralStorageGiB && !Token.isUnresolved(props.ephemeralStorageGiB) && (props.ephemeralStorageGiB < 21 || props.ephemeralStorageGiB > 200)) { throw new Error('Ephemeral storage size must be between 21GiB and 200GiB'); } diff --git a/packages/@aws-cdk/aws-ecs/test/fargate/fargate-task-definition.test.ts b/packages/@aws-cdk/aws-ecs/test/fargate/fargate-task-definition.test.ts index e0b7afca3389f..e2eb71d53ceee 100644 --- a/packages/@aws-cdk/aws-ecs/test/fargate/fargate-task-definition.test.ts +++ b/packages/@aws-cdk/aws-ecs/test/fargate/fargate-task-definition.test.ts @@ -251,6 +251,25 @@ describe('fargate task definition', () => { 'Add the \'taskRole\' in ImportedTaskDefinitionProps to instantiate ImportedTaskDefinition'); }); + test('Passing in token for ephemeral storage will not throw error', () => { + // GIVEN + const stack = new cdk.Stack(); + + // WHEN + const param = new cdk.CfnParameter(stack, 'prammm', { + type: 'Number', + default: 1, + }); + + const taskDefinition = new ecs.FargateTaskDefinition(stack, 'FargateTaskDef', { + ephemeralStorageGiB: param.valueAsNumber, + }); + + // THEN + expect(() => { + taskDefinition.ephemeralStorageGiB; + }).toBeTruthy; + }); test('runtime testing for windows container', () => { // GIVEN