Skip to content

Commit 3e9e0a8

Browse files
fix(ecs): require task pidMode for Linux-based Fargate tasks, not host (#30020)
### Issue # (if applicable) Closes #29995. ### Reason for this change Only the `task` option is allowed for [`pidMode`](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecs-taskdefinition.html#cfn-ecs-taskdefinition-pidmode) on Linux-based Fargate tasks. ### Description of changes This PR builds on the changes introduced in #29670 but fixes the handling of `pidMode` so that it matches the behavior allowed by CloudFormation and described in the [AWS User Guide](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecs-taskdefinition.html#cfn-ecs-taskdefinition-pidmode). ### Description of how you validated changes Updated the existing tests so that `task` is the only allowable `pidMode` setting if a Fargate task's OS is Linux-based. ### 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 a96cf55 commit 3e9e0a8

File tree

8 files changed

+63
-21
lines changed

8 files changed

+63
-21
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/fargate/integ.runtime.js.snapshot/aws-ecs-integ-runtime.template.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@
570570
"Family": "awsecsintegruntimeTaskDefGraviton28E28B263",
571571
"Memory": "1024",
572572
"NetworkMode": "awsvpc",
573-
"PidMode": "host",
573+
"PidMode": "task",
574574
"RequiresCompatibilities": [
575575
"FARGATE"
576576
],

packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/fargate/integ.runtime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const taskDefinitiongraviton2 = new ecs.FargateTaskDefinition(stack, 'TaskDefGra
2727
},
2828
cpu: 256,
2929
memoryLimitMiB: 1024,
30-
pidMode: ecs.PidMode.HOST,
30+
pidMode: ecs.PidMode.TASK,
3131
});
3232

3333
taskDefinitionwindows.addContainer('windowsservercore', {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,13 @@ const fargateTaskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDef', {
372372
},
373373
memoryLimitMiB: 512,
374374
cpu: 256,
375-
pidMode: ecs.PidMode.HOST,
375+
pidMode: ecs.PidMode.TASK,
376376
});
377377
```
378378

379379
**Note:** `pidMode` is only supported for tasks that are hosted on AWS Fargate if the tasks are using platform version 1.4.0
380-
or later (Linux). This isn't supported for Windows containers on Fargate.
380+
or later (Linux). Only the `task` option is supported for Linux containers. `pidMode` isn't supported for Windows containers on Fargate.
381+
If `pidMode` is specified for a Fargate task, then `runtimePlatform.operatingSystemFamily` must also be specified.
381382

382383
To add containers to a task definition, call `addContainer()`:
383384

packages/aws-cdk-lib/aws-ecs/lib/base/task-definition.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,11 @@ export interface TaskDefinitionProps extends CommonTaskDefinitionProps {
194194
* The process namespace to use for the containers in the task.
195195
*
196196
* Only supported for tasks that are hosted on AWS Fargate if the tasks
197-
* are using platform version 1.4.0 or later (Linux).
198-
* Not supported in Windows containers.
197+
* are using platform version 1.4.0 or later (Linux). Only the TASK option
198+
* is supported for Linux-based Fargate containers. Not supported in Windows
199+
* containers. If pidMode is specified for a Fargate task, then
200+
* runtimePlatform.operatingSystemFamily must also be specified. For more
201+
* information, see [Task Definition Parameters](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#task_definition_pidmode).
199202
*
200203
* @default - PidMode used by the task is not specified
201204
*/
@@ -378,8 +381,10 @@ export class TaskDefinition extends TaskDefinitionBase {
378381
* The process namespace to use for the containers in the task.
379382
*
380383
* Only supported for tasks that are hosted on AWS Fargate if the tasks
381-
* are using platform version 1.4.0 or later (Linux).
382-
* Not supported in Windows containers.
384+
* are using platform version 1.4.0 or later (Linux). Not supported in
385+
* Windows containers. If pidMode is specified for a Fargate task,
386+
* then runtimePlatform.operatingSystemFamily must also be specified. For more
387+
* information, see [Task Definition Parameters](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#task_definition_pidmode).
383388
*/
384389
public readonly pidMode?: PidMode;
385390

packages/aws-cdk-lib/aws-ecs/lib/fargate/fargate-task-definition.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,11 @@ export interface FargateTaskDefinitionProps extends CommonTaskDefinitionProps {
8383
* The process namespace to use for the containers in the task.
8484
*
8585
* Only supported for tasks that are hosted on AWS Fargate if the tasks
86-
* are using platform version 1.4.0 or later (Linux).
87-
* Not supported in Windows containers.
86+
* are using platform version 1.4.0 or later (Linux). Only the TASK option
87+
* is supported for Linux-based Fargate containers. Not supported in
88+
* Windows containers. If pidMode is specified for a Fargate task, then
89+
* runtimePlatform.operatingSystemFamily must also be specified. For more
90+
* information, see [Task Definition Parameters](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#task_definition_pidmode).
8891
*
8992
* @default - PidMode used by the task is not specified
9093
*/
@@ -168,11 +171,16 @@ export class FargateTaskDefinition extends TaskDefinition implements IFargateTas
168171
}
169172

170173
if (props.pidMode) {
174+
if (!props.runtimePlatform?.operatingSystemFamily) {
175+
throw new Error('Specifying \'pidMode\' requires that operating system family also be provided.');
176+
}
171177
if (props.runtimePlatform?.operatingSystemFamily?.isWindows()) {
172178
throw new Error('\'pidMode\' is not supported for Windows containers.');
173179
}
174-
if (!Token.isUnresolved(props.pidMode) && props.pidMode !== PidMode.HOST) {
175-
throw new Error(`\'pidMode\' can only be set to \'${PidMode.HOST}\' for Fargate containers, got: \'${props.pidMode}\'.`);
180+
if (!Token.isUnresolved(props.pidMode)
181+
&& props.runtimePlatform?.operatingSystemFamily?.isLinux()
182+
&& props.pidMode !== PidMode.TASK) {
183+
throw new Error(`\'pidMode\' can only be set to \'${PidMode.TASK}\' for Linux Fargate containers, got: \'${props.pidMode}\'.`);
176184
}
177185
}
178186

