Skip to content

Commit

Permalink
feat(batch): ephemeralStorage property on job definitions (#25399)
Browse files Browse the repository at this point in the history
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*
  • Loading branch information
sumupitchayan authored Jun 20, 2023
1 parent ad89f01 commit a8768f4
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 119 deletions.
28 changes: 28 additions & 0 deletions packages/@aws-cdk/aws-batch-alpha/lib/ecs-container-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,13 @@ export interface IEcsFargateContainerDefinition extends IEcsContainerDefinition
* @default LATEST
*/
readonly fargatePlatformVersion?: ecs.FargatePlatformVersion;

/**
* The size for ephemeral storage.
*
* @default - 20 GiB
*/
readonly ephemeralStorageSize?: Size;
}

/**
Expand All @@ -903,6 +910,13 @@ export interface EcsFargateContainerDefinitionProps extends EcsContainerDefiniti
* @default LATEST
*/
readonly fargatePlatformVersion?: ecs.FargatePlatformVersion;

/**
* The size for ephemeral storage.
*
* @default - 20 GiB
*/
readonly ephemeralStorageSize?: Size;
}

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

constructor(scope: Construct, id: string, props: EcsFargateContainerDefinitionProps) {
super(scope, id, props);
this.assignPublicIp = props.assignPublicIp;
this.fargatePlatformVersion = props.fargatePlatformVersion;
this.ephemeralStorageSize = props.ephemeralStorageSize;

// validates ephemeralStorageSize is within limits
if (props.ephemeralStorageSize) {
if (props.ephemeralStorageSize.toGibibytes() > 200) {
throw new Error(`ECS Fargate container '${id}' specifies 'ephemeralStorageSize' at ${props.ephemeralStorageSize.toGibibytes()} > 200 GB`);
} else if (props.ephemeralStorageSize.toGibibytes() < 21) {
throw new Error(`ECS Fargate container '${id}' specifies 'ephemeralStorageSize' at ${props.ephemeralStorageSize.toGibibytes()} < 21 GB`);
}
}
}

/**
Expand All @@ -924,6 +949,9 @@ export class EcsFargateContainerDefinition extends EcsContainerDefinitionBase im
public _renderContainerDefinition(): CfnJobDefinition.ContainerPropertiesProperty {
return {
...super._renderContainerDefinition(),
ephemeralStorage: this.ephemeralStorageSize? {
sizeInGiB: this.ephemeralStorageSize?.toGibibytes(),
} : undefined,
fargatePlatformConfiguration: {
platformVersion: this.fargatePlatformVersion?.toString(),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ArnPrincipal, Role } from 'aws-cdk-lib/aws-iam';
import * as logs from 'aws-cdk-lib/aws-logs';
import { Secret } from 'aws-cdk-lib/aws-secretsmanager';
import { Size, Stack } from 'aws-cdk-lib';
import * as cdk from 'aws-cdk-lib';
import { EcsContainerDefinitionProps, EcsEc2ContainerDefinition, EcsFargateContainerDefinition, EcsJobDefinition, EcsVolume, IEcsEc2ContainerDefinition, LinuxParameters, UlimitName } from '../lib';
import { CfnJobDefinitionProps } from 'aws-cdk-lib/aws-batch';
import { capitalizePropertyNames } from './utils';
Expand Down Expand Up @@ -792,4 +793,74 @@ describe('Fargate containers', () => {
},
});
});

test('can set ephemeralStorageSize', () => {
// WHEN
new EcsJobDefinition(stack, 'ECSJobDefn', {
container: new EcsFargateContainerDefinition(stack, 'EcsFargateContainer', {
...defaultContainerProps,
fargatePlatformVersion: ecs.FargatePlatformVersion.LATEST,
ephemeralStorageSize: Size.gibibytes(100),
}),
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Batch::JobDefinition', {
...pascalCaseExpectedProps,
ContainerProperties: {
...pascalCaseExpectedProps.ContainerProperties,
ExecutionRoleArn: {
'Fn::GetAtt': ['EcsFargateContainerExecutionRole3286EAFE', 'Arn'],
},
EphemeralStorage: {
SizeInGiB: Size.gibibytes(100).toGibibytes(),
},
},
});
});

test('can set ephemeralStorageSize as token', () => {
const ephemeralStorageValue: number = cdk.Token.asNumber(150);

// WHEN
new EcsJobDefinition(stack, 'ECSJobDefn', {
container: new EcsFargateContainerDefinition(stack, 'EcsFargateContainer', {
...defaultContainerProps,
fargatePlatformVersion: ecs.FargatePlatformVersion.LATEST,
ephemeralStorageSize: Size.gibibytes(ephemeralStorageValue),
}),
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Batch::JobDefinition', {
...pascalCaseExpectedProps,
ContainerProperties: {
...pascalCaseExpectedProps.ContainerProperties,
ExecutionRoleArn: {
'Fn::GetAtt': ['EcsFargateContainerExecutionRole3286EAFE', 'Arn'],
},
EphemeralStorage: {
SizeInGiB: Size.gibibytes(150).toGibibytes(),
},
},
});
});

test('ephemeralStorageSize throws error when out of range', () => {
expect(() => new EcsJobDefinition(stack, 'ECSJobDefn', {
container: new EcsFargateContainerDefinition(stack, 'EcsFargateContainer', {
...defaultContainerProps,
fargatePlatformVersion: ecs.FargatePlatformVersion.LATEST,
ephemeralStorageSize: Size.gibibytes(19),
}),
})).toThrow("ECS Fargate container 'EcsFargateContainer' specifies 'ephemeralStorageSize' at 19 < 21 GB");

expect(() => new EcsJobDefinition(stack, 'ECSJobDefn2', {
container: new EcsFargateContainerDefinition(stack, 'EcsFargateContainer2', {
...defaultContainerProps,
fargatePlatformVersion: ecs.FargatePlatformVersion.LATEST,
ephemeralStorageSize: Size.gibibytes(201),
}),
})).toThrow("ECS Fargate container 'EcsFargateContainer2' specifies 'ephemeralStorageSize' at 201 > 200 GB");
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"validateOnSynth": false,
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/9186f9ef6b963bbe6a32d639a67ffce49446d36bd04de52f495e7ee371e3ce3d.json",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/7eabaa659955f076359ed72f88d929cfe7651a904b6038ae0f3b3215ab36ac6c.json",
"requiresBootstrapStackVersion": 6,
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
"additionalDependencies": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"version": "31.0.0",
"files": {
"9186f9ef6b963bbe6a32d639a67ffce49446d36bd04de52f495e7ee371e3ce3d": {
"7eabaa659955f076359ed72f88d929cfe7651a904b6038ae0f3b3215ab36ac6c": {
"source": {
"path": "stack.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "9186f9ef6b963bbe6a32d639a67ffce49446d36bd04de52f495e7ee371e3ce3d.json",
"objectKey": "7eabaa659955f076359ed72f88d929cfe7651a904b6038ae0f3b3215ab36ac6c.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,9 @@
"Type": "container",
"ContainerProperties": {
"Environment": [],
"EphemeralStorage": {
"SizeInGiB": 100
},
"ExecutionRoleArn": {
"Fn::GetAtt": [
"myFargateContainerExecutionRoleB9EB79EA",
Expand Down
Loading

0 comments on commit a8768f4

Please sign in to comment.