Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ecs-patterns): allow custom ephemeral storage for ECS Fargate services #29275

Merged
merged 19 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import { App, Stack, Duration } from 'aws-cdk-lib';
import * as integ from '@aws-cdk/integ-tests-alpha';
import { ApplicationLoadBalancedFargateService } from 'aws-cdk-lib/aws-ecs-patterns';
import { ContainerImage } from 'aws-cdk-lib/aws-ecs';

const app = new App();
const stack = new Stack(app, 'aws-ecs-patterns-alb-with-custom-storage');
const vpc = new ec2.Vpc(stack, 'VPC', { restrictDefaultSecurityGroup: false });

new ApplicationLoadBalancedFargateService(stack, 'ALBServiceWithCustomStorage', {
vpc,
memoryLimitMiB: 512,
ephemeralStorageGiB: 35,
taskImageOptions: {
image: ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
},
assignPublicIp: true,
healthCheck: {
command: ['CMD-SHELL', 'curl -f http://localhost/ || exit 1'],
interval: Duration.seconds(10),
retries: 10,
},
});

new integ.IntegTest(app, 'ApplicationLoadBalancedFargateServiceCustomStorageTest', {
testCases: [stack],
});

app.synth();
29 changes: 29 additions & 0 deletions packages/aws-cdk-lib/aws-ecs-patterns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1093,4 +1093,33 @@ const scheduledFargateTask = new ecsPatterns.ScheduledFargateTask(this, 'Schedul
},
],
});
```

### Use custom ephemeral storage for ECS Fargate tasks

You can pass a custom ephemeral storage (21GiB - 200GiB) to ECS Fargate tasks on Fargate Platform Version 1.4.0 or later.

```ts
const vpc = new ec2.Vpc(stack, 'Vpc', { maxAzs: 2, restrictDefaultSecurityGroup: false });
const cluster = new ecs.Cluster(stack, 'FargateCluster', { vpc });

new ecsPatterns.ApplicationLoadBalancedFargateService(stack, 'ALBFargateServiceWithCustomEphemeralStorage', {
cluster,
memoryLimitMiB: 1024,
cpu: 512,
ephemeralStorageGiB: 21,
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
},
});

new ecsPatterns.NetworkLoadBalancedFargateService(stack, 'NLBFargateServiceWithCustomEphemeralStorage', {
cluster,
memoryLimitMiB: 1024,
cpu: 512,
ephemeralStorageGiB: 200,
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
},
});
```
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ export interface FargateServiceBaseProps {
*/
readonly memoryLimitMiB?: number;

/**
* The amount (in GiB) of ephemeral storage to be allocated to the task.
*
* The minimum supported value is `21` GiB and the maximum supported value is `200` GiB.
*
* 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;

/**
* The platform version on which to run your service.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,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
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,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
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,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
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,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
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,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,
runtimePlatform: props.runtimePlatform,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,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 All @@ -97,6 +98,9 @@ export class ScheduledFargateTask extends ScheduledTaskBase {
if (props.memoryLimitMiB) {
Annotations.of(this).addWarningV2('@aws-cdk/aws-ecs-patterns:propertyIgnored', 'Property \'memoryLimitMiB\' is ignored, use \'scheduledFargateTaskImageOptions.memoryLimitMiB\' instead.');
}
if (props.ephemeralStorageGiB) {
Annotations.of(this).addWarningV2('@aws-cdk/aws-ecs-patterns:propertyIgnored', 'Property \'ephemeralStorageGiB\' is ignored, use \'scheduledFargateTaskImageOptions.ephemeralStorageGiB\' instead.');
}
aaythapa marked this conversation as resolved.
Show resolved Hide resolved
if (props.runtimePlatform) {
Annotations.of(this).addWarningV2('@aws-cdk/aws-ecs-patterns:propertyIgnored', 'Property \'runtimePlatform\' is ignored.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ describe('Application Load Balancer', () => {
cpu: 256,
assignPublicIp: true,
memoryLimitMiB: 512,
ephemeralStorageGiB: 50,
desiredCount: 3,
enableECSManagedTags: true,
enableExecuteCommand: true,
Expand Down Expand Up @@ -349,6 +350,9 @@ describe('Application Load Balancer', () => {
},
],
Cpu: '256',
EphemeralStorage: {
SizeInGiB: 50,
},
ExecutionRoleArn: {
'Fn::GetAtt': [
'ExecutionRole605A040B',
Expand Down Expand Up @@ -847,6 +851,7 @@ describe('Network Load Balancer', () => {
cpu: 256,
assignPublicIp: true,
memoryLimitMiB: 512,
ephemeralStorageGiB: 80,
desiredCount: 3,
enableECSManagedTags: true,
enableExecuteCommand: true,
Expand Down Expand Up @@ -955,6 +960,9 @@ describe('Network Load Balancer', () => {
},
],
Cpu: '256',
EphemeralStorage: {
SizeInGiB: 80,
},
ExecutionRoleArn: {
'Fn::GetAtt': [
'ExecutionRole605A040B',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ testDeprecated('test Fargate queue worker service construct - with optional prop
new ecsPatterns.QueueProcessingFargateService(stack, 'Service', {
cluster,
memoryLimitMiB: 512,
ephemeralStorageGiB: 100,
image: ecs.ContainerImage.fromRegistry('test'),
command: ['-c', '4', 'amazon.com'],
enableLogging: false,
Expand Down Expand Up @@ -565,6 +566,9 @@ testDeprecated('test Fargate queue worker service construct - with optional prop
}),
],
Family: 'fargate-task-family',
EphemeralStorage: {
SizeInGiB: 100,
},
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ test('Can create a scheduled Fargate Task - with optional props', () => {
image: ecs.ContainerImage.fromRegistry('henk'),
memoryLimitMiB: 512,
cpu: 2,
ephemeralStorageGiB: 100,
environment: { TRIGGER: 'CloudWatch Events' },
},
desiredTaskCount: 2,
Expand Down Expand Up @@ -162,6 +163,9 @@ test('Can create a scheduled Fargate Task - with optional props', () => {
Name: 'ScheduledContainer',
},
],
EphemeralStorage: {
SizeInGiB: 100,
},
});
});

Expand Down