Skip to content

Commit 643042b

Browse files
authored
feat(stepfunctions-tasks): add revision number (#24226)
I can specify revision number when we use `EcsRunTask`. According to [API referance](https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_RunTask.html#ECS-RunTask-request-taskDefinition), we can specify revision number. I initially thought that `EcsTask` has to have this propety, but revision number can be specified only when we execute([ECS referance](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#family)). So I fixed it like this. Closes #23491 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 7dd8b7e commit 643042b

File tree

8 files changed

+212
-9
lines changed

8 files changed

+212
-9
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ state machine role's policy. Use it in the case where the call requires more tha
228228
to be executed:
229229

230230
```ts
231-
const detectLabels = new tasks.CallAwsService(stack, 'DetectLabels', {
231+
const detectLabels = new tasks.CallAwsService(this, 'DetectLabels', {
232232
service: 'rekognition',
233233
action: 'detectLabels',
234234
iamResources: ['*'],

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ export interface EcsRunTaskProps extends sfn.TaskStateBaseProps {
2727
*/
2828
readonly taskDefinition: ecs.TaskDefinition;
2929

30+
/**
31+
* The revision number of ECS task definiton family
32+
*
33+
* @default - '$latest'
34+
*/
35+
readonly revisionNumber?: number;
36+
3037
/**
3138
* Container setting overrides
3239
*
@@ -282,7 +289,7 @@ export class EcsRunTask extends sfn.TaskStateBase implements ec2.IConnectable {
282289
Resource: integrationResourceArn('ecs', 'runTask', this.integrationPattern),
283290
Parameters: sfn.FieldUtils.renderObject({
284291
Cluster: this.props.cluster.clusterArn,
285-
TaskDefinition: this.props.taskDefinition.family,
292+
TaskDefinition: this.props.revisionNumber === undefined ? this.props.taskDefinition.family : `${this.props.taskDefinition.family}:${this.props.revisionNumber.toString()}`,
286293
NetworkConfiguration: this.networkConfiguration,
287294
Overrides: renderOverrides(this.props.containerOverrides),
288295
...this.props.launchTarget.bind(this, { taskDefinition: this.props.taskDefinition, cluster: this.props.cluster }).parameters,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"version": "30.0.0",
33
"files": {
4-
"add92abd33bdedcd7447faa84ea0fe330f8f547f3de2b71052511e602a947aa7": {
4+
"e24a1536363f5a0e085f665d59c23d647a03864eb45d88b8d07e2d0d8e285ee1": {
55
"source": {
66
"path": "aws-sfn-tasks-ecs-fargate-integ.template.json",
77
"packaging": "file"
88
},
99
"destinations": {
1010
"current_account-current_region": {
1111
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12-
"objectKey": "add92abd33bdedcd7447faa84ea0fe330f8f547f3de2b71052511e602a947aa7.json",
12+
"objectKey": "e24a1536363f5a0e085f665d59c23d647a03864eb45d88b8d07e2d0d8e285ee1.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-stepfunctions-tasks/test/ecs/integ.fargate-run-task.js.snapshot/aws-sfn-tasks-ecs-fargate-integ.template.json

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,22 @@
560560
}
561561
}
562562
},
563+
"FargeateTaskSetRevisionNumberSecurityGroup916C9B0B": {
564+
"Type": "AWS::EC2::SecurityGroup",
565+
"Properties": {
566+
"GroupDescription": "aws-sfn-tasks-ecs-fargate-integ/FargeateTaskSetRevisionNumber/SecurityGroup",
567+
"SecurityGroupEgress": [
568+
{
569+
"CidrIp": "0.0.0.0/0",
570+
"Description": "Allow all outbound traffic by default",
571+
"IpProtocol": "-1"
572+
}
573+
],
574+
"VpcId": {
575+
"Ref": "FargateClusterVpc377E8024"
576+
}
577+
}
578+
},
563579
"StateMachineRoleB840431D": {
564580
"Type": "AWS::IAM::Role",
565581
"Properties": {
@@ -775,7 +791,7 @@
775791
"Fn::Join": [
776792
"",
777793
[
778-
"{\"StartAt\":\"Start\",\"States\":{\"Start\":{\"Type\":\"Pass\",\"Result\":{\"SomeKey\":\"SomeValue\",\"Timeout\":900},\"Next\":\"FargateTask\"},\"FargateTask\":{\"End\":true,\"Type\":\"Task\",\"TimeoutSecondsPath\":\"$.Timeout\",\"Resource\":\"arn:",
794+
"{\"StartAt\":\"Start\",\"States\":{\"Start\":{\"Type\":\"Pass\",\"Result\":{\"SomeKey\":\"SomeValue\",\"Timeout\":900},\"Next\":\"FargateTask\"},\"FargateTask\":{\"Next\":\"FargeateTaskSetRevisionNumber\",\"Type\":\"Task\",\"TimeoutSecondsPath\":\"$.Timeout\",\"Resource\":\"arn:",
779795
{
780796
"Ref": "AWS::Partition"
781797
},
@@ -801,7 +817,33 @@
801817
"GroupId"
802818
]
803819
},
804-
"\"]}},\"Overrides\":{\"ContainerOverrides\":[{\"Name\":\"TheContainer\",\"Environment\":[{\"Name\":\"SOME_KEY\",\"Value.$\":\"$.SomeKey\"}]}]},\"LaunchType\":\"FARGATE\",\"PlatformVersion\":\"1.4.0\"}}}}"
820+
"\"]}},\"Overrides\":{\"ContainerOverrides\":[{\"Name\":\"TheContainer\",\"Environment\":[{\"Name\":\"SOME_KEY\",\"Value.$\":\"$.SomeKey\"}]}]},\"LaunchType\":\"FARGATE\",\"PlatformVersion\":\"1.4.0\"}},\"FargeateTaskSetRevisionNumber\":{\"End\":true,\"Type\":\"Task\",\"Resource\":\"arn:",
821+
{
822+
"Ref": "AWS::Partition"
823+
},
824+
":states:::ecs:runTask\",\"Parameters\":{\"Cluster\":\"",
825+
{
826+
"Fn::GetAtt": [
827+
"FargateCluster7CCD5F93",
828+
"Arn"
829+
]
830+
},
831+
"\",\"TaskDefinition\":\"awssfntasksecsfargateintegTaskDefD0F4AD10:1\",\"NetworkConfiguration\":{\"AwsvpcConfiguration\":{\"Subnets\":[\"",
832+
{
833+
"Ref": "FargateClusterVpcPrivateSubnet1Subnet9127625F"
834+
},
835+
"\",\"",
836+
{
837+
"Ref": "FargateClusterVpcPrivateSubnet2Subnet307CEE57"
838+
},
839+
"\"],\"SecurityGroups\":[\"",
840+
{
841+
"Fn::GetAtt": [
842+
"FargeateTaskSetRevisionNumberSecurityGroup916C9B0B",
843+
"GroupId"
844+
]
845+
},
846+
"\"]}},\"LaunchType\":\"FARGATE\",\"PlatformVersion\":\"1.4.0\"}}}}"
805847
]
806848
]
807849
}

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

Lines changed: 7 additions & 1 deletion
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}/add92abd33bdedcd7447faa84ea0fe330f8f547f3de2b71052511e602a947aa7.json",
20+
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/e24a1536363f5a0e085f665d59c23d647a03864eb45d88b8d07e2d0d8e285ee1.json",
2121
"requiresBootstrapStackVersion": 6,
2222
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
2323
"additionalDependencies": [
@@ -213,6 +213,12 @@
213213
"data": "FargateTaskSecurityGroup0BBB27CB"
214214
}
215215
],
216+
"/aws-sfn-tasks-ecs-fargate-integ/FargeateTaskSetRevisionNumber/SecurityGroup/Resource": [
217+
{
218+
"type": "aws:cdk:logicalId",
219+
"data": "FargeateTaskSetRevisionNumberSecurityGroup916C9B0B"
220+
}
221+
],
216222
"/aws-sfn-tasks-ecs-fargate-integ/StateMachine/Role/Resource": [
217223
{
218224
"type": "aws:cdk:logicalId",

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

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,50 @@
10161016
"version": "0.0.0"
10171017
}
10181018
},
1019+
"FargeateTaskSetRevisionNumber": {
1020+
"id": "FargeateTaskSetRevisionNumber",
1021+
"path": "aws-sfn-tasks-ecs-fargate-integ/FargeateTaskSetRevisionNumber",
1022+
"children": {
1023+
"SecurityGroup": {
1024+
"id": "SecurityGroup",
1025+
"path": "aws-sfn-tasks-ecs-fargate-integ/FargeateTaskSetRevisionNumber/SecurityGroup",
1026+
"children": {
1027+
"Resource": {
1028+
"id": "Resource",
1029+
"path": "aws-sfn-tasks-ecs-fargate-integ/FargeateTaskSetRevisionNumber/SecurityGroup/Resource",
1030+
"attributes": {
1031+
"aws:cdk:cloudformation:type": "AWS::EC2::SecurityGroup",
1032+
"aws:cdk:cloudformation:props": {
1033+
"groupDescription": "aws-sfn-tasks-ecs-fargate-integ/FargeateTaskSetRevisionNumber/SecurityGroup",
1034+
"securityGroupEgress": [
1035+
{
1036+
"cidrIp": "0.0.0.0/0",
1037+
"description": "Allow all outbound traffic by default",
1038+
"ipProtocol": "-1"
1039+
}
1040+
],
1041+
"vpcId": {
1042+
"Ref": "FargateClusterVpc377E8024"
1043+
}
1044+
}
1045+
},
1046+
"constructInfo": {
1047+
"fqn": "@aws-cdk/aws-ec2.CfnSecurityGroup",
1048+
"version": "0.0.0"
1049+
}
1050+
}
1051+
},
1052+
"constructInfo": {
1053+
"fqn": "@aws-cdk/aws-ec2.SecurityGroup",
1054+
"version": "0.0.0"
1055+
}
1056+
}
1057+
},
1058+
"constructInfo": {
1059+
"fqn": "@aws-cdk/aws-stepfunctions-tasks.EcsRunTask",
1060+
"version": "0.0.0"
1061+
}
1062+
},
10191063
"StateMachine": {
10201064
"id": "StateMachine",
10211065
"path": "aws-sfn-tasks-ecs-fargate-integ/StateMachine",
@@ -1282,7 +1326,7 @@
12821326
"Fn::Join": [
12831327
"",
12841328
[
1285-
"{\"StartAt\":\"Start\",\"States\":{\"Start\":{\"Type\":\"Pass\",\"Result\":{\"SomeKey\":\"SomeValue\",\"Timeout\":900},\"Next\":\"FargateTask\"},\"FargateTask\":{\"End\":true,\"Type\":\"Task\",\"TimeoutSecondsPath\":\"$.Timeout\",\"Resource\":\"arn:",
1329+
"{\"StartAt\":\"Start\",\"States\":{\"Start\":{\"Type\":\"Pass\",\"Result\":{\"SomeKey\":\"SomeValue\",\"Timeout\":900},\"Next\":\"FargateTask\"},\"FargateTask\":{\"Next\":\"FargeateTaskSetRevisionNumber\",\"Type\":\"Task\",\"TimeoutSecondsPath\":\"$.Timeout\",\"Resource\":\"arn:",
12861330
{
12871331
"Ref": "AWS::Partition"
12881332
},
@@ -1308,7 +1352,33 @@
13081352
"GroupId"
13091353
]
13101354
},
1311-
"\"]}},\"Overrides\":{\"ContainerOverrides\":[{\"Name\":\"TheContainer\",\"Environment\":[{\"Name\":\"SOME_KEY\",\"Value.$\":\"$.SomeKey\"}]}]},\"LaunchType\":\"FARGATE\",\"PlatformVersion\":\"1.4.0\"}}}}"
1355+
"\"]}},\"Overrides\":{\"ContainerOverrides\":[{\"Name\":\"TheContainer\",\"Environment\":[{\"Name\":\"SOME_KEY\",\"Value.$\":\"$.SomeKey\"}]}]},\"LaunchType\":\"FARGATE\",\"PlatformVersion\":\"1.4.0\"}},\"FargeateTaskSetRevisionNumber\":{\"End\":true,\"Type\":\"Task\",\"Resource\":\"arn:",
1356+
{
1357+
"Ref": "AWS::Partition"
1358+
},
1359+
":states:::ecs:runTask\",\"Parameters\":{\"Cluster\":\"",
1360+
{
1361+
"Fn::GetAtt": [
1362+
"FargateCluster7CCD5F93",
1363+
"Arn"
1364+
]
1365+
},
1366+
"\",\"TaskDefinition\":\"awssfntasksecsfargateintegTaskDefD0F4AD10:1\",\"NetworkConfiguration\":{\"AwsvpcConfiguration\":{\"Subnets\":[\"",
1367+
{
1368+
"Ref": "FargateClusterVpcPrivateSubnet1Subnet9127625F"
1369+
},
1370+
"\",\"",
1371+
{
1372+
"Ref": "FargateClusterVpcPrivateSubnet2Subnet307CEE57"
1373+
},
1374+
"\"],\"SecurityGroups\":[\"",
1375+
{
1376+
"Fn::GetAtt": [
1377+
"FargeateTaskSetRevisionNumberSecurityGroup916C9B0B",
1378+
"GroupId"
1379+
]
1380+
},
1381+
"\"]}},\"LaunchType\":\"FARGATE\",\"PlatformVersion\":\"1.4.0\"}}}}"
13121382
]
13131383
]
13141384
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ const definition = new sfn.Pass(stack, 'Start', {
5555
}),
5656
taskTimeout: sfn.Timeout.at('$.Timeout'),
5757
}),
58+
).next(
59+
new tasks.EcsRunTask(stack, 'FargeateTaskSetRevisionNumber', {
60+
cluster,
61+
taskDefinition,
62+
revisionNumber: 1,
63+
launchTarget: new tasks.EcsFargateLaunchTarget({
64+
platformVersion: ecs.FargatePlatformVersion.VERSION1_4,
65+
}),
66+
}),
5867
);
5968

6069
const sm = new sfn.StateMachine(stack, 'StateMachine', {

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,3 +563,72 @@ test('Running a task with WAIT_FOR_TASK_TOKEN and task token in environment', ()
563563
taskDefinition,
564564
})).not.toThrow();
565565
});
566+
567+
test('Set revision number of ECS task denition family', () => {
568+
// When
569+
const taskDefinition = new ecs.TaskDefinition(stack, 'TD', {
570+
memoryMiB: '512',
571+
cpu: '256',
572+
compatibility: ecs.Compatibility.FARGATE,
573+
});
574+
taskDefinition.addContainer('TheContainer', {
575+
image: ecs.ContainerImage.fromRegistry('foo/bar'),
576+
memoryLimitMiB: 256,
577+
});
578+
const runTask = new tasks.EcsRunTask(stack, 'task', {
579+
cluster,
580+
taskDefinition: taskDefinition,
581+
revisionNumber: 1,
582+
launchTarget: new tasks.EcsFargateLaunchTarget(),
583+
});
584+
585+
// Then
586+
expect(stack.resolve(runTask.toStateJson())).toEqual(
587+
{
588+
End: true,
589+
Parameters: {
590+
Cluster: {
591+
'Fn::GetAtt': [
592+
'ClusterEB0386A7',
593+
'Arn',
594+
],
595+
},
596+
LaunchType: 'FARGATE',
597+
NetworkConfiguration: {
598+
AwsvpcConfiguration: {
599+
SecurityGroups: [
600+
{
601+
'Fn::GetAtt': [
602+
'taskSecurityGroup28F0D539',
603+
'GroupId',
604+
],
605+
},
606+
],
607+
Subnets: [
608+
{
609+
Ref: 'VpcPrivateSubnet1Subnet536B997A',
610+
},
611+
{
612+
Ref: 'VpcPrivateSubnet2Subnet3788AAA1',
613+
},
614+
],
615+
},
616+
},
617+
TaskDefinition: 'TD:1',
618+
},
619+
Resource: {
620+
'Fn::Join': [
621+
'',
622+
[
623+
'arn:',
624+
{
625+
'Ref': 'AWS::Partition',
626+
},
627+
':states:::ecs:runTask',
628+
],
629+
],
630+
},
631+
Type: 'Task',
632+
},
633+
);
634+
});

0 commit comments

Comments
 (0)