Skip to content

Commit fe02901

Browse files
kackytGavinZZ
authored andcommitted
feat(stepfunctions-tasks): add cpu and memory parameters to EcsRunTask (#30140)
### Issue # (if applicable) Closes #30027 . ### Reason for this change As described in the issue. ### Description of changes Add cpu and memoryMiB property to EcsRunTaskProps. ### Description of how you validated changes Add unit tests. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 56b44c4 commit fe02901

File tree

8 files changed

+147
-25
lines changed

8 files changed

+147
-25
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/ecs/integ.fargate-run-task.js.snapshot/aws-sfn-tasks-ecs-fargate-run-task.assets.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/ecs/integ.fargate-run-task.js.snapshot/aws-sfn-tasks-ecs-fargate-run-task.template.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@
828828
"GroupId"
829829
]
830830
},
831-
"\"]}},\"Overrides\":{\"ContainerOverrides\":[{\"Name\":\"TheContainer\",\"Environment\":[{\"Name\":\"SOME_KEY\",\"Value.$\":\"$.SomeKey\"}]}]},\"LaunchType\":\"FARGATE\",\"PlatformVersion\":\"1.4.0\"}},\"FargateTaskSetRevisionNumber\":{\"Next\":\"FargateTaskWithPropagatedTag\",\"Type\":\"Task\",\"Resource\":\"arn:",
831+
"\"]}},\"Overrides\":{\"Cpu\":\"1024\",\"Memory\":\"2048\",\"ContainerOverrides\":[{\"Name\":\"TheContainer\",\"Environment\":[{\"Name\":\"SOME_KEY\",\"Value.$\":\"$.SomeKey\"}]}]},\"LaunchType\":\"FARGATE\",\"PlatformVersion\":\"1.4.0\"}},\"FargateTaskSetRevisionNumber\":{\"Next\":\"FargateTaskWithPropagatedTag\",\"Type\":\"Task\",\"Resource\":\"arn:",
832832
{
833833
"Ref": "AWS::Partition"
834834
},

packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/ecs/integ.fargate-run-task.js.snapshot/manifest.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/ecs/integ.fargate-run-task.js.snapshot/tree.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/ecs/integ.fargate-run-task.ts

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ const definition = new sfn.Pass(stack, 'Start', {
5757
platformVersion: ecs.FargatePlatformVersion.VERSION1_4,
5858
}),
5959
taskTimeout: sfn.Timeout.at('$.Timeout'),
60+
cpu: '1024',
61+
memoryMiB: '2048',
6062
}),
6163
).next(
6264
new tasks.EcsRunTask(stack, 'FargateTaskSetRevisionNumber', {

packages/aws-cdk-lib/aws-stepfunctions-tasks/README.md

+33
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,39 @@ const runTask = new tasks.EcsRunTask(this, 'RunFargate', {
701701
});
702702
```
703703

704+
#### Override CPU and Memory Parameter
705+
706+
By setting the property cpu or memoryMiB, you can override the Fargate or EC2 task instance size at runtime.
707+
708+
see: https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_TaskOverride.html
709+
710+
```ts
711+
const vpc = ec2.Vpc.fromLookup(this, 'Vpc', {
712+
isDefault: true,
713+
});
714+
const cluster = new ecs.Cluster(this, 'ECSCluster', { vpc });
715+
716+
const taskDefinition = new ecs.TaskDefinition(this, 'TD', {
717+
compatibility: ecs.Compatibility.FARGATE,
718+
cpu: '256',
719+
memoryMiB: '512'
720+
});
721+
722+
taskDefinition.addContainer('TheContainer', {
723+
image: ecs.ContainerImage.fromRegistry('foo/bar'),
724+
});
725+
726+
const runTask = new tasks.EcsRunTask(this, 'Run', {
727+
integrationPattern: sfn.IntegrationPattern.RUN_JOB,
728+
cluster,
729+
taskDefinition,
730+
launchTarget: new tasks.EcsFargateLaunchTarget(),
731+
cpu: '1024',
732+
memoryMiB: '1048'
733+
});
734+
```
735+
736+
704737
#### ECS enable Exec
705738

706739
By setting the property [`enableExecuteCommand`](https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_RunTask.html#ECS-RunTask-request-enableExecuteCommand) to `true`, you can enable the [ECS Exec feature](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-exec.html) for the task for either Fargate or EC2 launch types.

packages/aws-cdk-lib/aws-stepfunctions-tasks/lib/ecs/run-task.ts

+50-17
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,20 @@ export interface EcsRunTaskProps extends sfn.TaskStateBaseProps {
9090
* @default false
9191
*/
9292
readonly enableExecuteCommand?: boolean;
93+
94+
/**
95+
* Cpu setting override
96+
* @see https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_TaskOverride.html
97+
* @default - No override
98+
*/
99+
readonly cpu?: string;
100+
101+
/**
102+
* Memory setting override
103+
* @see https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_TaskOverride.html
104+
* @default - No override
105+
*/
106+
readonly memoryMiB?: string;
93107
}
94108

95109
/**
@@ -310,7 +324,12 @@ export class EcsRunTask extends sfn.TaskStateBase implements ec2.IConnectable {
310324
Cluster: this.props.cluster.clusterArn,
311325
TaskDefinition: this.props.revisionNumber === undefined ? this.props.taskDefinition.family : `${this.props.taskDefinition.family}:${this.props.revisionNumber.toString()}`,
312326
NetworkConfiguration: this.networkConfiguration,
313-
Overrides: renderOverrides(this.props.containerOverrides),
327+
Overrides: renderOverrides(
328+
{
329+
cpu: this.props.cpu,
330+
memoryMiB: this.props.memoryMiB,
331+
containerOverrides: this.props.containerOverrides,
332+
}),
314333
PropagateTags: this.props.propagatedTagSource,
315334
...this.props.launchTarget.bind(this, { taskDefinition: this.props.taskDefinition, cluster: this.props.cluster }).parameters,
316335
EnableExecuteCommand: this.props.enableExecuteCommand,
@@ -415,26 +434,40 @@ export class EcsRunTask extends sfn.TaskStateBase implements ec2.IConnectable {
415434
}
416435
}
417436

418-
function renderOverrides(containerOverrides?: ContainerOverride[]) {
419-
if (!containerOverrides || containerOverrides.length === 0) {
437+
interface OverrideProps {
438+
cpu?: string;
439+
memoryMiB?: string;
440+
containerOverrides?: ContainerOverride[];
441+
}
442+
443+
function renderOverrides(props: OverrideProps) {
444+
const containerOverrides = props.containerOverrides;
445+
const noContainerOverrides = !containerOverrides || containerOverrides.length === 0;
446+
if (noContainerOverrides && !props.cpu && !props.memoryMiB) {
420447
return undefined;
421448
}
422449

423450
const ret = new Array<any>();
424-
for (const override of containerOverrides) {
425-
ret.push({
426-
Name: override.containerDefinition.containerName,
427-
Command: override.command,
428-
Cpu: override.cpu,
429-
Memory: override.memoryLimit,
430-
MemoryReservation: override.memoryReservation,
431-
Environment:
432-
override.environment?.map((e) => ({
433-
Name: e.name,
434-
Value: e.value,
435-
})),
436-
});
451+
if (!noContainerOverrides) {
452+
for (const override of containerOverrides) {
453+
ret.push({
454+
Name: override.containerDefinition.containerName,
455+
Command: override.command,
456+
Cpu: override.cpu,
457+
Memory: override.memoryLimit,
458+
MemoryReservation: override.memoryReservation,
459+
Environment:
460+
override.environment?.map((e) => ({
461+
Name: e.name,
462+
Value: e.value,
463+
})),
464+
});
465+
}
437466
}
438467

439-
return { ContainerOverrides: ret };
468+
return {
469+
Cpu: props.cpu,
470+
Memory: props.memoryMiB,
471+
ContainerOverrides: noContainerOverrides ? undefined : ret,
472+
};
440473
}

packages/aws-cdk-lib/aws-stepfunctions-tasks/test/ecs/run-tasks.test.ts

+54
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,60 @@ test('Running a task with NONE as propagated tag source', () => {
209209
}).toStateJson())).toHaveProperty('Parameters.PropagateTags', 'NONE');
210210
});
211211

212+
test('Running a task with cpu parameter', () => {
213+
const taskDefinition = new ecs.TaskDefinition(stack, 'TD', {
214+
memoryMiB: '1024',
215+
cpu: '512',
216+
compatibility: ecs.Compatibility.FARGATE,
217+
});
218+
const containerDefinition = taskDefinition.addContainer('TheContainer', {
219+
containerName: 'ExplicitContainerName',
220+
image: ecs.ContainerImage.fromRegistry('foo/bar'),
221+
memoryLimitMiB: 256,
222+
});
223+
224+
expect(stack.resolve(
225+
new tasks.EcsRunTask(stack, 'task', {
226+
cluster,
227+
taskDefinition,
228+
cpu: '1024',
229+
containerOverrides: [
230+
{
231+
containerDefinition,
232+
environment: [{ name: 'SOME_KEY', value: sfn.JsonPath.stringAt('$.SomeKey') }],
233+
},
234+
],
235+
launchTarget: new tasks.EcsFargateLaunchTarget(),
236+
}).toStateJson())).toHaveProperty('Parameters.Overrides.Cpu', '1024');
237+
});
238+
239+
test('Running a task with memory parameter', () => {
240+
const taskDefinition = new ecs.TaskDefinition(stack, 'TD', {
241+
memoryMiB: '1024',
242+
cpu: '512',
243+
compatibility: ecs.Compatibility.FARGATE,
244+
});
245+
const containerDefinition = taskDefinition.addContainer('TheContainer', {
246+
containerName: 'ExplicitContainerName',
247+
image: ecs.ContainerImage.fromRegistry('foo/bar'),
248+
memoryLimitMiB: 256,
249+
});
250+
251+
expect(stack.resolve(
252+
new tasks.EcsRunTask(stack, 'task', {
253+
cluster,
254+
taskDefinition,
255+
memoryMiB: '2048',
256+
containerOverrides: [
257+
{
258+
containerDefinition,
259+
environment: [{ name: 'SOME_KEY', value: sfn.JsonPath.stringAt('$.SomeKey') }],
260+
},
261+
],
262+
launchTarget: new tasks.EcsFargateLaunchTarget(),
263+
}).toStateJson())).toHaveProperty('Parameters.Overrides.Memory', '2048');
264+
});
265+
212266
test('Running a Fargate Task', () => {
213267
const taskDefinition = new ecs.TaskDefinition(stack, 'TD', {
214268
memoryMiB: '512',

0 commit comments

Comments
 (0)