Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ecs): support pidMode for FargateTaskDefinition #29670

Merged
merged 3 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@
"Family": "awsecsintegruntimeTaskDefGraviton28E28B263",
"Memory": "1024",
"NetworkMode": "awsvpc",
"PidMode": "host",
"RequiresCompatibilities": [
"FARGATE"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const taskDefinitiongraviton2 = new ecs.FargateTaskDefinition(stack, 'TaskDefGra
},
cpu: 256,
memoryLimitMiB: 1024,
pidMode: ecs.PidMode.HOST,
});

taskDefinitionwindows.addContainer('windowsservercore', {
Expand Down
17 changes: 17 additions & 0 deletions packages/aws-cdk-lib/aws-ecs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,23 @@ const fargateTaskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDef', {
});
```

To specify the process namespace to use for the containers in the task, use the `pidMode` property:

```ts
const fargateTaskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDef', {
runtimePlatform: {
operatingSystemFamily: ecs.OperatingSystemFamily.LINUX,
cpuArchitecture: ecs.CpuArchitecture.ARM64,
},
memoryLimitMiB: 512,
cpu: 256,
pidMode: ecs.PidMode.HOST,
});
```

**Note:** `pidMode` is only supported for tasks that are hosted on AWS Fargate if the tasks are using platform version 1.4.0
or later (Linux). This isn't supported for Windows containers on Fargate.

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

```ts
Expand Down
8 changes: 5 additions & 3 deletions packages/aws-cdk-lib/aws-ecs/lib/base/task-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ export interface TaskDefinitionProps extends CommonTaskDefinitionProps {
/**
* The process namespace to use for the containers in the task.
*
* Not supported in Fargate and Windows containers.
* Only supported for tasks that are hosted on AWS Fargate if the tasks
* are using platform version 1.4.0 or later (Linux).
* Not supported in Windows containers.
*
* @default - PidMode used by the task is not specified
*/
Expand All @@ -219,8 +221,8 @@ export interface TaskDefinitionProps extends CommonTaskDefinitionProps {

/**
* The operating system that your task definitions are running on.
* A runtimePlatform is supported only for tasks using the Fargate launch type.
*
* A runtimePlatform is supported only for tasks using the Fargate launch type.
*
* @default - Undefined.
*/
Expand Down Expand Up @@ -455,7 +457,7 @@ export class TaskDefinition extends TaskDefinitionBase {
this.ephemeralStorageGiB = props.ephemeralStorageGiB;

// validate the cpu and memory size for the Windows operation system family.
if (props.runtimePlatform?.operatingSystemFamily?._operatingSystemFamily.includes('WINDOWS')) {
if (props.runtimePlatform?.operatingSystemFamily?.isWindows()) {
// We know that props.cpu and props.memoryMiB are defined because an error would have been thrown previously if they were not.
// But, typescript is not able to figure this out, so using the `!` operator here to let the type-checker know they are defined.
this.checkFargateWindowsBasedTasksSize(props.cpu!, props.memoryMiB!, props.runtimePlatform!);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface Ec2TaskDefinitionProps extends CommonTaskDefinitionProps {
/**
* The process namespace to use for the containers in the task.
*
* Not supported in Fargate and Windows containers.
* Not supported in Windows containers.
*
* @default - PidMode used by the task is not specified
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Compatibility,
ITaskDefinition,
NetworkMode,
PidMode,
TaskDefinition,
} from '../base/task-definition';
import { RuntimePlatform } from '../runtime-platform';
Expand Down Expand Up @@ -77,6 +78,17 @@ export interface FargateTaskDefinitionProps extends CommonTaskDefinitionProps {
* @default - Undefined.
*/
readonly runtimePlatform?: RuntimePlatform;

/**
* The process namespace to use for the containers in the task.
*
* Only supported for tasks that are hosted on AWS Fargate if the tasks
* are using platform version 1.4.0 or later (Linux).
lpizzinidev marked this conversation as resolved.
Show resolved Hide resolved
* Not supported in Windows containers.
*
* @default - PidMode used by the task is not specified
*/
readonly pidMode?: PidMode;
}

/**
Expand Down Expand Up @@ -147,13 +159,23 @@ export class FargateTaskDefinition extends TaskDefinition implements IFargateTas
memoryMiB: props.memoryLimitMiB !== undefined ? Tokenization.stringifyNumber(props.memoryLimitMiB) : '512',
compatibility: Compatibility.FARGATE,
networkMode: NetworkMode.AWS_VPC,
pidMode: props.pidMode,
});

// eslint-disable-next-line max-len
if (props.ephemeralStorageGiB && !Token.isUnresolved(props.ephemeralStorageGiB) && (props.ephemeralStorageGiB < 21 || props.ephemeralStorageGiB > 200)) {
throw new Error('Ephemeral storage size must be between 21GiB and 200GiB');
}

if (props.pidMode) {
if (props.runtimePlatform?.operatingSystemFamily?.isWindows()) {
throw new Error('\'pidMode\' is not supported for Windows containers.');
}
if (!Token.isUnresolved(props.pidMode) && props.pidMode !== PidMode.HOST) {
throw new Error(`\'pidMode\' can only be set to \'${PidMode.HOST}\' for Fargate containers, got: \'${props.pidMode}\'.`);
}
}

this.ephemeralStorageGiB = props.ephemeralStorageGiB;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ describe('fargate task definition', () => {
cpuArchitecture: ecs.CpuArchitecture.X86_64,
operatingSystemFamily: ecs.OperatingSystemFamily.LINUX,
},
pidMode: ecs.PidMode.HOST,
});

taskDefinition.addVolume({
Expand All @@ -84,6 +85,7 @@ describe('fargate task definition', () => {
Family: 'myApp',
Memory: '1024',
NetworkMode: 'awsvpc',
PidMode: 'host',
RequiresCompatibilities: [
ecs.LaunchType.FARGATE,
],
Expand Down Expand Up @@ -161,6 +163,38 @@ describe('fargate task definition', () => {

// THEN
});

test('throws when pidMode is specified on Windows', () => {
// GIVEN
const stack = new cdk.Stack();

// WHEN
// THEN
expect(() => {
new ecs.FargateTaskDefinition(stack, 'FargateTaskDef', {
pidMode: ecs.PidMode.HOST,
runtimePlatform: {
operatingSystemFamily: ecs.OperatingSystemFamily.WINDOWS_SERVER_2019_CORE,
cpuArchitecture: ecs.CpuArchitecture.X86_64,
},
cpu: 1024,
memoryLimitMiB: 2048,
});
}).toThrow(/'pidMode' is not supported for Windows containers./);
});

test('throws when pidMode is not host', () => {
// GIVEN
const stack = new cdk.Stack();

// WHEN
// THEN
expect(() => {
new ecs.FargateTaskDefinition(stack, 'FargateTaskDef', {
pidMode: ecs.PidMode.TASK,
});
}).toThrow(/'pidMode' can only be set to 'host' for Fargate containers, got: 'task'./);
});
});
describe('When configuredAtLaunch in the Volume', ()=> {
test('do not throw when configuredAtLaunch is false', () => {
Expand Down
Loading