Skip to content

Commit a8768f4

Browse files
feat(batch): ephemeralStorage property on job definitions (#25399)
Closes #25393. Adds missing `ephemeralStorage` property to `EcsFargateContainerDefinition` and `EcsFargateContainerDefinitionProps` along with a unit test. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent ad89f01 commit a8768f4

File tree

7 files changed

+225
-119
lines changed

7 files changed

+225
-119
lines changed

packages/@aws-cdk/aws-batch-alpha/lib/ecs-container-definition.ts

+28
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,13 @@ export interface IEcsFargateContainerDefinition extends IEcsContainerDefinition
880880
* @default LATEST
881881
*/
882882
readonly fargatePlatformVersion?: ecs.FargatePlatformVersion;
883+
884+
/**
885+
* The size for ephemeral storage.
886+
*
887+
* @default - 20 GiB
888+
*/
889+
readonly ephemeralStorageSize?: Size;
883890
}
884891

885892
/**
@@ -903,6 +910,13 @@ export interface EcsFargateContainerDefinitionProps extends EcsContainerDefiniti
903910
* @default LATEST
904911
*/
905912
readonly fargatePlatformVersion?: ecs.FargatePlatformVersion;
913+
914+
/**
915+
* The size for ephemeral storage.
916+
*
917+
* @default - 20 GiB
918+
*/
919+
readonly ephemeralStorageSize?: Size;
906920
}
907921

