Skip to content

Commit

Permalink
feat(ecs): add support for Fargate PV1.4 ephemeral storage
Browse files Browse the repository at this point in the history
  • Loading branch information
otterley committed Jul 7, 2021
1 parent 71c0a4c commit bf6d38e
Show file tree
Hide file tree
Showing 6 changed files with 146 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 @@ -223,6 +223,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 - 20GiB
*/
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 < 20 || 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: 19,
});
}, /Ephemeral storage size must be between 21GiB and 200GiB/);

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

'When importing from an existing Fargate TaskDefinition': {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"Resources": {
"TaskDefTaskRole1EDB4A67": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "ecs-tasks.amazonaws.com"
}
}
],
"Version": "2012-10-17"
}
}
},
"TaskDef54694570": {
"Type": "AWS::ECS::TaskDefinition",
"Properties": {
"ContainerDefinitions": [
{
"Essential": true,
"Image": "amazon/amazon-ecs-sample",
"Name": "web"
}
],
"Cpu": "256",
"EphemeralStorage": {
"SizeInGiB": 100
},
"Family": "awsecsephemeralstorageTaskDef7E6908C5",
"Memory": "512",
"NetworkMode": "awsvpc",
"RequiresCompatibilities": [
"FARGATE"
],
"TaskRoleArn": {
"Fn::GetAtt": [
"TaskDefTaskRole1EDB4A67",
"Arn"
]
}
}
}
}
}
15 changes: 15 additions & 0 deletions packages/@aws-cdk/aws-ecs/test/fargate/integ.ephemeral-storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as cdk from '@aws-cdk/core';
import * as ecs from '../../lib';

const app = new cdk.App();
const stack = new cdk.Stack(app, 'aws-ecs-ephemeral-storage');

const taskDefinition = new ecs.FargateTaskDefinition(stack, 'TaskDef', {
ephemeralStorageGiB: 100,
});

taskDefinition.addContainer('web', {
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
});

app.synth();

0 comments on commit bf6d38e

Please sign in to comment.