Skip to content

Commit cff1fcd

Browse files
authored
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*
1 parent 842f49a commit cff1fcd

File tree

3 files changed

+39
-34
lines changed

3 files changed

+39
-34
lines changed

packages/aws-cdk-lib/aws-ecs/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -1174,10 +1174,10 @@ const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef');
11741174
taskDefinition.addContainer('TheContainer', {
11751175
image: ecs.ContainerImage.fromRegistry('example-image'),
11761176
memoryLimitMiB: 256,
1177-
logging: ecs.LogDrivers.awsLogs({
1177+
logging: ecs.LogDrivers.awsLogs({
11781178
streamPrefix: 'EventDemo',
11791179
mode: ecs.AwsLogDriverMode.NON_BLOCKING,
1180-
maxBufferSize: Size.mebibytes(25),
1180+
maxBufferSize: Size.mebibytes(25),
11811181
}),
11821182
});
11831183
```
@@ -1606,7 +1606,7 @@ to work, you need to have the SSM plugin for the AWS CLI installed locally. For
16061606
[Install Session Manager plugin for AWS CLI](https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html).
16071607

16081608
To enable the ECS Exec feature for your containers, set the boolean flag `enableExecuteCommand` to `true` in
1609-
your `Ec2Service` or `FargateService`.
1609+
your `Ec2Service`, `FargateService` or `ExternalService`.
16101610

16111611
```ts
16121612
declare const cluster: ecs.Cluster;
@@ -1771,9 +1771,9 @@ const service = new ecs.FargateService(this, 'Service', {
17711771
## ServiceManagedVolume
17721772

17731773
Amazon ECS now supports the attachment of Amazon Elastic Block Store (EBS) volumes to ECS tasks,
1774-
allowing you to utilize persistent, high-performance block storage with your ECS services.
1775-
This feature supports various use cases, such as using EBS volumes as extended ephemeral storage or
1776-
loading data from EBS snapshots.
1774+
allowing you to utilize persistent, high-performance block storage with your ECS services.
1775+
This feature supports various use cases, such as using EBS volumes as extended ephemeral storage or
1776+
loading data from EBS snapshots.
17771777
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`.
17781778

17791779
You can only attach a single volume for each task in the ECS Service.

packages/aws-cdk-lib/aws-ecs/lib/external/external-service.ts

-4
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,6 @@ export class ExternalService extends BaseService implements IExternalService {
105105
throw new Error ('Cloud map options are not supported for External service');
106106
}
107107

108-
if (props.enableExecuteCommand !== undefined) {
109-
throw new Error ('Enable Execute Command options are not supported for External service');
110-
}
111-
112108
if (props.capacityProviderStrategies !== undefined) {
113109
throw new Error ('Capacity Providers are not supported for External service');
114110
}

packages/aws-cdk-lib/aws-ecs/test/external/external-service.test.ts

+33-24
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,39 @@ describe('external service', () => {
237237
});
238238
});
239239

240+
test('with enableExecuteCommand set to true', () => {
241+
// GIVEN
242+
const stack = new cdk.Stack();
243+
const vpc = new ec2.Vpc(stack, 'MyVpc', {});
244+
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
245+
addDefaultCapacityProvider(cluster, stack, vpc);
246+
const taskDefinition = new ecs.ExternalTaskDefinition(stack, 'TaskDef');
247+
248+
taskDefinition.addContainer('web', {
249+
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
250+
memoryLimitMiB: 512,
251+
});
252+
253+
// WHEN
254+
new ecs.ExternalService(stack, 'ExternalService', {
255+
cluster,
256+
taskDefinition,
257+
enableExecuteCommand: true,
258+
});
259+
260+
// THEN
261+
Template.fromStack(stack).hasResourceProperties('AWS::ECS::Service', {
262+
TaskDefinition: {
263+
Ref: 'TaskDef54694570',
264+
},
265+
Cluster: {
266+
Ref: 'EcsCluster97242B84',
267+
},
268+
LaunchType: LaunchType.EXTERNAL,
269+
EnableExecuteCommand: true,
270+
});
271+
});
272+
240273
test('throws when task definition is not External compatible', () => {
241274
const stack = new cdk.Stack();
242275
const vpc = new ec2.Vpc(stack, 'MyVpc', {});
@@ -306,30 +339,6 @@ describe('external service', () => {
306339

307340
});
308341

309-
test('error if enableExecuteCommand options provided with external service', () => {
310-
// GIVEN
311-
const stack = new cdk.Stack();
312-
const vpc = new ec2.Vpc(stack, 'MyVpc', {});
313-
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
314-
addDefaultCapacityProvider(cluster, stack, vpc);
315-
const taskDefinition = new ecs.ExternalTaskDefinition(stack, 'TaskDef');
316-
317-
taskDefinition.addContainer('web', {
318-
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
319-
memoryLimitMiB: 512,
320-
});
321-
322-
// THEN
323-
expect(() => new ecs.ExternalService(stack, 'ExternalService', {
324-
cluster,
325-
taskDefinition,
326-
enableExecuteCommand: true,
327-
})).toThrow('Enable Execute Command options are not supported for External service');
328-
329-
// THEN
330-
331-
});
332-
333342
test('error if capacityProviderStrategies options provided with external service', () => {
334343
// GIVEN
335344
const stack = new cdk.Stack();

0 commit comments

Comments
 (0)