Skip to content

Commit f1bf935

Browse files
authored
feat(ecs): add support for Fargate PV1.4 ephemeral storage (#15440)
Add support for ephemeral storage on Fargate PV 1.4.0 or later. Closes #14570 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent a85ad39 commit f1bf935

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

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

+11
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,17 @@ const fargateTaskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDef', {
231231
});
232232
```
233233

234+
On Fargate Platform Version 1.4.0 or later, you may specify up to 200GiB of
235+
[ephemeral storage](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/fargate-task-storage.html#fargate-task-storage-pv14):
236+
237+
```ts
238+
const fargateTaskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDef', {
239+
memoryLimitMiB: 512,
240+
cpu: 256,
241+
ephemeralStorageGiB: 100
242+
});
243+
```
244+
234245
To add containers to a task definition, call `addContainer()`:
235246

236247
```ts

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

+21
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,15 @@ export interface TaskDefinitionProps extends CommonTaskDefinitionProps {
199199
* @default - No inference accelerators.
200200
*/
201201
readonly inferenceAccelerators?: InferenceAccelerator[];
202+
203+
/**
204+
* The amount (in GiB) of ephemeral storage to be allocated to the task.
205+
*
206+
* Only supported in Fargate platform version 1.4.0 or later.
207+
*
208+
* @default - Undefined, in which case, the task will receive 20GiB ephemeral storage.
209+
*/
210+
readonly ephemeralStorageGiB?: number;
202211
}
203212

204213
/**
@@ -329,6 +338,13 @@ export class TaskDefinition extends TaskDefinitionBase {
329338
*/
330339
public readonly compatibility: Compatibility;
331340

341+
/**
342+
* The amount (in GiB) of ephemeral storage to be allocated to the task.
343+
*
344+
* Only supported in Fargate platform version 1.4.0 or later.
345+
*/
346+
public readonly ephemeralStorageGiB?: number;
347+
332348
/**
333349
* The container definitions.
334350
*/
@@ -399,6 +415,8 @@ export class TaskDefinition extends TaskDefinitionBase {
399415
props.inferenceAccelerators.forEach(ia => this.addInferenceAccelerator(ia));
400416
}
401417

418+
this.ephemeralStorageGiB = props.ephemeralStorageGiB;
419+
402420
const taskDef = new CfnTaskDefinition(this, 'Resource', {
403421
containerDefinitions: Lazy.any({ produce: () => this.renderContainers() }, { omitEmptyArray: true }),
404422
volumes: Lazy.any({ produce: () => this.renderVolumes() }, { omitEmptyArray: true }),
@@ -424,6 +442,9 @@ export class TaskDefinition extends TaskDefinitionBase {
424442
produce: () =>
425443
!isFargateCompatible(this.compatibility) ? this.renderInferenceAccelerators() : undefined,
426444
}, { omitEmptyArray: true }),
445+
ephemeralStorage: this.ephemeralStorageGiB ? {
446+
sizeInGiB: this.ephemeralStorageGiB,
447+
} : undefined,
427448
});
428449

429450
if (props.placementConstraints) {

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

+20
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ export interface FargateTaskDefinitionProps extends CommonTaskDefinitionProps {
5050
* @default 512
5151
*/
5252
readonly memoryLimitMiB?: number;
53+
54+
/**
55+
* The amount (in GiB) of ephemeral storage to be allocated to the task. The maximum supported value is 200 GiB.
56+
*
57+
* NOTE: This parameter is only supported for tasks hosted on AWS Fargate using platform version 1.4.0 or later.
58+
*
59+
* @default 20
60+
*/
61+
readonly ephemeralStorageGiB?: number;
5362
}
5463

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

116+
/**
117+
* The amount (in GiB) of ephemeral storage to be allocated to the task.
118+
*/
119+
public readonly ephemeralStorageGiB?: number;
120+
107121
/**
108122
* Constructs a new instance of the FargateTaskDefinition class.
109123
*/
@@ -115,5 +129,11 @@ export class FargateTaskDefinition extends TaskDefinition implements IFargateTas
115129
compatibility: Compatibility.FARGATE,
116130
networkMode: NetworkMode.AWS_VPC,
117131
});
132+
133+
if (props.ephemeralStorageGiB && (props.ephemeralStorageGiB < 21 || props.ephemeralStorageGiB > 200)) {
134+
throw new Error('Ephemeral storage size must be between 21GiB and 200GiB');
135+
}
136+
137+
this.ephemeralStorageGiB = props.ephemeralStorageGiB;
118138
}
119139
}

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

+30
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ nodeunitShim({
5858
taskRole: new iam.Role(stack, 'TaskRole', {
5959
assumedBy: new iam.ServicePrincipal('ecs-tasks.amazonaws.com'),
6060
}),
61+
ephemeralStorageGiB: 21,
6162
});
6263

6364
taskDefinition.addVolume({
@@ -76,6 +77,9 @@ nodeunitShim({
7677
'Arn',
7778
],
7879
},
80+
EphemeralStorage: {
81+
SizeInGiB: 21,
82+
},
7983
Family: 'myApp',
8084
Memory: '1024',
8185
NetworkMode: 'awsvpc',
@@ -131,6 +135,32 @@ nodeunitShim({
131135

132136
test.done();
133137
},
138+
139+
'throws when ephemeral storage request is too high'(test: Test) {
140+
// GIVEN
141+
const stack = new cdk.Stack();
142+
test.throws(() => {
143+
new ecs.FargateTaskDefinition(stack, 'FargateTaskDef', {
144+
ephemeralStorageGiB: 201,
145+
});
146+
}, /Ephemeral storage size must be between 21GiB and 200GiB/);
147+
148+
// THEN
149+
test.done();
150+
},
151+
152+
'throws when ephemeral storage request is too low'(test: Test) {
153+
// GIVEN
154+
const stack = new cdk.Stack();
155+
test.throws(() => {
156+
new ecs.FargateTaskDefinition(stack, 'FargateTaskDef', {
157+
ephemeralStorageGiB: 20,
158+
});
159+
}, /Ephemeral storage size must be between 21GiB and 200GiB/);
160+
161+
// THEN
162+
test.done();
163+
},
134164
},
135165

136166
'When importing from an existing Fargate TaskDefinition': {

0 commit comments

Comments
 (0)