From cff1fcd6961227f5ef8fd0c1fd3a6d108dc28e34 Mon Sep 17 00:00:00 2001 From: mazyu36 Date: Tue, 8 Oct 2024 07:19:07 +0900 Subject: [PATCH] fix(ecs): ecs exec cannot be enabled for ECS Anywhere (ecs.ExternalService) (#31374) ### Issue # (if applicable) Closes #31181. ### Reason for this change In the `ecs.ExternalService` class (ECS Anywhere), the `enableExecuteCommand` property cannot be set to true, so it is not possible to enable ECS exec. However, the [documentation](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-exec.html) states that ECS Anywhere supports ECS Exec. > ECS Exec is supported for tasks that run on the following infrastructure: > Linux and Windows containers on external instances (Amazon ECS Anywhere) ### Description of changes Remove unnecessary if statement. ### Description of how you validated changes Fix an unit test. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/aws-cdk-lib/aws-ecs/README.md | 12 ++-- .../aws-ecs/lib/external/external-service.ts | 4 -- .../test/external/external-service.test.ts | 57 +++++++++++-------- 3 files changed, 39 insertions(+), 34 deletions(-) diff --git a/packages/aws-cdk-lib/aws-ecs/README.md b/packages/aws-cdk-lib/aws-ecs/README.md index 61fbaae1adab9..8225842edfbb7 100644 --- a/packages/aws-cdk-lib/aws-ecs/README.md +++ b/packages/aws-cdk-lib/aws-ecs/README.md @@ -1174,10 +1174,10 @@ const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef'); taskDefinition.addContainer('TheContainer', { image: ecs.ContainerImage.fromRegistry('example-image'), memoryLimitMiB: 256, - logging: ecs.LogDrivers.awsLogs({ + logging: ecs.LogDrivers.awsLogs({ streamPrefix: 'EventDemo', mode: ecs.AwsLogDriverMode.NON_BLOCKING, - maxBufferSize: Size.mebibytes(25), + maxBufferSize: Size.mebibytes(25), }), }); ``` @@ -1606,7 +1606,7 @@ to work, you need to have the SSM plugin for the AWS CLI installed locally. For [Install Session Manager plugin for AWS CLI](https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html). To enable the ECS Exec feature for your containers, set the boolean flag `enableExecuteCommand` to `true` in -your `Ec2Service` or `FargateService`. +your `Ec2Service`, `FargateService` or `ExternalService`. ```ts declare const cluster: ecs.Cluster; @@ -1771,9 +1771,9 @@ const service = new ecs.FargateService(this, 'Service', { ## ServiceManagedVolume Amazon ECS now supports the attachment of Amazon Elastic Block Store (EBS) volumes to ECS tasks, -allowing you to utilize persistent, high-performance block storage with your ECS services. -This feature supports various use cases, such as using EBS volumes as extended ephemeral storage or -loading data from EBS snapshots. +allowing you to utilize persistent, high-performance block storage with your ECS services. +This feature supports various use cases, such as using EBS volumes as extended ephemeral storage or +loading data from EBS snapshots. You can also specify `encrypted: true` so that ECS will manage the KMS key. If you want to use your own KMS key, you may do so by providing both `encrypted: true` and `kmsKeyId`. You can only attach a single volume for each task in the ECS Service. diff --git a/packages/aws-cdk-lib/aws-ecs/lib/external/external-service.ts b/packages/aws-cdk-lib/aws-ecs/lib/external/external-service.ts index 1b21cb3e41c4c..89b52f908554a 100644 --- a/packages/aws-cdk-lib/aws-ecs/lib/external/external-service.ts +++ b/packages/aws-cdk-lib/aws-ecs/lib/external/external-service.ts @@ -105,10 +105,6 @@ export class ExternalService extends BaseService implements IExternalService { throw new Error ('Cloud map options are not supported for External service'); } - if (props.enableExecuteCommand !== undefined) { - throw new Error ('Enable Execute Command options are not supported for External service'); - } - if (props.capacityProviderStrategies !== undefined) { throw new Error ('Capacity Providers are not supported for External service'); } diff --git a/packages/aws-cdk-lib/aws-ecs/test/external/external-service.test.ts b/packages/aws-cdk-lib/aws-ecs/test/external/external-service.test.ts index 5e64a502b225c..d77246d693959 100644 --- a/packages/aws-cdk-lib/aws-ecs/test/external/external-service.test.ts +++ b/packages/aws-cdk-lib/aws-ecs/test/external/external-service.test.ts @@ -237,6 +237,39 @@ describe('external service', () => { }); }); + test('with enableExecuteCommand set to true', () => { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.Vpc(stack, 'MyVpc', {}); + const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc }); + addDefaultCapacityProvider(cluster, stack, vpc); + const taskDefinition = new ecs.ExternalTaskDefinition(stack, 'TaskDef'); + + taskDefinition.addContainer('web', { + image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'), + memoryLimitMiB: 512, + }); + + // WHEN + new ecs.ExternalService(stack, 'ExternalService', { + cluster, + taskDefinition, + enableExecuteCommand: true, + }); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::ECS::Service', { + TaskDefinition: { + Ref: 'TaskDef54694570', + }, + Cluster: { + Ref: 'EcsCluster97242B84', + }, + LaunchType: LaunchType.EXTERNAL, + EnableExecuteCommand: true, + }); + }); + test('throws when task definition is not External compatible', () => { const stack = new cdk.Stack(); const vpc = new ec2.Vpc(stack, 'MyVpc', {}); @@ -306,30 +339,6 @@ describe('external service', () => { }); - test('error if enableExecuteCommand options provided with external service', () => { - // GIVEN - const stack = new cdk.Stack(); - const vpc = new ec2.Vpc(stack, 'MyVpc', {}); - const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc }); - addDefaultCapacityProvider(cluster, stack, vpc); - const taskDefinition = new ecs.ExternalTaskDefinition(stack, 'TaskDef'); - - taskDefinition.addContainer('web', { - image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'), - memoryLimitMiB: 512, - }); - - // THEN - expect(() => new ecs.ExternalService(stack, 'ExternalService', { - cluster, - taskDefinition, - enableExecuteCommand: true, - })).toThrow('Enable Execute Command options are not supported for External service'); - - // THEN - - }); - test('error if capacityProviderStrategies options provided with external service', () => { // GIVEN const stack = new cdk.Stack();