From 26c7ac291b473efbed8c3fd77441da5abd80d12e Mon Sep 17 00:00:00 2001 From: Xiaopeng Dai Date: Mon, 26 Feb 2024 09:49:40 -0800 Subject: [PATCH] Allow custom ephemeral storage for ECS Fargate services --- .../aws-cdk-lib/aws-ecs-patterns/README.md | 29 +++++++++++++++++++ .../lib/base/fargate-service-base.ts | 11 +++++++ ...plication-load-balanced-fargate-service.ts | 1 + ...-multiple-target-groups-fargate-service.ts | 1 + .../network-load-balanced-fargate-service.ts | 1 + ...-multiple-target-groups-fargate-service.ts | 1 + .../queue-processing-fargate-service.ts | 1 + .../lib/fargate/scheduled-fargate-task.ts | 4 +++ .../load-balanced-fargate-service-v2.test.ts | 8 +++++ .../queue-processing-fargate-service.test.ts | 4 +++ .../fargate/scheduled-fargate-task.test.ts | 4 +++ 11 files changed, 65 insertions(+) diff --git a/packages/aws-cdk-lib/aws-ecs-patterns/README.md b/packages/aws-cdk-lib/aws-ecs-patterns/README.md index ab4991275a62e..08a2ba242a377 100644 --- a/packages/aws-cdk-lib/aws-ecs-patterns/README.md +++ b/packages/aws-cdk-lib/aws-ecs-patterns/README.md @@ -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'), + }, +}); ``` \ No newline at end of file diff --git a/packages/aws-cdk-lib/aws-ecs-patterns/lib/base/fargate-service-base.ts b/packages/aws-cdk-lib/aws-ecs-patterns/lib/base/fargate-service-base.ts index 823640674fd59..d8df18c5501db 100644 --- a/packages/aws-cdk-lib/aws-ecs-patterns/lib/base/fargate-service-base.ts +++ b/packages/aws-cdk-lib/aws-ecs-patterns/lib/base/fargate-service-base.ts @@ -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. * diff --git a/packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts b/packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts index 5c101ee7dd964..4bc76a56b73dc 100644 --- a/packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts +++ b/packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts @@ -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, diff --git a/packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/application-multiple-target-groups-fargate-service.ts b/packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/application-multiple-target-groups-fargate-service.ts index df9ed2cfd79e3..aca6f639ae0ac 100644 --- a/packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/application-multiple-target-groups-fargate-service.ts +++ b/packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/application-multiple-target-groups-fargate-service.ts @@ -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, diff --git a/packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/network-load-balanced-fargate-service.ts b/packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/network-load-balanced-fargate-service.ts index a7c2c01f6d9f7..993a17a92aed2 100644 --- a/packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/network-load-balanced-fargate-service.ts +++ b/packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/network-load-balanced-fargate-service.ts @@ -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, diff --git a/packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/network-multiple-target-groups-fargate-service.ts b/packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/network-multiple-target-groups-fargate-service.ts index 21d97d8e9d5cb..9c6cc3c66649f 100644 --- a/packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/network-multiple-target-groups-fargate-service.ts +++ b/packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/network-multiple-target-groups-fargate-service.ts @@ -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, diff --git a/packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/queue-processing-fargate-service.ts b/packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/queue-processing-fargate-service.ts index f2f26d5e8669d..77d28ea2821d0 100644 --- a/packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/queue-processing-fargate-service.ts +++ b/packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/queue-processing-fargate-service.ts @@ -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, }); diff --git a/packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/scheduled-fargate-task.ts b/packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/scheduled-fargate-task.ts index c7725538ca7e2..bfd63a8fa2fc9 100644 --- a/packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/scheduled-fargate-task.ts +++ b/packages/aws-cdk-lib/aws-ecs-patterns/lib/fargate/scheduled-fargate-task.ts @@ -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, @@ -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.'); + } if (props.runtimePlatform) { Annotations.of(this).addWarningV2('@aws-cdk/aws-ecs-patterns:propertyIgnored', 'Property \'runtimePlatform\' is ignored.'); } diff --git a/packages/aws-cdk-lib/aws-ecs-patterns/test/fargate/load-balanced-fargate-service-v2.test.ts b/packages/aws-cdk-lib/aws-ecs-patterns/test/fargate/load-balanced-fargate-service-v2.test.ts index 53c34c69b5dde..b7417f4ce962f 100644 --- a/packages/aws-cdk-lib/aws-ecs-patterns/test/fargate/load-balanced-fargate-service-v2.test.ts +++ b/packages/aws-cdk-lib/aws-ecs-patterns/test/fargate/load-balanced-fargate-service-v2.test.ts @@ -225,6 +225,7 @@ describe('Application Load Balancer', () => { cpu: 256, assignPublicIp: true, memoryLimitMiB: 512, + ephemeralStorageGiB: 50, desiredCount: 3, enableECSManagedTags: true, enableExecuteCommand: true, @@ -349,6 +350,9 @@ describe('Application Load Balancer', () => { }, ], Cpu: '256', + EphemeralStorage: { + SizeInGiB: 50, + }, ExecutionRoleArn: { 'Fn::GetAtt': [ 'ExecutionRole605A040B', @@ -847,6 +851,7 @@ describe('Network Load Balancer', () => { cpu: 256, assignPublicIp: true, memoryLimitMiB: 512, + ephemeralStorageGiB: 80, desiredCount: 3, enableECSManagedTags: true, enableExecuteCommand: true, @@ -955,6 +960,9 @@ describe('Network Load Balancer', () => { }, ], Cpu: '256', + EphemeralStorage: { + SizeInGiB: 80, + }, ExecutionRoleArn: { 'Fn::GetAtt': [ 'ExecutionRole605A040B', diff --git a/packages/aws-cdk-lib/aws-ecs-patterns/test/fargate/queue-processing-fargate-service.test.ts b/packages/aws-cdk-lib/aws-ecs-patterns/test/fargate/queue-processing-fargate-service.test.ts index f69c898ba7c1a..b4290f9c15f54 100644 --- a/packages/aws-cdk-lib/aws-ecs-patterns/test/fargate/queue-processing-fargate-service.test.ts +++ b/packages/aws-cdk-lib/aws-ecs-patterns/test/fargate/queue-processing-fargate-service.test.ts @@ -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, @@ -565,6 +566,9 @@ testDeprecated('test Fargate queue worker service construct - with optional prop }), ], Family: 'fargate-task-family', + EphemeralStorage: { + SizeInGiB: 100, + }, }); }); diff --git a/packages/aws-cdk-lib/aws-ecs-patterns/test/fargate/scheduled-fargate-task.test.ts b/packages/aws-cdk-lib/aws-ecs-patterns/test/fargate/scheduled-fargate-task.test.ts index 8b318fd8b1167..3e98cac7e023e 100644 --- a/packages/aws-cdk-lib/aws-ecs-patterns/test/fargate/scheduled-fargate-task.test.ts +++ b/packages/aws-cdk-lib/aws-ecs-patterns/test/fargate/scheduled-fargate-task.test.ts @@ -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, @@ -162,6 +163,9 @@ test('Can create a scheduled Fargate Task - with optional props', () => { Name: 'ScheduledContainer', }, ], + EphemeralStorage: { + SizeInGiB: 100, + }, }); });