packages/aws-cdk-lib/aws-ecs/lib/runtime-platform.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,17 @@ export class OperatingSystemFamily {
9090
private constructor(public readonly _operatingSystemFamily: string) { }
9191

9292
/**
93-
* Returns true if the operating system family is Windows
93+
* Indicates whether the operating system family is Windows
9494
*/
9595
public isWindows(): boolean {
96-
return this._operatingSystemFamily?.toLowerCase().startsWith('windows') ? true : false;
96+
return this._operatingSystemFamily?.toLowerCase().startsWith('windows');
97+
}
98+
99+
/**
100+
* Indicates whether the operating system family is Linux
101+
*/
102+
public isLinux(): boolean {
103+
return this._operatingSystemFamily?.toLowerCase().startsWith('linux');
97104
}
98105
}
99106

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ describe('fargate service', () => {
758758
},
759759
memoryLimitMiB: 512,
760760
cpu: 256,
761-
pidMode: ecs.PidMode.HOST,
761+
pidMode: ecs.PidMode.TASK,
762762
});
763763

764764
// WHEN

packages/aws-cdk-lib/aws-ecs/test/fargate/fargate-task-definition.test.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe('fargate task definition', () => {
6060
cpuArchitecture: ecs.CpuArchitecture.X86_64,
6161
operatingSystemFamily: ecs.OperatingSystemFamily.LINUX,
6262
},
63-
pidMode: ecs.PidMode.HOST,
63+
pidMode: ecs.PidMode.TASK,
6464
});
6565

6666
taskDefinition.addVolume({
@@ -85,7 +85,7 @@ describe('fargate task definition', () => {
8585
Family: 'myApp',
8686
Memory: '1024',
8787
NetworkMode: 'awsvpc',
88-
PidMode: 'host',
88+
PidMode: 'task',
8989
RequiresCompatibilities: [
9090
ecs.LaunchType.FARGATE,
9191
],
@@ -164,6 +164,24 @@ describe('fargate task definition', () => {
164164
// THEN
165165
});
166166

167+
test('throws when pidMode is specified without an operating system family', () => {
168+
// GIVEN
169+
const stack = new cdk.Stack();
170+
171+
// WHEN
172+
// THEN
173+
expect(() => {
174+
new ecs.FargateTaskDefinition(stack, 'FargateTaskDef', {
175+
pidMode: ecs.PidMode.TASK,
176+
runtimePlatform: {
177+
cpuArchitecture: ecs.CpuArchitecture.X86_64,
178+
},
179+
cpu: 1024,
180+
memoryLimitMiB: 2048,
181+
});
182+
}).toThrow(/Specifying 'pidMode' requires that operating system family also be provided./);
183+
});
184+
167185
test('throws when pidMode is specified on Windows', () => {
168186
// GIVEN
169187
const stack = new cdk.Stack();
@@ -172,7 +190,7 @@ describe('fargate task definition', () => {
172190
// THEN
173191
expect(() => {
174192
new ecs.FargateTaskDefinition(stack, 'FargateTaskDef', {
175-
pidMode: ecs.PidMode.HOST,
193+
pidMode: ecs.PidMode.TASK,
176194
runtimePlatform: {
177195
operatingSystemFamily: ecs.OperatingSystemFamily.WINDOWS_SERVER_2019_CORE,
178196
cpuArchitecture: ecs.CpuArchitecture.X86_64,
@@ -183,17 +201,20 @@ describe('fargate task definition', () => {
183201
}).toThrow(/'pidMode' is not supported for Windows containers./);
184202
});
185203

186-
test('throws when pidMode is not host', () => {
204+
test('throws when pidMode is not task', () => {
187205
// GIVEN
188206
const stack = new cdk.Stack();
189207

190208
// WHEN
191209
// THEN
192210
expect(() => {
193211
new ecs.FargateTaskDefinition(stack, 'FargateTaskDef', {
194-
pidMode: ecs.PidMode.TASK,
212+
pidMode: ecs.PidMode.HOST,
213+
runtimePlatform: {
214+
operatingSystemFamily: ecs.OperatingSystemFamily.LINUX,
215+
},
195216
});
196-
}).toThrow(/'pidMode' can only be set to 'host' for Fargate containers, got: 'task'./);
217+
}).toThrow(/'pidMode' can only be set to 'task' for Linux Fargate containers, got: 'host'./);
197218
});
198219
});
199220
describe('When configuredAtLaunch in the Volume', ()=> {

0 commit comments

Comments
 (0)