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 ef12609 commit ea7b87e
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 5 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();
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@
npmlog "^4.1.2"
upath "^2.0.1"

"@lerna/project@4.0.0":
"@lerna/project@4.0.0", "@lerna/project@^4.0.0":
version "4.0.0"
resolved "https://registry.yarnpkg.com/@lerna/project/-/project-4.0.0.tgz#ff84893935833533a74deff30c0e64ddb7f0ba6b"
integrity sha512-o0MlVbDkD5qRPkFKlBZsXZjoNTWPyuL58564nSfZJ6JYNmgAptnWPB2dQlAc7HWRZkmnC2fCkEdoU+jioPavbg==
Expand Down Expand Up @@ -2999,7 +2999,7 @@ conventional-changelog-codemirror@^2.0.8:
dependencies:
q "^1.5.1"

conventional-changelog-config-spec@2.1.0:
conventional-changelog-config-spec@2.1.0, conventional-changelog-config-spec@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/conventional-changelog-config-spec/-/conventional-changelog-config-spec-2.1.0.tgz#874a635287ef8b581fd8558532bf655d4fb59f2d"
integrity sha512-IpVePh16EbbB02V+UA+HQnnPIohgXvJRxHcS5+Uwk4AT5LjzCZJm5sp/yqs5C6KZJ1jMsV4paEV13BN1pvDuxQ==
Expand Down Expand Up @@ -3084,7 +3084,7 @@ conventional-changelog-preset-loader@^2.3.4:
resolved "https://registry.yarnpkg.com/conventional-changelog-preset-loader/-/conventional-changelog-preset-loader-2.3.4.tgz#14a855abbffd59027fd602581f1f34d9862ea44c"
integrity sha512-GEKRWkrSAZeTq5+YjUZOYxdHq+ci4dNwHvpaBC3+ENalzFWuCWa9EZXSuZBpkr72sMdKB+1fyDV4takK1Lf58g==

conventional-changelog-writer@^4.0.18:
conventional-changelog-writer@^4.0.18, conventional-changelog-writer@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/conventional-changelog-writer/-/conventional-changelog-writer-4.1.0.tgz#1ca7880b75aa28695ad33312a1f2366f4b12659f"
integrity sha512-WwKcUp7WyXYGQmkLsX4QmU42AZ1lqlvRW9mqoyiQzdD+rJWbTepdWoKJuwXTS+yq79XKnQNa93/roViPQrAQgw==
Expand Down Expand Up @@ -3505,7 +3505,7 @@ detect-indent@^5.0.0:
resolved "https://registry.yarnpkg.com/detect-indent/-/detect-indent-5.0.0.tgz#3871cc0a6a002e8c3e5b3cf7f336264675f06b9d"
integrity sha1-OHHMCmoALow+Wzz38zYmRnXwa50=

detect-indent@^6.0.0:
detect-indent@^6.0.0, detect-indent@^6.1.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/detect-indent/-/detect-indent-6.1.0.tgz#592485ebbbf6b3b1ab2be175c8393d04ca0d57e6"
integrity sha512-reYkTUJAZb9gUuZ2RvVCNhVHdg62RHnJ7WJl8ftMi4diZ6NWlciOzQN88pUhSELEwflJht4oQDv0F0BMlwaYtA==
Expand Down Expand Up @@ -4643,7 +4643,7 @@ getpass@^0.1.1:
dependencies:
assert-plus "^1.0.0"

git-raw-commits@^2.0.8:
git-raw-commits@^2.0.10, git-raw-commits@^2.0.8:
version "2.0.10"
resolved "https://registry.yarnpkg.com/git-raw-commits/-/git-raw-commits-2.0.10.tgz#e2255ed9563b1c9c3ea6bd05806410290297bbc1"
integrity sha512-sHhX5lsbG9SOO6yXdlwgEMQ/ljIn7qMpAbJZCGfXX2fq5T8M5SrDnpYk9/4HswTildcIqatsWa91vty6VhWSaQ==
Expand Down

0 comments on commit ea7b87e

Please sign in to comment.