Skip to content

Commit

Permalink
Add ephemeral storage support to aws-ecs-patterns
Browse files Browse the repository at this point in the history
Fargate supports a new ephemeral storage option, to request up to 200
GiB of task storage. Support for it was added to aws-ecs but not to
aws-ecs-patterns.

This change adds support for ephemeral storage, so the following
patterns can take advantage of it:

* Application load balanced fargate service
* Application multiple target groups fargate service
* Network load balanced fargate service
* Network multiple target groups fargate service
* Queue processing fargate service
* Scheduled fargate task

Closes aws#18105

Signed-off-by: Eduardo Lopez <252504+tapichu@users.noreply.github.com>
  • Loading branch information
tapichu committed Dec 20, 2021
1 parent 32f1c80 commit c6e4964
Show file tree
Hide file tree
Showing 17 changed files with 2,483 additions and 0 deletions.
140 changes: 140 additions & 0 deletions packages/@aws-cdk/aws-ecs-patterns/README.md
Expand Up @@ -76,6 +76,25 @@ If you need to encrypt the traffic between the load balancer and the ECS tasks,

Additionally, if more than one application target group are needed, instantiate one of the following:

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
declare const cluster: ecs.Cluster;
const loadBalancedFargateService = new ecsPatterns.ApplicationLoadBalancedFargateService(this, 'Service', {
cluster,
memoryLimitMiB: 1024,
cpu: 512,
ephemeralStorageGiB: 100,
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
},
});

loadBalancedFargateService.targetGroup.configureHealthCheck({
path: "/custom-health-path",
});
```

* `ApplicationMultipleTargetGroupsEc2Service`

```ts
Expand Down Expand Up @@ -125,6 +144,32 @@ const loadBalancedFargateService = new ecsPatterns.ApplicationMultipleTargetGrou
});
```

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
// One application load balancer with one listener and two target groups.
declare const cluster: ecs.Cluster;
const loadBalancedFargateService = new ecsPatterns.ApplicationMultipleTargetGroupsFargateService(this, 'Service', {
cluster,
memoryLimitMiB: 1024,
cpu: 512,
ephemeralStorageGiB: 100,
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
},
targetGroups: [
{
containerPort: 80,
},
{
containerPort: 90,
pathPattern: 'a/b/c',
priority: 10,
},
],
});
```

## Network Load Balanced Services

To define an Amazon ECS service that is behind a network load balancer, instantiate one of the following:
Expand Down Expand Up @@ -169,6 +214,21 @@ If you specify the option `recordType` you can decide if you want the construct

Additionally, if more than one network target group is needed, instantiate one of the following:

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
declare const cluster: ecs.Cluster;
const loadBalancedFargateService = new ecsPatterns.NetworkLoadBalancedFargateService(this, 'Service', {
cluster,
memoryLimitMiB: 1024,
cpu: 512,
ephemeralStorageGiB: 100,
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
},
});
```

* NetworkMultipleTargetGroupsEc2Service

```ts
Expand Down Expand Up @@ -253,6 +313,49 @@ const loadBalancedFargateService = new ecsPatterns.NetworkMultipleTargetGroupsFa
});
```

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
// Two network load balancers, each with their own listener and target group.
declare const cluster: ecs.Cluster;
const loadBalancedFargateService = new ecsPatterns.NetworkMultipleTargetGroupsFargateService(this, 'Service', {
cluster,
memoryLimitMiB: 512,
ephemeralStorageGiB: 100,
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
},
loadBalancers: [
{
name: 'lb1',
listeners: [
{
name: 'listener1',
},
],
},
{
name: 'lb2',
listeners: [
{
name: 'listener2',
},
],
},
],
targetGroups: [
{
containerPort: 80,
listener: 'listener1',
},
{
containerPort: 90,
listener: 'listener2',
},
],
});
```

## Queue Processing Services

To define a service that creates a queue and reads from that queue, instantiate one of the following:
Expand Down Expand Up @@ -299,6 +402,27 @@ const queueProcessingFargateService = new ecsPatterns.QueueProcessingFargateServ

