Skip to content

Commit e3bc59a

Browse files
authored
feat(events-targets): Add tagging for ECS tasks triggered by an event (#23838)
This adds the ability to pass a tagList and the propagateTags flag to EC2 and Fargate ECS tasks triggered by an event. Users can leverage either or both of these attributes to apply tags an ECS task that's triggered through an Event. Both of these attributes are defined in the EcsParameters for a Rule https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-events-rule-ecsparameters.html. This closes #9823. This is a non-breaking change. These new fields are added as optional with default values such that it won't impact existing stacks. Also note that these are two separate fields: a user can pass either or both but they're not dependent on each other since they have different uses. `propagateTags` will copy tags from the task definition, while `tagList` provides an explicit set of tags to be applied to the task but that are not on the task definition. In either case, these tags are separate from the tags on the EventBridge bus or Rule. I did see that there was previously a PR opened for this in the past which was closed for staleness (#19583). There was a comment on there asking if this could be added to the existing tag system (#19583 (review)). I'm not sure if that would make sense in this case since the resource being created at deploy-time by CloudFormation isn't the resource being tagged. The event rule itself is instead being given the flag to pass along tags onto the Task that it's creating at some time in the future when the schedule triggers.I did see that there was previously a PR opened for this in the past which was closed for staleness (#19583). There was a comment on there asking if this could be added to the existing tag system (#19583 (review)). I'm not sure if that would make sense in this case since the resource being created at deploy-time by CloudFormation isn't the resource being tagged. The event rule itself is instead being given the flag to pass along tags onto the Task that it's creating at some time in the future when the schedule triggers. ---- ### All Submissions: * [x ] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Construct Runtime Dependencies: * [ ] This PR adds new construct runtime dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-construct-runtime-dependencies) ### New Features * [x ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [x ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 567247a commit e3bc59a

File tree

18 files changed

+433
-66
lines changed

18 files changed

+433
-66
lines changed
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"version":"21.0.0"}
1+
{"version":"29.0.0"}

packages/@aws-cdk-testing/framework-integ/test/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/EcsTestDefaultTestDeployAssert8B2741C4.assets.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "21.0.0",
2+
"version": "29.0.0",
33
"files": {
44
"21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": {
55
"source": {

packages/@aws-cdk-testing/framework-integ/test/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/aws-ecs-integ-ecs.assets.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
2-
"version": "21.0.0",
2+
"version": "29.0.0",
33
"files": {
4-
"0872557613b8f4b6c7ef173ce71b7a0f06895bacc709f34d5a62ffabcc0f5700": {
4+
"07b921a20dfe7af7de139cb4595c2f46d90f9625f61d13cff19dffca84918cd7": {
55
"source": {
66
"path": "aws-ecs-integ-ecs.template.json",
77
"packaging": "file"
88
},
99
"destinations": {
1010
"current_account-current_region": {
1111
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12-
"objectKey": "0872557613b8f4b6c7ef173ce71b7a0f06895bacc709f34d5a62ffabcc0f5700.json",
12+
"objectKey": "07b921a20dfe7af7de139cb4595c2f46d90f9625f61d13cff19dffca84918cd7.json",
1313
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
1414
}
1515
}

packages/@aws-cdk-testing/framework-integ/test/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/aws-ecs-integ-ecs.template.json

+7
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,13 @@
942942
}
943943
},
944944
"EcsParameters": {
945+
"PropagateTags": "TASK_DEFINITION",
946+
"TagList": [
947+
{
948+
"Key": "my-tag",
949+
"Value": "my-tag-value"
950+
}
951+
],
945952
"TaskCount": 1,
946953
"TaskDefinitionArn": {
947954
"Ref": "TaskDef54694570"

packages/@aws-cdk-testing/framework-integ/test/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/integ.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "21.0.0",
2+
"version": "29.0.0",
33
"testCases": {
44
"EcsTest/DefaultTest": {
55
"stacks": [

packages/@aws-cdk-testing/framework-integ/test/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/manifest.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
{
2-
"version": "21.0.0",
2+
"version": "29.0.0",
33
"artifacts": {
4-
"Tree": {
5-
"type": "cdk:tree",
6-
"properties": {
7-
"file": "tree.json"
8-
}
9-
},
104
"aws-ecs-integ-ecs.assets": {
115
"type": "cdk:asset-manifest",
126
"properties": {
@@ -23,7 +17,7 @@
2317
"validateOnSynth": false,
2418
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
2519
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
26-
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/0872557613b8f4b6c7ef173ce71b7a0f06895bacc709f34d5a62ffabcc0f5700.json",
20+
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/07b921a20dfe7af7de139cb4595c2f46d90f9625f61d13cff19dffca84918cd7.json",
2721
"requiresBootstrapStackVersion": 6,
2822
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
2923
"additionalDependencies": [
@@ -340,6 +334,12 @@
340334
]
341335
},
342336
"displayName": "EcsTest/DefaultTest/DeployAssert"
337+
},
338+
"Tree": {
339+
"type": "cdk:tree",
340+
"properties": {
341+
"file": "tree.json"
342+
}
343343
}
344344
}
345345
}

packages/@aws-cdk-testing/framework-integ/test/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/tree.json

+100-11
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@
44
"id": "App",
55
"path": "",
66
"children": {
7-
"Tree": {
8-
"id": "Tree",
9-
"path": "Tree",
10-
"constructInfo": {
11-
"fqn": "constructs.Construct",
12-
"version": "10.1.140"
13-
}
14-
},
157
"aws-ecs-integ-ecs": {
168
"id": "aws-ecs-integ-ecs",
179
"path": "aws-ecs-integ-ecs",
@@ -437,6 +429,14 @@
437429
"id": "InstanceRole",
438430
"path": "aws-ecs-integ-ecs/EcsCluster/DefaultAutoScalingGroup/InstanceRole",
439431
"children": {
432+
"ImportInstanceRole": {
433+
"id": "ImportInstanceRole",
434+
"path": "aws-ecs-integ-ecs/EcsCluster/DefaultAutoScalingGroup/InstanceRole/ImportInstanceRole",
435+
"constructInfo": {
436+
"fqn": "@aws-cdk/core.Resource",
437+
"version": "0.0.0"
438+
}
439+
},
440440
"Resource": {
441441
"id": "Resource",
442442
"path": "aws-ecs-integ-ecs/EcsCluster/DefaultAutoScalingGroup/InstanceRole/Resource",
@@ -652,6 +652,14 @@
652652
"id": "ServiceRole",
653653
"path": "aws-ecs-integ-ecs/EcsCluster/DefaultAutoScalingGroup/DrainECSHook/Function/ServiceRole",
654654
"children": {
655+
"ImportServiceRole": {
656+
"id": "ImportServiceRole",
657+
"path": "aws-ecs-integ-ecs/EcsCluster/DefaultAutoScalingGroup/DrainECSHook/Function/ServiceRole/ImportServiceRole",
658+
"constructInfo": {
659+
"fqn": "@aws-cdk/core.Resource",
660+
"version": "0.0.0"
661+
}
662+
},
655663
"Resource": {
656664
"id": "Resource",
657665
"path": "aws-ecs-integ-ecs/EcsCluster/DefaultAutoScalingGroup/DrainECSHook/Function/ServiceRole/Resource",
@@ -912,7 +920,7 @@
912920
},
913921
"constructInfo": {
914922
"fqn": "constructs.Construct",
915-
"version": "10.1.140"
923+
"version": "10.1.216"
916924
}
917925
},
918926
"LifecycleHookDrainHook": {
@@ -952,6 +960,14 @@
952960
"id": "Role",
953961
"path": "aws-ecs-integ-ecs/EcsCluster/DefaultAutoScalingGroup/LifecycleHookDrainHook/Role",
954962
"children": {
963+
"ImportRole": {
964+
"id": "ImportRole",
965+
"path": "aws-ecs-integ-ecs/EcsCluster/DefaultAutoScalingGroup/LifecycleHookDrainHook/Role/ImportRole",
966+
"constructInfo": {
967+
"fqn": "@aws-cdk/core.Resource",
968+
"version": "0.0.0"
969+
}
970+
},
955971
"Resource": {
956972
"id": "Resource",
957973
"path": "aws-ecs-integ-ecs/EcsCluster/DefaultAutoScalingGroup/LifecycleHookDrainHook/Role/Resource",
@@ -1179,6 +1195,14 @@
11791195
"id": "TaskRole",
11801196
"path": "aws-ecs-integ-ecs/TaskDef/TaskRole",
11811197
"children": {
1198+
"ImportTaskRole": {
1199+
"id": "ImportTaskRole",
1200+
"path": "aws-ecs-integ-ecs/TaskDef/TaskRole/ImportTaskRole",
1201+
"constructInfo": {
1202+
"fqn": "@aws-cdk/core.Resource",
1203+
"version": "0.0.0"
1204+
}
1205+
},
11821206
"Resource": {
11831207
"id": "Resource",
11841208
"path": "aws-ecs-integ-ecs/TaskDef/TaskRole/Resource",
@@ -1324,6 +1348,14 @@
13241348
"id": "ExecutionRole",
13251349
"path": "aws-ecs-integ-ecs/TaskDef/ExecutionRole",
13261350
"children": {
1351+
"ImportExecutionRole": {
1352+
"id": "ImportExecutionRole",
1353+
"path": "aws-ecs-integ-ecs/TaskDef/ExecutionRole/ImportExecutionRole",
1354+
"constructInfo": {
1355+
"fqn": "@aws-cdk/core.Resource",
1356+
"version": "0.0.0"
1357+
}
1358+
},
13271359
"Resource": {
13281360
"id": "Resource",
13291361
"path": "aws-ecs-integ-ecs/TaskDef/ExecutionRole/Resource",
@@ -1442,6 +1474,14 @@
14421474
"id": "EventsRole",
14431475
"path": "aws-ecs-integ-ecs/TaskDef/EventsRole",
14441476
"children": {
1477+
"ImportEventsRole": {
1478+
"id": "ImportEventsRole",
1479+
"path": "aws-ecs-integ-ecs/TaskDef/EventsRole/ImportEventsRole",
1480+
"constructInfo": {
1481+
"fqn": "@aws-cdk/core.Resource",
1482+
"version": "0.0.0"
1483+
}
1484+
},
14451485
"Resource": {
14461486
"id": "Resource",
14471487
"path": "aws-ecs-integ-ecs/TaskDef/EventsRole/Resource",
@@ -1571,7 +1611,14 @@
15711611
"taskCount": 1,
15721612
"taskDefinitionArn": {
15731613
"Ref": "TaskDef54694570"
1574-
}
1614+
},
1615+
"propagateTags": "TASK_DEFINITION",
1616+
"tagList": [
1617+
{
1618+
"key": "my-tag",
1619+
"value": "my-tag-value"
1620+
}
1621+
]
15751622
},
15761623
"deadLetterConfig": {
15771624
"arn": {
@@ -1596,6 +1643,22 @@
15961643
"fqn": "@aws-cdk/aws-events.Rule",
15971644
"version": "0.0.0"
15981645
}
1646+
},
1647+
"BootstrapVersion": {
1648+
"id": "BootstrapVersion",
1649+
"path": "aws-ecs-integ-ecs/BootstrapVersion",
1650+
"constructInfo": {
1651+
"fqn": "@aws-cdk/core.CfnParameter",
1652+
"version": "0.0.0"
1653+
}
1654+
},
1655+
"CheckBootstrapVersion": {
1656+
"id": "CheckBootstrapVersion",
1657+
"path": "aws-ecs-integ-ecs/CheckBootstrapVersion",
1658+
"constructInfo": {
1659+
"fqn": "@aws-cdk/core.CfnRule",
1660+
"version": "0.0.0"
1661+
}
15991662
}
16001663
},
16011664
"constructInfo": {
@@ -1616,12 +1679,30 @@
16161679
"path": "EcsTest/DefaultTest/Default",
16171680
"constructInfo": {
16181681
"fqn": "constructs.Construct",
1619-
"version": "10.1.140"
1682+
"version": "10.1.216"
16201683
}
16211684
},
16221685
"DeployAssert": {
16231686
"id": "DeployAssert",
16241687
"path": "EcsTest/DefaultTest/DeployAssert",
1688+
"children": {
1689+
"BootstrapVersion": {
1690+
"id": "BootstrapVersion",
1691+
"path": "EcsTest/DefaultTest/DeployAssert/BootstrapVersion",
1692+
"constructInfo": {
1693+
"fqn": "@aws-cdk/core.CfnParameter",
1694+
"version": "0.0.0"
1695+
}
1696+
},
1697+
"CheckBootstrapVersion": {
1698+
"id": "CheckBootstrapVersion",
1699+
"path": "EcsTest/DefaultTest/DeployAssert/CheckBootstrapVersion",
1700+
"constructInfo": {
1701+
"fqn": "@aws-cdk/core.CfnRule",
1702+
"version": "0.0.0"
1703+
}
1704+
}
1705+
},
16251706
"constructInfo": {
16261707
"fqn": "@aws-cdk/core.Stack",
16271708
"version": "0.0.0"
@@ -1638,6 +1719,14 @@
16381719
"fqn": "@aws-cdk/integ-tests.IntegTest",
16391720
"version": "0.0.0"
16401721
}
1722+
},
1723+
"Tree": {
1724+
"id": "Tree",
1725+
"path": "Tree",
1726+
"constructInfo": {
1727+
"fqn": "constructs.Construct",
1728+
"version": "10.1.216"
1729+
}
16411730
}
16421731
},
16431732
"constructInfo": {

packages/@aws-cdk-testing/framework-integ/test/aws-events-targets/test/ecs/integ.event-ec2-task.ts

+7
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ rule.addTarget(new targets.EcsTask({
4545
],
4646
}],
4747
deadLetterQueue,
48+
propagateTags: ecs.PropagatedTagSource.TASK_DEFINITION,
49+
tags: [
50+
{
51+
key: 'my-tag',
52+
value: 'my-tag-value',
53+
},
54+
],
4855
}));
4956

5057
new integ.IntegTest(app, 'EcsTest', {

packages/@aws-cdk-testing/framework-integ/test/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/EcsFargateTestDefaultTestDeployAssert36341BFB.assets.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "20.0.0",
2+
"version": "29.0.0",
33
"files": {
44
"21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": {
55
"source": {

packages/@aws-cdk-testing/framework-integ/test/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/aws-ecs-integ-fargate.assets.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
2-
"version": "20.0.0",
2+
"version": "29.0.0",
33
"files": {
4-
"21e73448eb441396f1651456a83004f72f1f50583a8c6e054fc95fc99118d943": {
4+
"30cd2908bd974e2e1f17ce2fd217f78c8377a301b45a4b7a471e00c77fdff513": {
55
"source": {
66
"path": "aws-ecs-integ-fargate.template.json",
77
"packaging": "file"
88
},
99
"destinations": {
1010
"current_account-current_region": {
1111
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12-
"objectKey": "21e73448eb441396f1651456a83004f72f1f50583a8c6e054fc95fc99118d943.json",
12+
"objectKey": "30cd2908bd974e2e1f17ce2fd217f78c8377a301b45a4b7a471e00c77fdff513.json",
1313
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
1414
}
1515
}

packages/@aws-cdk-testing/framework-integ/test/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/aws-ecs-integ-fargate.template.json

+7
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,13 @@
535535
]
536536
}
537537
},
538+
"PropagateTags": "TASK_DEFINITION",
539+
"TagList": [
540+
{
541+
"Key": "my-tag",
542+
"Value": "my-tag-value"
543+
}
544+
],
538545
"TaskCount": 1,
539546
"TaskDefinitionArn": {
540547
"Ref": "TaskDef54694570"
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
2-
"version": "20.0.0",
2+
"version": "29.0.0",
33
"testCases": {
44
"EcsFargateTest/DefaultTest": {
55
"stacks": [
66
"aws-ecs-integ-fargate"
77
],
8-
"assertionStack": "EcsFargateTest/DefaultTest/DeployAssert"
8+
"assertionStack": "EcsFargateTest/DefaultTest/DeployAssert",
9+
"assertionStackName": "EcsFargateTestDefaultTestDeployAssert36341BFB"
910
}
1011
}
1112
}

0 commit comments

Comments
 (0)