908922
/**
@@ -911,11 +925,22 @@ export interface EcsFargateContainerDefinitionProps extends EcsContainerDefiniti
911925
export class EcsFargateContainerDefinition extends EcsContainerDefinitionBase implements IEcsFargateContainerDefinition {
912926
public readonly fargatePlatformVersion?: ecs.FargatePlatformVersion;
913927
public readonly assignPublicIp?: boolean;
928+
public readonly ephemeralStorageSize?: Size;
914929

915930
constructor(scope: Construct, id: string, props: EcsFargateContainerDefinitionProps) {
916931
super(scope, id, props);
917932
this.assignPublicIp = props.assignPublicIp;
918933
this.fargatePlatformVersion = props.fargatePlatformVersion;
934+
this.ephemeralStorageSize = props.ephemeralStorageSize;
935+
936+
// validates ephemeralStorageSize is within limits
937+
if (props.ephemeralStorageSize) {
938+
if (props.ephemeralStorageSize.toGibibytes() > 200) {
939+
throw new Error(`ECS Fargate container '${id}' specifies 'ephemeralStorageSize' at ${props.ephemeralStorageSize.toGibibytes()} > 200 GB`);
940+
} else if (props.ephemeralStorageSize.toGibibytes() < 21) {
941+
throw new Error(`ECS Fargate container '${id}' specifies 'ephemeralStorageSize' at ${props.ephemeralStorageSize.toGibibytes()} < 21 GB`);
942+
}
943+
}
919944
}
920945

921946
/**
@@ -924,6 +949,9 @@ export class EcsFargateContainerDefinition extends EcsContainerDefinitionBase im
924949
public _renderContainerDefinition(): CfnJobDefinition.ContainerPropertiesProperty {
925950
return {
926951
...super._renderContainerDefinition(),
952+
ephemeralStorage: this.ephemeralStorageSize? {
953+
sizeInGiB: this.ephemeralStorageSize?.toGibibytes(),
954+
} : undefined,
927955
fargatePlatformConfiguration: {
928956
platformVersion: this.fargatePlatformVersion?.toString(),
929957
},

packages/@aws-cdk/aws-batch-alpha/test/ecs-container-definition.test.ts

+71
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ArnPrincipal, Role } from 'aws-cdk-lib/aws-iam';
99
import * as logs from 'aws-cdk-lib/aws-logs';
1010
import { Secret } from 'aws-cdk-lib/aws-secretsmanager';
1111
import { Size, Stack } from 'aws-cdk-lib';
12+
import * as cdk from 'aws-cdk-lib';
1213
import { EcsContainerDefinitionProps, EcsEc2ContainerDefinition, EcsFargateContainerDefinition, EcsJobDefinition, EcsVolume, IEcsEc2ContainerDefinition, LinuxParameters, UlimitName } from '../lib';
1314
import { CfnJobDefinitionProps } from 'aws-cdk-lib/aws-batch';
1415
import { capitalizePropertyNames } from './utils';
@@ -792,4 +793,74 @@ describe('Fargate containers', () => {
792793
},
793794
});
794795
});
796+
797+
test('can set ephemeralStorageSize', () => {
798+
// WHEN
799+
new EcsJobDefinition(stack, 'ECSJobDefn', {
800+
container: new EcsFargateContainerDefinition(stack, 'EcsFargateContainer', {
801+
...defaultContainerProps,
802+
fargatePlatformVersion: ecs.FargatePlatformVersion.LATEST,
803+
ephemeralStorageSize: Size.gibibytes(100),
804+
}),
805+
});
806+
807+
// THEN
808+
Template.fromStack(stack).hasResourceProperties('AWS::Batch::JobDefinition', {
809+
...pascalCaseExpectedProps,
810+
ContainerProperties: {
811+
...pascalCaseExpectedProps.ContainerProperties,
812+
ExecutionRoleArn: {
813+
'Fn::GetAtt': ['EcsFargateContainerExecutionRole3286EAFE', 'Arn'],
814+
},
815+
EphemeralStorage: {
816+
SizeInGiB: Size.gibibytes(100).toGibibytes(),
817+
},
818+
},
819+
});
820+
});
821+
822+
test('can set ephemeralStorageSize as token', () => {
823+
const ephemeralStorageValue: number = cdk.Token.asNumber(150);
824+
825+
// WHEN
826+
new EcsJobDefinition(stack, 'ECSJobDefn', {
827+
container: new EcsFargateContainerDefinition(stack, 'EcsFargateContainer', {
828+
...defaultContainerProps,
829+
fargatePlatformVersion: ecs.FargatePlatformVersion.LATEST,
830+
ephemeralStorageSize: Size.gibibytes(ephemeralStorageValue),
831+
}),
832+
});
833+
834+
// THEN
835+
Template.fromStack(stack).hasResourceProperties('AWS::Batch::JobDefinition', {
836+
...pascalCaseExpectedProps,
837+
ContainerProperties: {
838+
...pascalCaseExpectedProps.ContainerProperties,
839+
ExecutionRoleArn: {
840+
'Fn::GetAtt': ['EcsFargateContainerExecutionRole3286EAFE', 'Arn'],
841+
},
842+
EphemeralStorage: {
843+
SizeInGiB: Size.gibibytes(150).toGibibytes(),
844+
},
845+
},
846+
});
847+
});
848+
849+
test('ephemeralStorageSize throws error when out of range', () => {
850+
expect(() => new EcsJobDefinition(stack, 'ECSJobDefn', {
851+
container: new EcsFargateContainerDefinition(stack, 'EcsFargateContainer', {
852+
...defaultContainerProps,
853+
fargatePlatformVersion: ecs.FargatePlatformVersion.LATEST,
854+
ephemeralStorageSize: Size.gibibytes(19),
855+
}),
856+
})).toThrow("ECS Fargate container 'EcsFargateContainer' specifies 'ephemeralStorageSize' at 19 < 21 GB");
857+
858+
expect(() => new EcsJobDefinition(stack, 'ECSJobDefn2', {
859+
container: new EcsFargateContainerDefinition(stack, 'EcsFargateContainer2', {
860+
...defaultContainerProps,
861+
fargatePlatformVersion: ecs.FargatePlatformVersion.LATEST,
862+
ephemeralStorageSize: Size.gibibytes(201),
863+
}),
864+
})).toThrow("ECS Fargate container 'EcsFargateContainer2' specifies 'ephemeralStorageSize' at 201 > 200 GB");
865+
});
795866
});

packages/@aws-cdk/aws-batch-alpha/test/integ.ecs-job-definition.js.snapshot/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"validateOnSynth": false,
1818
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
1919
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
20-
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/9186f9ef6b963bbe6a32d639a67ffce49446d36bd04de52f495e7ee371e3ce3d.json",
20+
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/7eabaa659955f076359ed72f88d929cfe7651a904b6038ae0f3b3215ab36ac6c.json",
2121
"requiresBootstrapStackVersion": 6,
2222
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
2323
"additionalDependencies": [

packages/@aws-cdk/aws-batch-alpha/test/integ.ecs-job-definition.js.snapshot/stack.assets.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"version": "31.0.0",
33
"files": {
4-
"9186f9ef6b963bbe6a32d639a67ffce49446d36bd04de52f495e7ee371e3ce3d": {
4+
"7eabaa659955f076359ed72f88d929cfe7651a904b6038ae0f3b3215ab36ac6c": {
55
"source": {
66
"path": "stack.template.json",
77
"packaging": "file"
88
},
99
"destinations": {
1010
"current_account-current_region": {
1111
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12-
"objectKey": "9186f9ef6b963bbe6a32d639a67ffce49446d36bd04de52f495e7ee371e3ce3d.json",
12+
"objectKey": "7eabaa659955f076359ed72f88d929cfe7651a904b6038ae0f3b3215ab36ac6c.json",
1313
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
1414
}
1515
}

packages/@aws-cdk/aws-batch-alpha/test/integ.ecs-job-definition.js.snapshot/stack.template.json

+3
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,9 @@
579579
"Type": "container",
580580
"ContainerProperties": {
581581
"Environment": [],
582+
"EphemeralStorage": {
583+
"SizeInGiB": 100
584+
},
582585
"ExecutionRoleArn": {
583586
"Fn::GetAtt": [
584587
"myFargateContainerExecutionRoleB9EB79EA",

0 commit comments

Comments
 (0)