when queue not provided by user, CDK will create a primary queue and a dead letter queue with default redrive policy and attach permission to the task to be able to access the primary queue.

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
declare const cluster: ecs.Cluster;
const queueProcessingFargateService = new ecsPatterns.QueueProcessingFargateService(this, 'Service', {
cluster,
memoryLimitMiB: 512,
ephemeralStorageGiB: 100,
image: ecs.ContainerImage.fromRegistry('test'),
command: ["-c", "4", "amazon.com"],
enableLogging: false,
desiredTaskCount: 2,
environment: {
TEST_ENVIRONMENT_VARIABLE1: "test environment variable 1 value",
TEST_ENVIRONMENT_VARIABLE2: "test environment variable 2 value",
},
maxScalingCapacity: 5,
containerName: 'test',
});
```

## Scheduled Tasks

To define a task that runs periodically, there are 2 options:
Expand Down Expand Up @@ -336,6 +460,22 @@ const scheduledFargateTask = new ecsPatterns.ScheduledFargateTask(this, 'Schedul
});
```

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
declare const cluster: ecs.Cluster;
const scheduledFargateTask = new ecsPatterns.ScheduledFargateTask(this, 'ScheduledFargateTask', {
cluster,
scheduledFargateTaskImageOptions: {
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
memoryLimitMiB: 512,
ephemeralStorageGiB: 100,
},
schedule: appscaling.Schedule.expression('rate(1 minute)'),
platformVersion: ecs.FargatePlatformVersion.LATEST,
});
```

## Additional Examples

In addition to using the constructs, users can also add logic to customize these constructs:
Expand Down
Expand Up @@ -60,6 +60,17 @@ export interface ApplicationLoadBalancedFargateServiceProps extends ApplicationL
*/
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.
*
* This default is set in the underlying FargateTaskDefinition construct.
*
* @default 20
*/
readonly ephemeralStorageGiB?: number;

/**
* Determines whether the service will be assigned a public IP address.
*
Expand Down Expand Up @@ -129,6 +140,7 @@ export class ApplicationLoadBalancedFargateService extends ApplicationLoadBalanc
this.taskDefinition = new FargateTaskDefinition(this, 'TaskDef', {
memoryLimitMiB: props.memoryLimitMiB,
cpu: props.cpu,
ephemeralStorageGiB: props.ephemeralStorageGiB,
executionRole: taskImageOptions.executionRole,
taskRole: taskImageOptions.taskRole,
family: taskImageOptions.family,
Expand Down
Expand Up @@ -64,6 +64,17 @@ export interface ApplicationMultipleTargetGroupsFargateServiceProps extends Appl
*/
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.
*
* This default is set in the underlying FargateTaskDefinition construct.
*
* @default 20
*/
readonly ephemeralStorageGiB?: number;

/**
* Determines whether the service will be assigned a public IP address.
*
Expand Down Expand Up @@ -125,6 +136,7 @@ export class ApplicationMultipleTargetGroupsFargateService extends ApplicationMu
this.taskDefinition = new FargateTaskDefinition(this, 'TaskDef', {
memoryLimitMiB: props.memoryLimitMiB,
cpu: props.cpu,
ephemeralStorageGiB: props.ephemeralStorageGiB,
executionRole: taskImageOptions.executionRole,
taskRole: taskImageOptions.taskRole,
family: taskImageOptions.family,
Expand Down
Expand Up @@ -60,6 +60,17 @@ export interface NetworkLoadBalancedFargateServiceProps extends NetworkLoadBalan
*/
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.
*
* This default is set in the underlying FargateTaskDefinition construct.
*
* @default 20
*/
readonly ephemeralStorageGiB?: number;

/**
* Determines whether the service will be assigned a public IP address.
*
Expand Down Expand Up @@ -118,6 +129,7 @@ export class NetworkLoadBalancedFargateService extends NetworkLoadBalancedServic
this.taskDefinition = new FargateTaskDefinition(this, 'TaskDef', {
memoryLimitMiB: props.memoryLimitMiB,
cpu: props.cpu,
ephemeralStorageGiB: props.ephemeralStorageGiB,
executionRole: taskImageOptions.executionRole,
taskRole: taskImageOptions.taskRole,
family: taskImageOptions.family,
Expand Down
Expand Up @@ -64,6 +64,17 @@ export interface NetworkMultipleTargetGroupsFargateServiceProps extends NetworkM
*/
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.
*
* This default is set in the underlying FargateTaskDefinition construct.
*
* @default 20
*/
readonly ephemeralStorageGiB?: number;

/**
* Determines whether the service will be assigned a public IP address.
*
Expand Down Expand Up @@ -125,6 +136,7 @@ export class NetworkMultipleTargetGroupsFargateService extends NetworkMultipleTa
this.taskDefinition = new FargateTaskDefinition(this, 'TaskDef', {
memoryLimitMiB: props.memoryLimitMiB,
cpu: props.cpu,
ephemeralStorageGiB: props.ephemeralStorageGiB,
executionRole: taskImageOptions.executionRole,
taskRole: taskImageOptions.taskRole,
family: taskImageOptions.family,
Expand Down
Expand Up @@ -51,6 +51,17 @@ export interface QueueProcessingFargateServiceProps extends QueueProcessingServi
*/
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.
*
* This default is set in the underlying FargateTaskDefinition construct.
*
* @default 20
*/
readonly ephemeralStorageGiB?: number;

/**
* The platform version on which to run your service.
*
Expand Down Expand Up @@ -116,6 +127,7 @@ export class QueueProcessingFargateService extends QueueProcessingServiceBase {
this.taskDefinition = new FargateTaskDefinition(this, 'QueueProcessingTaskDef', {
memoryLimitMiB: props.memoryLimitMiB || 512,
cpu: props.cpu || 256,
ephemeralStorageGiB: props.ephemeralStorageGiB,
family: props.family,
});

Expand Down
Expand Up @@ -69,6 +69,17 @@ export interface ScheduledFargateTaskImageOptions extends ScheduledTaskImageProp
* @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.
*
* This default is set in the underlying FargateTaskDefinition construct.
*
* @default 20
*/
readonly ephemeralStorageGiB?: number;
}

/**
Expand Down Expand Up @@ -114,6 +125,7 @@ export class ScheduledFargateTask extends ScheduledTaskBase {
this.taskDefinition = new FargateTaskDefinition(this, 'ScheduledTaskDef', {
memoryLimitMiB: taskImageOptions.memoryLimitMiB || 512,
cpu: taskImageOptions.cpu || 256,
ephemeralStorageGiB: taskImageOptions.ephemeralStorageGiB,
});
this.taskDefinition.addContainer('ScheduledContainer', {
image: taskImageOptions.image,
Expand Down

0 comments on commit c6e4964

Please sign in to comment.