From 53d9b54ff6792da54c5d89f6a4b75da0056bffe2 Mon Sep 17 00:00:00 2001 From: Matthew Zizzi Date: Sun, 11 Jul 2021 14:09:55 -0400 Subject: [PATCH 1/3] enableExecuteCommand for select ecs-patterns constructs --- ...plication-load-balanced-fargate-service.ts | 8 +++ .../test.load-balanced-fargate-service-v2.ts | 69 ++++++++++++++++++- 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts index 19a2501355223..03fca1240b846 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts @@ -91,6 +91,13 @@ export interface ApplicationLoadBalancedFargateServiceProps extends ApplicationL * @default - A new security group is created. */ readonly securityGroups?: ISecurityGroup[]; + + /** + * Whether to enable the ability to execute into a container. + * + * @default - undefined + */ + readonly enableExecuteCommand?: boolean; } /** @@ -174,6 +181,7 @@ export class ApplicationLoadBalancedFargateService extends ApplicationLoadBalanc circuitBreaker: props.circuitBreaker, securityGroups: props.securityGroups, vpcSubnets: props.taskSubnets, + enableExecuteCommand: props.enableExecuteCommand, }); this.addServiceAsTarget(this.service); } diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service-v2.ts b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service-v2.ts index cad68a5a270ae..b0be85c133b6e 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service-v2.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service-v2.ts @@ -1,4 +1,4 @@ -import { expect, haveResource, haveResourceLike } from '@aws-cdk/assert-internal'; +import { ABSENT, expect, haveResource, haveResourceLike } from '@aws-cdk/assert-internal'; import { Vpc } from '@aws-cdk/aws-ec2'; import * as ecs from '@aws-cdk/aws-ecs'; import { CompositePrincipal, Role, ServicePrincipal } from '@aws-cdk/aws-iam'; @@ -386,6 +386,73 @@ export = { test.done(); }, + + 'test with enableExecuteCommand set to true'(test: Test) { + // GIVEN + const stack = new Stack(); + const vpc = new Vpc(stack, 'VPC'); + const cluster = new ecs.Cluster(stack, 'Cluster', { vpc }); + + // WHEN + new ApplicationLoadBalancedFargateService(stack, 'Service', { + cluster, + taskImageOptions: { + image: ecs.ContainerImage.fromRegistry('test'), + }, + enableExecuteCommand: true, + }); + + // THEN - stack contains a service with execute-command enabled + expect(stack).to(haveResourceLike('AWS::ECS::Service', { + EnableExecuteCommand: true, + })); + + test.done(); + }, + + 'test with enableExecuteCommand omitted'(test: Test) { + // GIVEN + const stack = new Stack(); + const vpc = new Vpc(stack, 'VPC'); + const cluster = new ecs.Cluster(stack, 'Cluster', { vpc }); + + // WHEN + new ApplicationLoadBalancedFargateService(stack, 'Service', { + cluster, + taskImageOptions: { + image: ecs.ContainerImage.fromRegistry('test'), + }, + }); + + // THEN - stack contains a service with execute-command enabled + expect(stack).to(haveResourceLike('AWS::ECS::Service', { + EnableExecuteCommand: ABSENT, + })); + + test.done(); + }, + + 'test with enableExecuteCommand false'(test: Test) { + // GIVEN + const stack = new Stack(); + const vpc = new Vpc(stack, 'VPC'); + const cluster = new ecs.Cluster(stack, 'Cluster', { vpc }); + + // WHEN + new ApplicationLoadBalancedFargateService(stack, 'Service', { + cluster, + taskImageOptions: { + image: ecs.ContainerImage.fromRegistry('test'), + }, + }); + + // THEN - stack contains a service with execute-command enabled + expect(stack).to(haveResourceLike('AWS::ECS::Service', { + EnableExecuteCommand: false, + })); + + test.done(); + }, }, 'When Network Load Balancer': { From 5ca1032b18aa8de9cbb0d21deed66c6ad1a95d45 Mon Sep 17 00:00:00 2001 From: Matthew Zizzi Date: Sun, 11 Jul 2021 16:36:48 -0400 Subject: [PATCH 2/3] fix busted test --- .../test/fargate/test.load-balanced-fargate-service-v2.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service-v2.ts b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service-v2.ts index b0be85c133b6e..c00b5450aaac0 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service-v2.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service-v2.ts @@ -444,6 +444,7 @@ export = { taskImageOptions: { image: ecs.ContainerImage.fromRegistry('test'), }, + enableExecuteCommand: false, }); // THEN - stack contains a service with execute-command enabled From 2addc151c3ada721bbad9f1bd8791a66bdb70df1 Mon Sep 17 00:00:00 2001 From: Matthew Zizzi Date: Thu, 2 Sep 2021 15:59:51 -0400 Subject: [PATCH 3/3] fix typos in enableExecuteCommand tests --- .../test/fargate/test.load-balanced-fargate-service-v2.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service-v2.ts b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service-v2.ts index c00b5450aaac0..d8a46135a1464 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service-v2.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service-v2.ts @@ -424,7 +424,7 @@ export = { }, }); - // THEN - stack contains a service with execute-command enabled + // THEN - stack contains a service with execute-command omitted expect(stack).to(haveResourceLike('AWS::ECS::Service', { EnableExecuteCommand: ABSENT, })); @@ -447,7 +447,7 @@ export = { enableExecuteCommand: false, }); - // THEN - stack contains a service with execute-command enabled + // THEN - stack contains a service with execute-command set to false expect(stack).to(haveResourceLike('AWS::ECS::Service', { EnableExecuteCommand: false, }));