Skip to content

Commit

Permalink
feat(stepfunctions-tasks): add revision number (#24226)
Browse files Browse the repository at this point in the history
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*
  • Loading branch information
westhouseK authored Feb 28, 2023
1 parent 7dd8b7e commit 643042b
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-stepfunctions-tasks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ state machine role's policy. Use it in the case where the call requires more tha
to be executed:

```ts
const detectLabels = new tasks.CallAwsService(stack, 'DetectLabels', {
const detectLabels = new tasks.CallAwsService(this, 'DetectLabels', {
service: 'rekognition',
action: 'detectLabels',
iamResources: ['*'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ export interface EcsRunTaskProps extends sfn.TaskStateBaseProps {
*/
readonly taskDefinition: ecs.TaskDefinition;

/**
* The revision number of ECS task definiton family
*
* @default - '$latest'
*/
readonly revisionNumber?: number;

/**
* Container setting overrides
*
Expand Down Expand Up @@ -282,7 +289,7 @@ export class EcsRunTask extends sfn.TaskStateBase implements ec2.IConnectable {
Resource: integrationResourceArn('ecs', 'runTask', this.integrationPattern),
Parameters: sfn.FieldUtils.renderObject({
Cluster: this.props.cluster.clusterArn,
TaskDefinition: this.props.taskDefinition.family,
TaskDefinition: this.props.revisionNumber === undefined ? this.props.taskDefinition.family : `${this.props.taskDefinition.family}:${this.props.revisionNumber.toString()}`,
NetworkConfiguration: this.networkConfiguration,
Overrides: renderOverrides(this.props.containerOverrides),
...this.props.launchTarget.bind(this, { taskDefinition: this.props.taskDefinition, cluster: this.props.cluster }).parameters,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"version": "30.0.0",
"files": {
"add92abd33bdedcd7447faa84ea0fe330f8f547f3de2b71052511e602a947aa7": {
"e24a1536363f5a0e085f665d59c23d647a03864eb45d88b8d07e2d0d8e285ee1": {
"source": {
"path": "aws-sfn-tasks-ecs-fargate-integ.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "add92abd33bdedcd7447faa84ea0fe330f8f547f3de2b71052511e602a947aa7.json",
"objectKey": "e24a1536363f5a0e085f665d59c23d647a03864eb45d88b8d07e2d0d8e285ee1.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 @@ -560,6 +560,22 @@
}
}
},
"FargeateTaskSetRevisionNumberSecurityGroup916C9B0B": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"GroupDescription": "aws-sfn-tasks-ecs-fargate-integ/FargeateTaskSetRevisionNumber/SecurityGroup",
"SecurityGroupEgress": [
{
"CidrIp": "0.0.0.0/0",
"Description": "Allow all outbound traffic by default",
"IpProtocol": "-1"
}
],
"VpcId": {
"Ref": "FargateClusterVpc377E8024"
}
}
},
"StateMachineRoleB840431D": {
"Type": "AWS::IAM::Role",
"Properties": {
Expand Down Expand Up @@ -775,7 +791,7 @@
"Fn::Join": [
"",
[
"{\"StartAt\":\"Start\",\"States\":{\"Start\":{\"Type\":\"Pass\",\"Result\":{\"SomeKey\":\"SomeValue\",\"Timeout\":900},\"Next\":\"FargateTask\"},\"FargateTask\":{\"End\":true,\"Type\":\"Task\",\"TimeoutSecondsPath\":\"$.Timeout\",\"Resource\":\"arn:",
"{\"StartAt\":\"Start\",\"States\":{\"Start\":{\"Type\":\"Pass\",\"Result\":{\"SomeKey\":\"SomeValue\",\"Timeout\":900},\"Next\":\"FargateTask\"},\"FargateTask\":{\"Next\":\"FargeateTaskSetRevisionNumber\",\"Type\":\"Task\",\"TimeoutSecondsPath\":\"$.Timeout\",\"Resource\":\"arn:",
{
"Ref": "AWS::Partition"
},
Expand All @@ -801,7 +817,33 @@
"GroupId"
]
},
"\"]}},\"Overrides\":{\"ContainerOverrides\":[{\"Name\":\"TheContainer\",\"Environment\":[{\"Name\":\"SOME_KEY\",\"Value.$\":\"$.SomeKey\"}]}]},\"LaunchType\":\"FARGATE\",\"PlatformVersion\":\"1.4.0\"}}}}"
"\"]}},\"Overrides\":{\"ContainerOverrides\":[{\"Name\":\"TheContainer\",\"Environment\":[{\"Name\":\"SOME_KEY\",\"Value.$\":\"$.SomeKey\"}]}]},\"LaunchType\":\"FARGATE\",\"PlatformVersion\":\"1.4.0\"}},\"FargeateTaskSetRevisionNumber\":{\"End\":true,\"Type\":\"Task\",\"Resource\":\"arn:",
{
"Ref": "AWS::Partition"
},
":states:::ecs:runTask\",\"Parameters\":{\"Cluster\":\"",
{
"Fn::GetAtt": [
"FargateCluster7CCD5F93",
"Arn"
]
},
"\",\"TaskDefinition\":\"awssfntasksecsfargateintegTaskDefD0F4AD10:1\",\"NetworkConfiguration\":{\"AwsvpcConfiguration\":{\"Subnets\":[\"",
{
"Ref": "FargateClusterVpcPrivateSubnet1Subnet9127625F"
},
"\",\"",
{
"Ref": "FargateClusterVpcPrivateSubnet2Subnet307CEE57"
},
"\"],\"SecurityGroups\":[\"",
{
"Fn::GetAtt": [
"FargeateTaskSetRevisionNumberSecurityGroup916C9B0B",
"GroupId"
]
},
"\"]}},\"LaunchType\":\"FARGATE\",\"PlatformVersion\":\"1.4.0\"}}}}"
]
]
}
Expand Down
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}/add92abd33bdedcd7447faa84ea0fe330f8f547f3de2b71052511e602a947aa7.json",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/e24a1536363f5a0e085f665d59c23d647a03864eb45d88b8d07e2d0d8e285ee1.json",
"requiresBootstrapStackVersion": 6,
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
"additionalDependencies": [
Expand Down Expand Up @@ -213,6 +213,12 @@
"data": "FargateTaskSecurityGroup0BBB27CB"
}
],
"/aws-sfn-tasks-ecs-fargate-integ/FargeateTaskSetRevisionNumber/SecurityGroup/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "FargeateTaskSetRevisionNumberSecurityGroup916C9B0B"
}
],
"/aws-sfn-tasks-ecs-fargate-integ/StateMachine/Role/Resource": [
{
"type": "aws:cdk:logicalId",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,50 @@
"version": "0.0.0"
}
},
"FargeateTaskSetRevisionNumber": {
"id": "FargeateTaskSetRevisionNumber",
"path": "aws-sfn-tasks-ecs-fargate-integ/FargeateTaskSetRevisionNumber",
"children": {
"SecurityGroup": {
"id": "SecurityGroup",
"path": "aws-sfn-tasks-ecs-fargate-integ/FargeateTaskSetRevisionNumber/SecurityGroup",
"children": {
"Resource": {
"id": "Resource",
"path": "aws-sfn-tasks-ecs-fargate-integ/FargeateTaskSetRevisionNumber/SecurityGroup/Resource",
"attributes": {
"aws:cdk:cloudformation:type": "AWS::EC2::SecurityGroup",
"aws:cdk:cloudformation:props": {
"groupDescription": "aws-sfn-tasks-ecs-fargate-integ/FargeateTaskSetRevisionNumber/SecurityGroup",
"securityGroupEgress": [
{
"cidrIp": "0.0.0.0/0",
"description": "Allow all outbound traffic by default",
"ipProtocol": "-1"
}
],
"vpcId": {
"Ref": "FargateClusterVpc377E8024"
}
}
},
"constructInfo": {
"fqn": "@aws-cdk/aws-ec2.CfnSecurityGroup",
"version": "0.0.0"
}
}
},
"constructInfo": {
"fqn": "@aws-cdk/aws-ec2.SecurityGroup",
"version": "0.0.0"
}
}
},
"constructInfo": {
"fqn": "@aws-cdk/aws-stepfunctions-tasks.EcsRunTask",
"version": "0.0.0"
}
},
"StateMachine": {
"id": "StateMachine",
"path": "aws-sfn-tasks-ecs-fargate-integ/StateMachine",
Expand Down Expand Up @@ -1282,7 +1326,7 @@
"Fn::Join": [
"",
[
"{\"StartAt\":\"Start\",\"States\":{\"Start\":{\"Type\":\"Pass\",\"Result\":{\"SomeKey\":\"SomeValue\",\"Timeout\":900},\"Next\":\"FargateTask\"},\"FargateTask\":{\"End\":true,\"Type\":\"Task\",\"TimeoutSecondsPath\":\"$.Timeout\",\"Resource\":\"arn:",
"{\"StartAt\":\"Start\",\"States\":{\"Start\":{\"Type\":\"Pass\",\"Result\":{\"SomeKey\":\"SomeValue\",\"Timeout\":900},\"Next\":\"FargateTask\"},\"FargateTask\":{\"Next\":\"FargeateTaskSetRevisionNumber\",\"Type\":\"Task\",\"TimeoutSecondsPath\":\"$.Timeout\",\"Resource\":\"arn:",
{
"Ref": "AWS::Partition"
},
Expand All @@ -1308,7 +1352,33 @@
"GroupId"
]
},
"\"]}},\"Overrides\":{\"ContainerOverrides\":[{\"Name\":\"TheContainer\",\"Environment\":[{\"Name\":\"SOME_KEY\",\"Value.$\":\"$.SomeKey\"}]}]},\"LaunchType\":\"FARGATE\",\"PlatformVersion\":\"1.4.0\"}}}}"
"\"]}},\"Overrides\":{\"ContainerOverrides\":[{\"Name\":\"TheContainer\",\"Environment\":[{\"Name\":\"SOME_KEY\",\"Value.$\":\"$.SomeKey\"}]}]},\"LaunchType\":\"FARGATE\",\"PlatformVersion\":\"1.4.0\"}},\"FargeateTaskSetRevisionNumber\":{\"End\":true,\"Type\":\"Task\",\"Resource\":\"arn:",
{
"Ref": "AWS::Partition"
},
":states:::ecs:runTask\",\"Parameters\":{\"Cluster\":\"",
{
"Fn::GetAtt": [
"FargateCluster7CCD5F93",
"Arn"
]
},
"\",\"TaskDefinition\":\"awssfntasksecsfargateintegTaskDefD0F4AD10:1\",\"NetworkConfiguration\":{\"AwsvpcConfiguration\":{\"Subnets\":[\"",
{
"Ref": "FargateClusterVpcPrivateSubnet1Subnet9127625F"
},
"\",\"",
{
"Ref": "FargateClusterVpcPrivateSubnet2Subnet307CEE57"
},
"\"],\"SecurityGroups\":[\"",
{
"Fn::GetAtt": [
"FargeateTaskSetRevisionNumberSecurityGroup916C9B0B",
"GroupId"
]
},
"\"]}},\"LaunchType\":\"FARGATE\",\"PlatformVersion\":\"1.4.0\"}}}}"
]
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ const definition = new sfn.Pass(stack, 'Start', {
}),
taskTimeout: sfn.Timeout.at('$.Timeout'),
}),
).next(
new tasks.EcsRunTask(stack, 'FargeateTaskSetRevisionNumber', {
cluster,
taskDefinition,
revisionNumber: 1,
launchTarget: new tasks.EcsFargateLaunchTarget({
platformVersion: ecs.FargatePlatformVersion.VERSION1_4,
}),
}),
);

const sm = new sfn.StateMachine(stack, 'StateMachine', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,3 +563,72 @@ test('Running a task with WAIT_FOR_TASK_TOKEN and task token in environment', ()
taskDefinition,
})).not.toThrow();
});

test('Set revision number of ECS task denition family', () => {
// When
const taskDefinition = new ecs.TaskDefinition(stack, 'TD', {
memoryMiB: '512',
cpu: '256',
compatibility: ecs.Compatibility.FARGATE,
});
taskDefinition.addContainer('TheContainer', {
image: ecs.ContainerImage.fromRegistry('foo/bar'),
memoryLimitMiB: 256,
});
const runTask = new tasks.EcsRunTask(stack, 'task', {
cluster,
taskDefinition: taskDefinition,
revisionNumber: 1,
launchTarget: new tasks.EcsFargateLaunchTarget(),
});

// Then
expect(stack.resolve(runTask.toStateJson())).toEqual(
{
End: true,
Parameters: {
Cluster: {
'Fn::GetAtt': [
'ClusterEB0386A7',
'Arn',
],
},
LaunchType: 'FARGATE',
NetworkConfiguration: {
AwsvpcConfiguration: {
SecurityGroups: [
{
'Fn::GetAtt': [
'taskSecurityGroup28F0D539',
'GroupId',
],
},
],
Subnets: [
{
Ref: 'VpcPrivateSubnet1Subnet536B997A',
},
{
Ref: 'VpcPrivateSubnet2Subnet3788AAA1',
},
],
},
},
TaskDefinition: 'TD:1',
},
Resource: {
'Fn::Join': [
'',
[
'arn:',
{
'Ref': 'AWS::Partition',
},
':states:::ecs:runTask',
],
],
},
Type: 'Task',
},
);
});

0 comments on commit 643042b

Please sign in to comment.