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(batch): add fargate Runtime Platform properties to ECS Fargate C… #28841

Merged
merged 13 commits into from
Jan 30, 2024

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,11 @@
"Type": "VCPU",
"Value": "16"
}
]
],
"RuntimePlatform": {
"CpuArchitecture": "ARM64",
"OperatingSystemFamily": "LINUX"
}
},
"JobDefinitionName": "foofoo",
"Parameters": {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ new batch.EcsJobDefinition(stack, 'ECSFargateJobDefn', {
memory: Size.mebibytes(32768),
ephemeralStorageSize: Size.gibibytes(100),
fargatePlatformVersion: FargatePlatformVersion.LATEST,
fargateCpuArchitecture: ecs.CpuArchitecture.ARM64,
fargateOperatingSystemFamily: ecs.OperatingSystemFamily.LINUX,
Comment on lines +63 to +64
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it guaranteed that the batch EcsFaragetContainerDefintion will always match ecs? I am wondering if there is a case where ECS needs some update and then batch needs to follow. Meaning there would be a time where batch doesn't support all types ECS supports.

I might be looking at this in the wrong way or have an miss understanding on how things connect together within CDK.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw it was already using enum values from ECS definition so I didn't really think about it... Looking at the current values they're the same but I'm not sure if that's guaranteed or it's documented somewhere.
If we can generate all these values from the schema in the future, then we can be confident about the values here.

}),
jobDefinitionName: 'foofoo',
parameters: {
Expand Down
15 changes: 15 additions & 0 deletions packages/aws-cdk-lib/aws-batch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,21 @@ jobDefn.container.addVolume(batch.EcsVolume.efs({
}));
```

### Running an ECS workflow with Fargate container

```ts
const jobDefn = new batch.EcsJobDefinition(this, 'JobDefn', {
container: new batch.EcsFargateContainerDefinition(this, 'myFargateContainer', {
image: ecs.ContainerImage.fromRegistry('public.ecr.aws/amazonlinux/amazonlinux:latest'),
memory: cdk.Size.mebibytes(2048),
cpu: 256,
ephemeralStorageSize: cdk.Size.gibibytes(100),
fargateCpuArchitecture: ecs.CpuArchitecture.ARM64,
fargateOperatingSystemFamily: ecs.OperatingSystemFamily.LINUX,
}),
});
```

### Secrets

You can expose SecretsManager Secret ARNs or SSM Parameters to your container as environment variables.
Expand Down
36 changes: 36 additions & 0 deletions packages/aws-cdk-lib/aws-batch/lib/ecs-container-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,20 @@ export interface IEcsFargateContainerDefinition extends IEcsContainerDefinition
* @default - 20 GiB
*/
readonly ephemeralStorageSize?: Size;

/**
* The vCPU architecture of Fargate Runtime.
*
* @default - X86_64
*/
readonly fargateCpuArchitecture?: ecs.CpuArchitecture;

/**
* The operating system for the compute environment.
*
* @default - LINUX
*/
readonly fargateOperatingSystemFamily?: ecs.OperatingSystemFamily;
}

/**
Expand Down Expand Up @@ -1009,6 +1023,20 @@ export interface EcsFargateContainerDefinitionProps extends EcsContainerDefiniti
* @default - 20 GiB
*/
readonly ephemeralStorageSize?: Size;

/**
* The vCPU architecture of Fargate Runtime.
*
* @default - X86_64
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my own understanding, why set defaults for this and fargateOperatingSystemFamily?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is the default value should be same as the default value in CFN resource.

*/
readonly fargateCpuArchitecture?: ecs.CpuArchitecture;

/**
* The operating system for the compute environment.
*
* @default - LINUX
*/
readonly fargateOperatingSystemFamily?: ecs.OperatingSystemFamily;
}

/**
Expand All @@ -1018,12 +1046,16 @@ export class EcsFargateContainerDefinition extends EcsContainerDefinitionBase im
public readonly fargatePlatformVersion?: ecs.FargatePlatformVersion;
public readonly assignPublicIp?: boolean;
public readonly ephemeralStorageSize?: Size;
public readonly fargateCpuArchitecture?: ecs.CpuArchitecture;
public readonly fargateOperatingSystemFamily?: ecs.OperatingSystemFamily;

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

// validates ephemeralStorageSize is within limits
if (props.ephemeralStorageSize) {
Expand All @@ -1050,6 +1082,10 @@ export class EcsFargateContainerDefinition extends EcsContainerDefinitionBase im
networkConfiguration: {
assignPublicIp: this.assignPublicIp ? 'ENABLED' : 'DISABLED',
},
runtimePlatform: {
cpuArchitecture: this.fargateCpuArchitecture?._cpuArchitecture,
operatingSystemFamily: this.fargateOperatingSystemFamily?._operatingSystemFamily,
},
};
};
}
Expand Down
21 changes: 21 additions & 0 deletions packages/aws-cdk-lib/aws-batch/test/ecs-job-definition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ test('EcsJobDefinition uses Compatibility.FARGATE for Fargate containers', () =>
});
});

test('EcsJobDefinition uses runtimePlatform for Fargate containers', () => {
// GIVEN
const stack = new Stack();

// WHEN
new EcsJobDefinition(stack, 'ECSJobDefn', {
container: new EcsFargateContainerDefinition(stack, 'EcsContainer', {
cpu: 256,
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
memory: Size.mebibytes(2048),
fargateCpuArchitecture: ecs.CpuArchitecture.ARM64,
fargateOperatingSystemFamily: ecs.OperatingSystemFamily.LINUX,
}),
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Batch::JobDefinition', {
PlatformCapabilities: [Compatibility.FARGATE],
});
});

test('can be imported from ARN', () => {
// GIVEN
const stack = new Stack();
Expand Down