Skip to content

Commit 3c0756a

Browse files
authored
feat(ecs): support pseudo terminal allocation in container definition (#24790)
ECS supports the allocation of a pseudo-terminal in the container definition, but the ContainerDefinition construct does not expose any way to configure it. A pseudoTerminal property has been added to ContainerDefinitionOptions and to the ContainerDefinition class and it is passed down the L1 construct. Closes #16263. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 1b94ea6 commit 3c0756a

22 files changed

+5065
-0
lines changed

Diff for: packages/@aws-cdk/aws-ecs/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -1360,3 +1360,18 @@ const customService = new ecs.FargateService(this, 'CustomizedService', {
13601360
},
13611361
});
13621362
```
1363+
1364+
## Enable pseudo-terminal (TTY) allocation
1365+
1366+
You can allocate a pseudo-terminal (TTY) for a container passing `pseudoTerminal` option while adding the container
1367+
to the task definition.
1368+
This maps to Tty option in the ["Create a container section"](https://docs.docker.com/engine/api/v1.38/#operation/ContainerCreate)
1369+
of the [Docker Remote API](https://docs.docker.com/engine/api/v1.38/) and the --tty option to [`docker run`](https://docs.docker.com/engine/reference/commandline/run/).
1370+
1371+
```ts
1372+
const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef');
1373+
taskDefinition.addContainer('TheContainer', {
1374+
image: ecs.ContainerImage.fromRegistry('example-image'),
1375+
pseudoTerminal: true
1376+
});
1377+
```

Diff for: packages/@aws-cdk/aws-ecs/lib/container-definition.ts

+17
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,15 @@ export interface ContainerDefinitionOptions {
349349
* @see https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#container_definition_systemcontrols
350350
*/
351351
readonly systemControls?: SystemControl[];
352+
353+
/**
354+
* When this parameter is true, a TTY is allocated. This parameter maps to Tty in the "Create a container section" of the
355+
* Docker Remote API and the --tty option to `docker run`.
356+
*
357+
* @default - false
358+
* @see https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#container_definition_pseudoterminal
359+
*/
360+
readonly pseudoTerminal?: boolean;
352361
}
353362

354363
/**
@@ -445,6 +454,11 @@ export class ContainerDefinition extends Construct {
445454
*/
446455
private readonly inferenceAcceleratorResources: string[] = [];
447456

457+
/**
458+
* Specifies whether a TTY must be allocated for this container.
459+
*/
460+
public readonly pseudoTerminal?: boolean;
461+
448462
/**
449463
* The configured container links
450464
*/
@@ -512,6 +526,8 @@ export class ContainerDefinition extends Construct {
512526
if (props.inferenceAcceleratorResources) {
513527
this.addInferenceAcceleratorResource(...props.inferenceAcceleratorResources);
514528
}
529+
530+
this.pseudoTerminal = props.pseudoTerminal;
515531
}
516532

517533
/**
@@ -756,6 +772,7 @@ export class ContainerDefinition extends Construct {
756772
name: this.containerName,
757773
portMappings: cdk.Lazy.any({ produce: () => this.portMappings.map(renderPortMapping) }, { omitEmptyArray: true }),
758774
privileged: this.props.privileged,
775+
pseudoTerminal: this.props.pseudoTerminal,
759776
readonlyRootFilesystem: this.props.readonlyRootFilesystem,
760777
repositoryCredentials: this.imageConfig.repositoryCredentials,
761778
startTimeout: this.props.startTimeout && this.props.startTimeout.toSeconds(),

Diff for: packages/@aws-cdk/aws-ecs/test/container-definition.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ describe('container definition', () => {
305305
gpuCount: 256,
306306
hostname: 'host.example.com',
307307
privileged: true,
308+
pseudoTerminal: true,
308309
readonlyRootFilesystem: true,
309310
startTimeout: cdk.Duration.millis(2000),
310311
stopTimeout: cdk.Duration.millis(5000),
@@ -441,6 +442,7 @@ describe('container definition', () => {
441442
MemoryReservation: 512,
442443
Name: 'Example Container',
443444
Privileged: true,
445+
PseudoTerminal: true,
444446
ReadonlyRootFilesystem: true,
445447
ResourceRequirements: [
446448
{

Diff for: packages/@aws-cdk/aws-ecs/test/ec2/ec2-task-definition.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ describe('ec2 task definition', () => {
264264
logging: new ecs.AwsLogDriver({ streamPrefix: 'prefix' }),
265265
memoryReservationMiB: 1024,
266266
privileged: true,
267+
pseudoTerminal: true,
267268
readonlyRootFilesystem: true,
268269
secrets: {
269270
SECRET: ecs.Secret.fromSecretsManager(secret),
@@ -389,6 +390,7 @@ describe('ec2 task definition', () => {
389390
MemoryReservation: 1024,
390391
Name: 'web',
391392
Privileged: true,
393+
PseudoTerminal: true,
392394
ReadonlyRootFilesystem: true,
393395
Secrets: [
394396
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": "30.1.0",
3+
"files": {
4+
"21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": {
5+
"source": {
6+
"path": "PseudoTerminalDefaultTestDeployAssert1B88B826.template.json",
7+
"packaging": "file"
8+
},
9+
"destinations": {
10+
"current_account-current_region": {
11+
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12+
"objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json",
13+
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
14+
}
15+
}
16+
}
17+
},
18+
"dockerImages": {}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"Parameters": {
3+
"BootstrapVersion": {
4+
"Type": "AWS::SSM::Parameter::Value<String>",
5+
"Default": "/cdk-bootstrap/hnb659fds/version",
6+
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
7+
}
8+
},
9+
"Rules": {
10+
"CheckBootstrapVersion": {
11+
"Assertions": [
12+
{
13+
"Assert": {
14+
"Fn::Not": [
15+
{
16+
"Fn::Contains": [
17+
[
18+
"1",
19+
"2",
20+
"3",
21+
"4",
22+
"5"
23+
],
24+
{
25+
"Ref": "BootstrapVersion"
26+
}
27+
]
28+
}
29+
]
30+
},
31+
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
32+
}
33+
]
34+
}
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": "30.1.0",
3+
"files": {
4+
"9b1c5a27bfd1f10bf81e372c212b17c4def357247c4c40d64b7c0657fccb4943": {
5+
"source": {
6+
"path": "aws-ecs-integ-pseudo-terminal.template.json",
7+
"packaging": "file"
8+
},
9+
"destinations": {
10+
"current_account-current_region": {
11+
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12+
"objectKey": "9b1c5a27bfd1f10bf81e372c212b17c4def357247c4c40d64b7c0657fccb4943.json",
13+
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
14+
}
15+
}
16+
}
17+
},
18+
"dockerImages": {}
19+
}

0 commit comments

Comments
 (0)