From 88d4149432d55e65b23448fd58d8ec3e96f3e72c Mon Sep 17 00:00:00 2001 From: Kyle Roach Date: Sun, 25 Oct 2020 00:49:00 -0400 Subject: [PATCH 1/2] feat(ecs-patterns): containerName for QueueProcessingEc2Service resolves #10517 --- packages/@aws-cdk/aws-ecs-patterns/README.md | 1 + .../lib/ecs/queue-processing-ecs-service.ts | 11 +++++++- .../ec2/test.queue-processing-ecs-service.ts | 27 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-ecs-patterns/README.md b/packages/@aws-cdk/aws-ecs-patterns/README.md index cd88ea5133292..057d05d9d350b 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/README.md +++ b/packages/@aws-cdk/aws-ecs-patterns/README.md @@ -33,6 +33,7 @@ const loadBalancedEcsService = new ecsPatterns.ApplicationLoadBalancedEc2Service }, }, desiredCount: 2, + containerName: 'test', }); ``` diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/queue-processing-ecs-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/queue-processing-ecs-service.ts index 0814ac40ba206..7e8eb03f4bf78 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/queue-processing-ecs-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/queue-processing-ecs-service.ts @@ -52,6 +52,13 @@ export interface QueueProcessingEc2ServiceProps extends QueueProcessingServiceBa * @default - No memory reserved. */ readonly memoryReservationMiB?: number; + + /** + * Optional name for the container added + * + * @default - QueueProcessingContainer + */ + readonly containerName?: string; } /** @@ -74,11 +81,13 @@ export class QueueProcessingEc2Service extends QueueProcessingServiceBase { constructor(scope: Construct, id: string, props: QueueProcessingEc2ServiceProps) { super(scope, id, props); + const containerName = props.containerName ?? 'QueueProcessingContainer'; + // Create a Task Definition for the container to start this.taskDefinition = new Ec2TaskDefinition(this, 'QueueProcessingTaskDef', { family: props.family, }); - this.taskDefinition.addContainer('QueueProcessingContainer', { + this.taskDefinition.addContainer(containerName, { image: props.image, memoryLimitMiB: props.memoryLimitMiB, memoryReservationMiB: props.memoryReservationMiB, diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.queue-processing-ecs-service.ts b/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.queue-processing-ecs-service.ts index 4bfa0732591cb..67e9cde5c4e92 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.queue-processing-ecs-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.queue-processing-ecs-service.ts @@ -281,4 +281,31 @@ export = { test.done(); }, + + 'can set custom containerName'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.Vpc(stack, 'VPC'); + const cluster = new ecs.Cluster(stack, 'Cluster', { vpc }); + cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') }); + + // WHEN + new ecsPatterns.QueueProcessingEc2Service(stack, 'Service', { + cluster, + memoryLimitMiB: 512, + image: ecs.ContainerImage.fromRegistry('test'), + containerName: 'my-container', + }); + + // THEN + expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', { + ContainerDefinitions: [ + { + Name: 'my-container', + }, + ], + })); + + test.done(); + }, }; From fad27f65a73dc3b9c208439c5e474295491381da Mon Sep 17 00:00:00 2001 From: Kyle Roach Date: Tue, 3 Nov 2020 10:02:37 -0400 Subject: [PATCH 2/2] feat: Configre containerName for QueueProcessingFargateService --- packages/@aws-cdk/aws-ecs-patterns/README.md | 7 +++--- .../queue-processing-fargate-service.ts | 12 ++++++++- .../test.queue-processing-fargate-service.ts | 25 +++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/packages/@aws-cdk/aws-ecs-patterns/README.md b/packages/@aws-cdk/aws-ecs-patterns/README.md index 057d05d9d350b..8f39f4b56cfbc 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/README.md +++ b/packages/@aws-cdk/aws-ecs-patterns/README.md @@ -33,7 +33,6 @@ const loadBalancedEcsService = new ecsPatterns.ApplicationLoadBalancedEc2Service }, }, desiredCount: 2, - containerName: 'test', }); ``` @@ -257,7 +256,8 @@ const queueProcessingEc2Service = new QueueProcessingEc2Service(stack, 'Service' TEST_ENVIRONMENT_VARIABLE2: "test environment variable 2 value" }, queue, - maxScalingCapacity: 5 + maxScalingCapacity: 5, + containerName: 'test', }); ``` @@ -276,7 +276,8 @@ const queueProcessingFargateService = new QueueProcessingFargateService(stack, ' TEST_ENVIRONMENT_VARIABLE2: "test environment variable 2 value" }, queue, - maxScalingCapacity: 5 + maxScalingCapacity: 5, + containerName: 'test', }); ``` diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/queue-processing-fargate-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/queue-processing-fargate-service.ts index 931fe00d9985b..bd0e9a9eeaa6b 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/queue-processing-fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/queue-processing-fargate-service.ts @@ -59,6 +59,13 @@ export interface QueueProcessingFargateServiceProps extends QueueProcessingServi * @default Latest */ readonly platformVersion?: FargatePlatformVersion; + + /** + * Optional name for the container added + * + * @default - QueueProcessingContainer + */ + readonly containerName?: string; } /** @@ -86,7 +93,10 @@ export class QueueProcessingFargateService extends QueueProcessingServiceBase { cpu: props.cpu || 256, family: props.family, }); - this.taskDefinition.addContainer('QueueProcessingContainer', { + + const containerName = props.containerName ?? 'QueueProcessingContainer'; + + this.taskDefinition.addContainer(containerName, { image: props.image, command: props.command, environment: this.environment, diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.queue-processing-fargate-service.ts b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.queue-processing-fargate-service.ts index 27cb8a08ed14a..bd7a2cb0dd293 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.queue-processing-fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.queue-processing-fargate-service.ts @@ -279,4 +279,29 @@ export = { test.done(); }, + + 'can set custom containerName'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.Vpc(stack, 'VPC'); + const cluster = new ecs.Cluster(stack, 'Cluster', { vpc }); + cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') }); + + // WHEN + new ecsPatterns.QueueProcessingFargateService(stack, 'Service', { + cluster, + containerName: 'my-container', + image: ecs.ContainerImage.fromRegistry('test'), + }); + + expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', { + ContainerDefinitions: [ + { + Name: 'my-container', + }, + ], + })); + + test.done(); + }, };