Skip to content

Commit

Permalink
feat(ecs): add support for Fargate PV1.4 ephemeral storage (aws#15440)
Browse files Browse the repository at this point in the history
Add support for ephemeral storage on Fargate PV 1.4.0 or later.

Closes aws#14570

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
otterley authored and david-doyle-as24 committed Sep 7, 2021
1 parent 26e1907 commit 86b571a
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/@aws-cdk/aws-ecs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,17 @@ const fargateTaskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDef', {
});
```

On Fargate Platform Version 1.4.0 or later, you may specify up to 200GiB of
[ephemeral storage](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/fargate-task-storage.html#fargate-task-storage-pv14):

```ts
const fargateTaskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDef', {
memoryLimitMiB: 512,
cpu: 256,
ephemeralStorageGiB: 100
});
```

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

```ts
Expand Down
21 changes: 21 additions & 0 deletions packages/@aws-cdk/aws-ecs/lib/base/task-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ export interface TaskDefinitionProps extends CommonTaskDefinitionProps {
* @default - No inference accelerators.
*/
readonly inferenceAccelerators?: InferenceAccelerator[];

/**
* The amount (in GiB) of ephemeral storage to be allocated to the task.
*
* Only supported in Fargate platform version 1.4.0 or later.
*
* @default - Undefined, in which case, the task will receive 20GiB ephemeral storage.
*/
readonly ephemeralStorageGiB?: number;
}

/**
Expand Down Expand Up @@ -329,6 +338,13 @@ export class TaskDefinition extends TaskDefinitionBase {
*/
public readonly compatibility: Compatibility;

/**
* The amount (in GiB) of ephemeral storage to be allocated to the task.
*
* Only supported in Fargate platform version 1.4.0 or later.
*/
public readonly ephemeralStorageGiB?: number;

/**
* The container definitions.
*/
Expand Down Expand Up @@ -399,6 +415,8 @@ export class TaskDefinition extends TaskDefinitionBase {
props.inferenceAccelerators.forEach(ia => this.addInferenceAccelerator(ia));
}

this.ephemeralStorageGiB = props.ephemeralStorageGiB;

const taskDef = new CfnTaskDefinition(this, 'Resource', {
containerDefinitions: Lazy.any({ produce: () => this.renderContainers() }, { omitEmptyArray: true }),
volumes: Lazy.any({ produce: () => this.renderVolumes() }, { omitEmptyArray: true }),
Expand All @@ -424,6 +442,9 @@ export class TaskDefinition extends TaskDefinitionBase {
produce: () =>
!isFargateCompatible(this.compatibility) ? this.renderInferenceAccelerators() : undefined,
}, { omitEmptyArray: true }),
ephemeralStorage: this.ephemeralStorageGiB ? {
sizeInGiB: this.ephemeralStorageGiB,
} : undefined,
});

if (props.placementConstraints) {
Expand Down
20 changes: 20 additions & 0 deletions packages/@aws-cdk/aws-ecs/lib/fargate/fargate-task-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ export interface FargateTaskDefinitionProps extends CommonTaskDefinitionProps {
* @default 512
*/
readonly memoryLimitMiB?: number;

/**
* The amount (in GiB) of ephemeral storage to be allocated to the task. The maximum supported value is 200 GiB.
*
* NOTE: This parameter is only supported for tasks hosted on AWS Fargate using platform version 1.4.0 or later.
*
* @default 20
*/
readonly ephemeralStorageGiB?: number;
}

/**
Expand Down Expand Up @@ -104,6 +113,11 @@ export class FargateTaskDefinition extends TaskDefinition implements IFargateTas
// we need to explicitly write the type here, as type deduction for enums won't lead to
// the import being generated in the .d.ts file.

/**
* The amount (in GiB) of ephemeral storage to be allocated to the task.
*/
public readonly ephemeralStorageGiB?: number;

/**
* Constructs a new instance of the FargateTaskDefinition class.
*/
Expand All @@ -115,5 +129,11 @@ export class FargateTaskDefinition extends TaskDefinition implements IFargateTas
compatibility: Compatibility.FARGATE,
networkMode: NetworkMode.AWS_VPC,
});

if (props.ephemeralStorageGiB && (props.ephemeralStorageGiB < 21 || props.ephemeralStorageGiB > 200)) {
throw new Error('Ephemeral storage size must be between 21GiB and 200GiB');
}

this.ephemeralStorageGiB = props.ephemeralStorageGiB;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ nodeunitShim({
taskRole: new iam.Role(stack, 'TaskRole', {
assumedBy: new iam.ServicePrincipal('ecs-tasks.amazonaws.com'),
}),
ephemeralStorageGiB: 21,
});

taskDefinition.addVolume({
Expand All @@ -76,6 +77,9 @@ nodeunitShim({
'Arn',
],
},
EphemeralStorage: {
SizeInGiB: 21,
},
Family: 'myApp',
Memory: '1024',
NetworkMode: 'awsvpc',
Expand Down Expand Up @@ -131,6 +135,32 @@ nodeunitShim({

test.done();
},

'throws when ephemeral storage request is too high'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
test.throws(() => {
new ecs.FargateTaskDefinition(stack, 'FargateTaskDef', {
ephemeralStorageGiB: 201,
});
}, /Ephemeral storage size must be between 21GiB and 200GiB/);

// THEN
test.done();
},

'throws when ephemeral storage request is too low'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
test.throws(() => {
new ecs.FargateTaskDefinition(stack, 'FargateTaskDef', {
ephemeralStorageGiB: 20,
});
}, /Ephemeral storage size must be between 21GiB and 200GiB/);

// THEN
test.done();
},
},

'When importing from an existing Fargate TaskDefinition': {
Expand Down

0 comments on commit 86b571a

Please sign in to comment.