From 680dcc5cab70631e57a9c97ddaaf3afe567d31fa Mon Sep 17 00:00:00 2001 From: Ryan Batchelder Date: Wed, 25 Jan 2023 14:58:11 -0500 Subject: [PATCH 1/6] Add the ability to propagate tags on ECS tasks triggered by a scheduled event Add the ability to set tags for ECS tasks triggered by a scheduled event --- .../aws-events-targets/lib/ecs-task.ts | 39 +++++- .../test/ecs/event-rule-target.test.ts | 82 ++++++++++++- ...efaultTestDeployAssert8B2741C4.assets.json | 2 +- .../aws-ecs-integ-ecs.assets.json | 6 +- .../aws-ecs-integ-ecs.template.json | 7 ++ .../integ.event-ec2-task.js.snapshot/cdk.out | 2 +- .../integ.json | 2 +- .../manifest.json | 16 +-- .../tree.json | 111 ++++++++++++++++-- .../test/ecs/integ.event-ec2-task.ts | 7 ++ ...efaultTestDeployAssert36341BFB.assets.json | 2 +- .../aws-ecs-integ-fargate.assets.json | 6 +- .../aws-ecs-integ-fargate.template.json | 7 ++ .../cdk.out | 2 +- .../integ.json | 5 +- .../manifest.json | 21 ++-- .../tree.json | 107 +++++++++++++---- .../test/ecs/integ.event-fargate-task.ts | 7 ++ 18 files changed, 364 insertions(+), 67 deletions(-) diff --git a/packages/@aws-cdk/aws-events-targets/lib/ecs-task.ts b/packages/@aws-cdk/aws-events-targets/lib/ecs-task.ts index c4736c0acb338..2bbd119a1e4ab 100644 --- a/packages/@aws-cdk/aws-events-targets/lib/ecs-task.ts +++ b/packages/@aws-cdk/aws-events-targets/lib/ecs-task.ts @@ -7,6 +7,20 @@ import { Construct } from 'constructs'; import { ContainerOverride } from './ecs-task-properties'; import { addToDeadLetterQueueResourcePolicy, bindBaseTargetConfig, singletonEventRole, TargetBaseProps } from './util'; +/** + * Tag + */ +export interface Tag { + + /** + * key to e tagged + */ + readonly key: string; + /** + * additional value + */ + readonly value: string; +} /** * Properties to define an ECS Event Task */ @@ -81,6 +95,20 @@ export interface EcsTaskProps extends TargetBaseProps { * @default - ECS will set the Fargate platform version to 'LATEST' */ readonly platformVersion?: ecs.FargatePlatformVersion; + + /** + * Specifies whether to propagate the tags from the task definition to the task. If no value is specified, the tags are not propagated. + * + * @default - Tags will not be propagated + */ + readonly propagateTags?: boolean + + /** + * The metadata that you apply to the task to help you categorize and organize them. Each tag consists of a key and an optional value, both of which you define. + * + * @default - No tags are applied to the task + */ + readonly tagList?: Tag[] } /** @@ -108,6 +136,8 @@ export class EcsTask implements events.IRuleTarget { private readonly taskCount: number; private readonly role: iam.IRole; private readonly platformVersion?: ecs.FargatePlatformVersion; + private readonly propagateTags?: ecs.PropagatedTagSource; + private readonly tagList?: Tag[] constructor(private readonly props: EcsTaskProps) { if (props.securityGroup !== undefined && props.securityGroups !== undefined) { @@ -118,12 +148,17 @@ export class EcsTask implements events.IRuleTarget { this.taskDefinition = props.taskDefinition; this.taskCount = props.taskCount ?? 1; this.platformVersion = props.platformVersion; + this.propagateTags = props.propagateTags === true ? ecs.PropagatedTagSource.TASK_DEFINITION : undefined ; this.role = props.role ?? singletonEventRole(this.taskDefinition); for (const stmt of this.createEventRolePolicyStatements()) { this.role.addToPrincipalPolicy(stmt); } + if (props.tagList) { + this.tagList = props.tagList; + } + // Security groups are only configurable with the "awsvpc" network mode. if (this.taskDefinition.networkMode !== ecs.NetworkMode.AWS_VPC) { if (props.securityGroup !== undefined || props.securityGroups !== undefined) { @@ -159,11 +194,13 @@ export class EcsTask implements events.IRuleTarget { const input = { containerOverrides }; const taskCount = this.taskCount; const taskDefinitionArn = this.taskDefinition.taskDefinitionArn; + const propagateTags = this.propagateTags; + const tagList = this.tagList; const subnetSelection = this.props.subnetSelection || { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS }; const assignPublicIp = subnetSelection.subnetType === ec2.SubnetType.PUBLIC ? 'ENABLED' : 'DISABLED'; - const baseEcsParameters = { taskCount, taskDefinitionArn }; + const baseEcsParameters = { taskCount, taskDefinitionArn, propagateTags, tagList }; const ecsParameters: events.CfnRule.EcsParametersProperty = this.taskDefinition.networkMode === ecs.NetworkMode.AWS_VPC ? { diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/event-rule-target.test.ts b/packages/@aws-cdk/aws-events-targets/test/ecs/event-rule-target.test.ts index c8c48d8821e0d..8fbdf78158668 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/event-rule-target.test.ts +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/event-rule-target.test.ts @@ -1,4 +1,4 @@ -import { Template } from '@aws-cdk/assertions'; +import { Match, Template } from '@aws-cdk/assertions'; import * as autoscaling from '@aws-cdk/aws-autoscaling'; import * as ec2 from '@aws-cdk/aws-ec2'; import * as ecs from '@aws-cdk/aws-ecs'; @@ -778,3 +778,83 @@ test('uses the specific fargate platform version', () => { ], }); }); + +test('sets the propagate tags flag', () => { + // GIVEN + const taskDefinition = new ecs.FargateTaskDefinition(stack, 'TaskDef'); + taskDefinition.addContainer('TheContainer', { + image: ecs.ContainerImage.fromRegistry('henk'), + }); + + const rule = new events.Rule(stack, 'Rule', { + schedule: events.Schedule.expression('rate(1 min)'), + }); + + // WHEN + rule.addTarget(new targets.EcsTask({ + cluster, + taskDefinition, + taskCount: 1, + containerOverrides: [{ + containerName: 'TheContainer', + command: ['echo', events.EventField.fromPath('$.detail.event')], + }], + propagateTags: true, + })); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::Events::Rule', { + Targets: [ + Match.objectLike({ + EcsParameters: Match.objectLike({ + PropagateTags: ecs.PropagatedTagSource.TASK_DEFINITION, + }), + }), + ], + }); +}); + +test('sets tag lists', () => { + // GIVEN + const taskDefinition = new ecs.FargateTaskDefinition(stack, 'TaskDef'); + taskDefinition.addContainer('TheContainer', { + image: ecs.ContainerImage.fromRegistry('henk'), + }); + + const rule = new events.Rule(stack, 'Rule', { + schedule: events.Schedule.expression('rate(1 min)'), + }); + + // WHEN + rule.addTarget(new targets.EcsTask({ + cluster, + taskDefinition, + taskCount: 1, + containerOverrides: [{ + containerName: 'TheContainer', + command: ['echo', events.EventField.fromPath('$.detail.event')], + }], + tagList: [ + { + key: 'my-tag', + value: 'my-tag-value', + }, + ], + })); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::Events::Rule', { + Targets: [ + Match.objectLike({ + EcsParameters: Match.objectLike({ + TagList: [ + { + Key: 'my-tag', + Value: 'my-tag-value', + }, + ], + }), + }), + ], + }); +}); diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/EcsTestDefaultTestDeployAssert8B2741C4.assets.json b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/EcsTestDefaultTestDeployAssert8B2741C4.assets.json index 3f1f9a15a2698..24f9c5f3d4cf1 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/EcsTestDefaultTestDeployAssert8B2741C4.assets.json +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/EcsTestDefaultTestDeployAssert8B2741C4.assets.json @@ -1,5 +1,5 @@ { - "version": "21.0.0", + "version": "29.0.0", "files": { "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { "source": { diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/aws-ecs-integ-ecs.assets.json b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/aws-ecs-integ-ecs.assets.json index 96fcd217c5d52..43973cfbde2ce 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/aws-ecs-integ-ecs.assets.json +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/aws-ecs-integ-ecs.assets.json @@ -1,7 +1,7 @@ { - "version": "21.0.0", + "version": "29.0.0", "files": { - "0872557613b8f4b6c7ef173ce71b7a0f06895bacc709f34d5a62ffabcc0f5700": { + "07b921a20dfe7af7de139cb4595c2f46d90f9625f61d13cff19dffca84918cd7": { "source": { "path": "aws-ecs-integ-ecs.template.json", "packaging": "file" @@ -9,7 +9,7 @@ "destinations": { "current_account-current_region": { "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", - "objectKey": "0872557613b8f4b6c7ef173ce71b7a0f06895bacc709f34d5a62ffabcc0f5700.json", + "objectKey": "07b921a20dfe7af7de139cb4595c2f46d90f9625f61d13cff19dffca84918cd7.json", "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" } } diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/aws-ecs-integ-ecs.template.json b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/aws-ecs-integ-ecs.template.json index 5c9ce38c39ba3..38ee6a82f1477 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/aws-ecs-integ-ecs.template.json +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/aws-ecs-integ-ecs.template.json @@ -942,6 +942,13 @@ } }, "EcsParameters": { + "PropagateTags": "TASK_DEFINITION", + "TagList": [ + { + "Key": "my-tag", + "Value": "my-tag-value" + } + ], "TaskCount": 1, "TaskDefinitionArn": { "Ref": "TaskDef54694570" diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/cdk.out b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/cdk.out index 588d7b269d34f..d8b441d447f8a 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/cdk.out +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/cdk.out @@ -1 +1 @@ -{"version":"20.0.0"} \ No newline at end of file +{"version":"29.0.0"} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/integ.json b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/integ.json index 03733012df53e..34309bf0f9864 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/integ.json +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/integ.json @@ -1,5 +1,5 @@ { - "version": "21.0.0", + "version": "29.0.0", "testCases": { "EcsTest/DefaultTest": { "stacks": [ diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/manifest.json b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/manifest.json index 47826f5c69888..83e9d3a754ee3 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/manifest.json +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/manifest.json @@ -1,12 +1,6 @@ { - "version": "21.0.0", + "version": "29.0.0", "artifacts": { - "Tree": { - "type": "cdk:tree", - "properties": { - "file": "tree.json" - } - }, "aws-ecs-integ-ecs.assets": { "type": "cdk:asset-manifest", "properties": { @@ -23,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}/0872557613b8f4b6c7ef173ce71b7a0f06895bacc709f34d5a62ffabcc0f5700.json", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/07b921a20dfe7af7de139cb4595c2f46d90f9625f61d13cff19dffca84918cd7.json", "requiresBootstrapStackVersion": 6, "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", "additionalDependencies": [ @@ -340,6 +334,12 @@ ] }, "displayName": "EcsTest/DefaultTest/DeployAssert" + }, + "Tree": { + "type": "cdk:tree", + "properties": { + "file": "tree.json" + } } } } \ No newline at end of file diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/tree.json b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/tree.json index 37a5409d87188..c9f0698ba2b68 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/tree.json +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.js.snapshot/tree.json @@ -4,14 +4,6 @@ "id": "App", "path": "", "children": { - "Tree": { - "id": "Tree", - "path": "Tree", - "constructInfo": { - "fqn": "constructs.Construct", - "version": "10.1.140" - } - }, "aws-ecs-integ-ecs": { "id": "aws-ecs-integ-ecs", "path": "aws-ecs-integ-ecs", @@ -437,6 +429,14 @@ "id": "InstanceRole", "path": "aws-ecs-integ-ecs/EcsCluster/DefaultAutoScalingGroup/InstanceRole", "children": { + "ImportInstanceRole": { + "id": "ImportInstanceRole", + "path": "aws-ecs-integ-ecs/EcsCluster/DefaultAutoScalingGroup/InstanceRole/ImportInstanceRole", + "constructInfo": { + "fqn": "@aws-cdk/core.Resource", + "version": "0.0.0" + } + }, "Resource": { "id": "Resource", "path": "aws-ecs-integ-ecs/EcsCluster/DefaultAutoScalingGroup/InstanceRole/Resource", @@ -652,6 +652,14 @@ "id": "ServiceRole", "path": "aws-ecs-integ-ecs/EcsCluster/DefaultAutoScalingGroup/DrainECSHook/Function/ServiceRole", "children": { + "ImportServiceRole": { + "id": "ImportServiceRole", + "path": "aws-ecs-integ-ecs/EcsCluster/DefaultAutoScalingGroup/DrainECSHook/Function/ServiceRole/ImportServiceRole", + "constructInfo": { + "fqn": "@aws-cdk/core.Resource", + "version": "0.0.0" + } + }, "Resource": { "id": "Resource", "path": "aws-ecs-integ-ecs/EcsCluster/DefaultAutoScalingGroup/DrainECSHook/Function/ServiceRole/Resource", @@ -912,7 +920,7 @@ }, "constructInfo": { "fqn": "constructs.Construct", - "version": "10.1.140" + "version": "10.1.216" } }, "LifecycleHookDrainHook": { @@ -952,6 +960,14 @@ "id": "Role", "path": "aws-ecs-integ-ecs/EcsCluster/DefaultAutoScalingGroup/LifecycleHookDrainHook/Role", "children": { + "ImportRole": { + "id": "ImportRole", + "path": "aws-ecs-integ-ecs/EcsCluster/DefaultAutoScalingGroup/LifecycleHookDrainHook/Role/ImportRole", + "constructInfo": { + "fqn": "@aws-cdk/core.Resource", + "version": "0.0.0" + } + }, "Resource": { "id": "Resource", "path": "aws-ecs-integ-ecs/EcsCluster/DefaultAutoScalingGroup/LifecycleHookDrainHook/Role/Resource", @@ -1179,6 +1195,14 @@ "id": "TaskRole", "path": "aws-ecs-integ-ecs/TaskDef/TaskRole", "children": { + "ImportTaskRole": { + "id": "ImportTaskRole", + "path": "aws-ecs-integ-ecs/TaskDef/TaskRole/ImportTaskRole", + "constructInfo": { + "fqn": "@aws-cdk/core.Resource", + "version": "0.0.0" + } + }, "Resource": { "id": "Resource", "path": "aws-ecs-integ-ecs/TaskDef/TaskRole/Resource", @@ -1324,6 +1348,14 @@ "id": "ExecutionRole", "path": "aws-ecs-integ-ecs/TaskDef/ExecutionRole", "children": { + "ImportExecutionRole": { + "id": "ImportExecutionRole", + "path": "aws-ecs-integ-ecs/TaskDef/ExecutionRole/ImportExecutionRole", + "constructInfo": { + "fqn": "@aws-cdk/core.Resource", + "version": "0.0.0" + } + }, "Resource": { "id": "Resource", "path": "aws-ecs-integ-ecs/TaskDef/ExecutionRole/Resource", @@ -1442,6 +1474,14 @@ "id": "EventsRole", "path": "aws-ecs-integ-ecs/TaskDef/EventsRole", "children": { + "ImportEventsRole": { + "id": "ImportEventsRole", + "path": "aws-ecs-integ-ecs/TaskDef/EventsRole/ImportEventsRole", + "constructInfo": { + "fqn": "@aws-cdk/core.Resource", + "version": "0.0.0" + } + }, "Resource": { "id": "Resource", "path": "aws-ecs-integ-ecs/TaskDef/EventsRole/Resource", @@ -1571,7 +1611,14 @@ "taskCount": 1, "taskDefinitionArn": { "Ref": "TaskDef54694570" - } + }, + "propagateTags": "TASK_DEFINITION", + "tagList": [ + { + "key": "my-tag", + "value": "my-tag-value" + } + ] }, "deadLetterConfig": { "arn": { @@ -1596,6 +1643,22 @@ "fqn": "@aws-cdk/aws-events.Rule", "version": "0.0.0" } + }, + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "aws-ecs-integ-ecs/BootstrapVersion", + "constructInfo": { + "fqn": "@aws-cdk/core.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "aws-ecs-integ-ecs/CheckBootstrapVersion", + "constructInfo": { + "fqn": "@aws-cdk/core.CfnRule", + "version": "0.0.0" + } } }, "constructInfo": { @@ -1616,12 +1679,30 @@ "path": "EcsTest/DefaultTest/Default", "constructInfo": { "fqn": "constructs.Construct", - "version": "10.1.140" + "version": "10.1.216" } }, "DeployAssert": { "id": "DeployAssert", "path": "EcsTest/DefaultTest/DeployAssert", + "children": { + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "EcsTest/DefaultTest/DeployAssert/BootstrapVersion", + "constructInfo": { + "fqn": "@aws-cdk/core.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "EcsTest/DefaultTest/DeployAssert/CheckBootstrapVersion", + "constructInfo": { + "fqn": "@aws-cdk/core.CfnRule", + "version": "0.0.0" + } + } + }, "constructInfo": { "fqn": "@aws-cdk/core.Stack", "version": "0.0.0" @@ -1638,6 +1719,14 @@ "fqn": "@aws-cdk/integ-tests.IntegTest", "version": "0.0.0" } + }, + "Tree": { + "id": "Tree", + "path": "Tree", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.1.216" + } } }, "constructInfo": { diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.ts b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.ts index f387c57cb7caf..3ea37989f5eac 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.ts +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.ts @@ -45,6 +45,13 @@ rule.addTarget(new targets.EcsTask({ ], }], deadLetterQueue, + propagateTags: true, + tagList: [ + { + key: 'my-tag', + value: 'my-tag-value', + }, + ], })); new integ.IntegTest(app, 'EcsTest', { diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/EcsFargateTestDefaultTestDeployAssert36341BFB.assets.json b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/EcsFargateTestDefaultTestDeployAssert36341BFB.assets.json index e92ac72eede43..8b8277844242b 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/EcsFargateTestDefaultTestDeployAssert36341BFB.assets.json +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/EcsFargateTestDefaultTestDeployAssert36341BFB.assets.json @@ -1,5 +1,5 @@ { - "version": "20.0.0", + "version": "29.0.0", "files": { "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { "source": { diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/aws-ecs-integ-fargate.assets.json b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/aws-ecs-integ-fargate.assets.json index db5d2093cfca5..1e6aabf49fca3 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/aws-ecs-integ-fargate.assets.json +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/aws-ecs-integ-fargate.assets.json @@ -1,7 +1,7 @@ { - "version": "20.0.0", + "version": "29.0.0", "files": { - "21e73448eb441396f1651456a83004f72f1f50583a8c6e054fc95fc99118d943": { + "30cd2908bd974e2e1f17ce2fd217f78c8377a301b45a4b7a471e00c77fdff513": { "source": { "path": "aws-ecs-integ-fargate.template.json", "packaging": "file" @@ -9,7 +9,7 @@ "destinations": { "current_account-current_region": { "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", - "objectKey": "21e73448eb441396f1651456a83004f72f1f50583a8c6e054fc95fc99118d943.json", + "objectKey": "30cd2908bd974e2e1f17ce2fd217f78c8377a301b45a4b7a471e00c77fdff513.json", "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" } } diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/aws-ecs-integ-fargate.template.json b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/aws-ecs-integ-fargate.template.json index 0b0870dfece7a..164d932674e9f 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/aws-ecs-integ-fargate.template.json +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/aws-ecs-integ-fargate.template.json @@ -535,6 +535,13 @@ ] } }, + "PropagateTags": "TASK_DEFINITION", + "TagList": [ + { + "Key": "my-tag", + "Value": "my-tag-value" + } + ], "TaskCount": 1, "TaskDefinitionArn": { "Ref": "TaskDef54694570" diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/cdk.out b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/cdk.out index 8ecc185e9dbee..d8b441d447f8a 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/cdk.out +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/cdk.out @@ -1 +1 @@ -{"version":"21.0.0"} \ No newline at end of file +{"version":"29.0.0"} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/integ.json b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/integ.json index bcf64cee1d1b5..0602053494e16 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/integ.json +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/integ.json @@ -1,11 +1,12 @@ { - "version": "20.0.0", + "version": "29.0.0", "testCases": { "EcsFargateTest/DefaultTest": { "stacks": [ "aws-ecs-integ-fargate" ], - "assertionStack": "EcsFargateTest/DefaultTest/DeployAssert" + "assertionStack": "EcsFargateTest/DefaultTest/DeployAssert", + "assertionStackName": "EcsFargateTestDefaultTestDeployAssert36341BFB" } } } \ No newline at end of file diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/manifest.json b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/manifest.json index eedaea2fc2e20..ff1ef8c7311e4 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/manifest.json +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/manifest.json @@ -1,12 +1,6 @@ { - "version": "20.0.0", + "version": "29.0.0", "artifacts": { - "Tree": { - "type": "cdk:tree", - "properties": { - "file": "tree.json" - } - }, "aws-ecs-integ-fargate.assets": { "type": "cdk:asset-manifest", "properties": { @@ -23,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}/21e73448eb441396f1651456a83004f72f1f50583a8c6e054fc95fc99118d943.json", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/30cd2908bd974e2e1f17ce2fd217f78c8377a301b45a4b7a471e00c77fdff513.json", "requiresBootstrapStackVersion": 6, "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", "additionalDependencies": [ @@ -144,10 +138,7 @@ "/aws-ecs-integ-fargate/TaskDef/Resource": [ { "type": "aws:cdk:logicalId", - "data": "TaskDef54694570", - "trace": [ - "!!DESTRUCTIVE_CHANGES: WILL_REPLACE" - ] + "data": "TaskDef54694570" } ], "/aws-ecs-integ-fargate/TaskDef/TheContainer/LogGroup/Resource": [ @@ -253,6 +244,12 @@ ] }, "displayName": "EcsFargateTest/DefaultTest/DeployAssert" + }, + "Tree": { + "type": "cdk:tree", + "properties": { + "file": "tree.json" + } } } } \ No newline at end of file diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/tree.json b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/tree.json index bc82dac404bd5..6acf636748337 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/tree.json +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.js.snapshot/tree.json @@ -4,14 +4,6 @@ "id": "App", "path": "", "children": { - "Tree": { - "id": "Tree", - "path": "Tree", - "constructInfo": { - "fqn": "constructs.Construct", - "version": "10.1.85" - } - }, "aws-ecs-integ-fargate": { "id": "aws-ecs-integ-fargate", "path": "aws-ecs-integ-fargate", @@ -91,8 +83,8 @@ "id": "Acl", "path": "aws-ecs-integ-fargate/Vpc/PublicSubnet1/Acl", "constructInfo": { - "fqn": "constructs.Construct", - "version": "10.1.85" + "fqn": "@aws-cdk/core.Resource", + "version": "0.0.0" } }, "RouteTable": { @@ -258,8 +250,8 @@ "id": "Acl", "path": "aws-ecs-integ-fargate/Vpc/PrivateSubnet1/Acl", "constructInfo": { - "fqn": "constructs.Construct", - "version": "10.1.85" + "fqn": "@aws-cdk/core.Resource", + "version": "0.0.0" } }, "RouteTable": { @@ -482,6 +474,14 @@ "id": "TaskRole", "path": "aws-ecs-integ-fargate/TaskDef/TaskRole", "children": { + "ImportTaskRole": { + "id": "ImportTaskRole", + "path": "aws-ecs-integ-fargate/TaskDef/TaskRole/ImportTaskRole", + "constructInfo": { + "fqn": "@aws-cdk/core.Resource", + "version": "0.0.0" + } + }, "Resource": { "id": "Resource", "path": "aws-ecs-integ-fargate/TaskDef/TaskRole/Resource", @@ -578,8 +578,8 @@ "id": "Staging", "path": "aws-ecs-integ-fargate/TaskDef/TheContainer/AssetImage/Staging", "constructInfo": { - "fqn": "constructs.Construct", - "version": "10.1.85" + "fqn": "@aws-cdk/core.AssetStaging", + "version": "0.0.0" } }, "Repository": { @@ -628,6 +628,14 @@ "id": "ExecutionRole", "path": "aws-ecs-integ-fargate/TaskDef/ExecutionRole", "children": { + "ImportExecutionRole": { + "id": "ImportExecutionRole", + "path": "aws-ecs-integ-fargate/TaskDef/ExecutionRole/ImportExecutionRole", + "constructInfo": { + "fqn": "@aws-cdk/core.Resource", + "version": "0.0.0" + } + }, "Resource": { "id": "Resource", "path": "aws-ecs-integ-fargate/TaskDef/ExecutionRole/Resource", @@ -746,6 +754,14 @@ "id": "EventsRole", "path": "aws-ecs-integ-fargate/TaskDef/EventsRole", "children": { + "ImportEventsRole": { + "id": "ImportEventsRole", + "path": "aws-ecs-integ-fargate/TaskDef/EventsRole/ImportEventsRole", + "constructInfo": { + "fqn": "@aws-cdk/core.Resource", + "version": "0.0.0" + } + }, "Resource": { "id": "Resource", "path": "aws-ecs-integ-fargate/TaskDef/EventsRole/Resource", @@ -918,6 +934,13 @@ "taskDefinitionArn": { "Ref": "TaskDef54694570" }, + "propagateTags": "TASK_DEFINITION", + "tagList": [ + { + "key": "my-tag", + "value": "my-tag-value" + } + ], "launchType": "FARGATE", "networkConfiguration": { "awsVpcConfiguration": { @@ -961,11 +984,27 @@ "fqn": "@aws-cdk/aws-events.Rule", "version": "0.0.0" } + }, + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "aws-ecs-integ-fargate/BootstrapVersion", + "constructInfo": { + "fqn": "@aws-cdk/core.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "aws-ecs-integ-fargate/CheckBootstrapVersion", + "constructInfo": { + "fqn": "@aws-cdk/core.CfnRule", + "version": "0.0.0" + } } }, "constructInfo": { - "fqn": "constructs.Construct", - "version": "10.1.85" + "fqn": "@aws-cdk/core.Stack", + "version": "0.0.0" } }, "EcsFargateTest": { @@ -981,15 +1020,33 @@ "path": "EcsFargateTest/DefaultTest/Default", "constructInfo": { "fqn": "constructs.Construct", - "version": "10.1.85" + "version": "10.1.216" } }, "DeployAssert": { "id": "DeployAssert", "path": "EcsFargateTest/DefaultTest/DeployAssert", + "children": { + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "EcsFargateTest/DefaultTest/DeployAssert/BootstrapVersion", + "constructInfo": { + "fqn": "@aws-cdk/core.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "EcsFargateTest/DefaultTest/DeployAssert/CheckBootstrapVersion", + "constructInfo": { + "fqn": "@aws-cdk/core.CfnRule", + "version": "0.0.0" + } + } + }, "constructInfo": { - "fqn": "constructs.Construct", - "version": "10.1.85" + "fqn": "@aws-cdk/core.Stack", + "version": "0.0.0" } } }, @@ -1003,11 +1060,19 @@ "fqn": "@aws-cdk/integ-tests.IntegTest", "version": "0.0.0" } + }, + "Tree": { + "id": "Tree", + "path": "Tree", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.1.216" + } } }, "constructInfo": { - "fqn": "constructs.Construct", - "version": "10.1.85" + "fqn": "@aws-cdk/core.App", + "version": "0.0.0" } } } \ No newline at end of file diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.ts b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.ts index c1f0c9b93aa0f..2b717035b16ea 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.ts +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.ts @@ -41,6 +41,13 @@ rule.addTarget(new targets.EcsTask({ ], }], deadLetterQueue, + propagateTags: true, + tagList: [ + { + key: 'my-tag', + value: 'my-tag-value', + }, + ], })); new integ.IntegTest(app, 'EcsFargateTest', { From 66dc94ecf7eaebd8d7f51bd7e0f7a56a38644cde Mon Sep 17 00:00:00 2001 From: Ryan Batchelder Date: Wed, 25 Jan 2023 16:00:01 -0500 Subject: [PATCH 2/6] Added README snippet for triggering an ECS task from an Event Fixed a few minor issues on existing README entries that Rosetta threw errors on --- .../@aws-cdk/aws-events-targets/README.md | 40 ++++++++++++++++--- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/packages/@aws-cdk/aws-events-targets/README.md b/packages/@aws-cdk/aws-events-targets/README.md index 7dc1c9116b720..488e1dce8ad49 100644 --- a/packages/@aws-cdk/aws-events-targets/README.md +++ b/packages/@aws-cdk/aws-events-targets/README.md @@ -103,9 +103,9 @@ declare const logGroup: logs.LogGroup; declare const rule: events.Rule; rule.addTarget(new targets.CloudWatchLogGroup(logGroup, { - logEvent: targets.LogGroupTargetInput({ - timestamp: events.EventField.from('$.time'), - message: events.EventField.from('$.detail-type'), + logEvent: targets.LogGroupTargetInput.fromObject({ + timestamp: events.EventField.time, + message: events.EventField.detailType, }), })); ``` @@ -119,7 +119,7 @@ declare const logGroup: logs.LogGroup; declare const rule: events.Rule; rule.addTarget(new targets.CloudWatchLogGroup(logGroup, { - logEvent: targets.LogGroupTargetInput({ + logEvent: targets.LogGroupTargetInput.fromObject({ message: JSON.stringify({ CustomField: 'CustomValue', }), @@ -321,7 +321,7 @@ const destination = new events.ApiDestination(this, 'Destination', { }); const rule = new events.Rule(this, 'Rule', { - schedule: events.Schedule.rate(cdk.Duration.minutes(1)), + schedule: events.Schedule.rate(cdk.Duration.hours(1)), targets: [new targets.ApiDestination(destination)], }); ``` @@ -345,3 +345,33 @@ rule.addTarget(new targets.EventBus( ), )); ``` + +## Run an ECS Task + +Use the `EcsTask` target to run an ECS Task. + +The code snippet below creates a scheduled event rule that will run the task described in `taskDefinition` every hour. + +```ts +import * as ecs from "@aws-cdk/aws-ecs" +declare const cluster: ecs.ICluster +declare const taskDefinition: ecs.TaskDefinition + +const rule = new events.Rule(this, 'Rule', { + schedule: events.Schedule.rate(cdk.Duration.hours(1)), +}); + +rule.addTarget( + new targets.EcsTask( { + cluster: cluster, + taskDefinition: taskDefinition, + propagateTags: true, + tagList: [ + { + key: 'my-tag', + value: 'my-tag-value', + }, + ], + }) +); +``` From 20b1ddc2ec0642863492259b13a82a778542b504 Mon Sep 17 00:00:00 2001 From: Ryan Batchelder Date: Wed, 25 Jan 2023 16:26:18 -0500 Subject: [PATCH 3/6] Remove README cleanups that aren't required for Rosetta, to be re-added in a separate chore PR later --- packages/@aws-cdk/aws-events-targets/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-events-targets/README.md b/packages/@aws-cdk/aws-events-targets/README.md index 488e1dce8ad49..619c243403717 100644 --- a/packages/@aws-cdk/aws-events-targets/README.md +++ b/packages/@aws-cdk/aws-events-targets/README.md @@ -321,7 +321,7 @@ const destination = new events.ApiDestination(this, 'Destination', { }); const rule = new events.Rule(this, 'Rule', { - schedule: events.Schedule.rate(cdk.Duration.hours(1)), + schedule: events.Schedule.rate(cdk.Duration.minutes(1)), targets: [new targets.ApiDestination(destination)], }); ``` From 7230673719bdd21515e19fd7b12d36a454180d0e Mon Sep 17 00:00:00 2001 From: Ryan Batchelder Date: Thu, 26 Jan 2023 09:30:21 -0500 Subject: [PATCH 4/6] Update propagateTags to take the enum of valid values instead of a boolean to avoid breaking changes should the allowed values change in the future --- .../@aws-cdk/aws-events-targets/README.md | 2 +- .../aws-events-targets/lib/ecs-task.ts | 9 ++++-- .../test/ecs/event-rule-target.test.ts | 28 ++++++++++++++++++- .../test/ecs/integ.event-ec2-task.ts | 2 +- .../test/ecs/integ.event-fargate-task.ts | 2 +- 5 files changed, 37 insertions(+), 6 deletions(-) diff --git a/packages/@aws-cdk/aws-events-targets/README.md b/packages/@aws-cdk/aws-events-targets/README.md index 619c243403717..c4935785504c5 100644 --- a/packages/@aws-cdk/aws-events-targets/README.md +++ b/packages/@aws-cdk/aws-events-targets/README.md @@ -365,7 +365,7 @@ rule.addTarget( new targets.EcsTask( { cluster: cluster, taskDefinition: taskDefinition, - propagateTags: true, + propagateTags: ecs.PropagatedTagSource.TASK_DEFINITION, tagList: [ { key: 'my-tag', diff --git a/packages/@aws-cdk/aws-events-targets/lib/ecs-task.ts b/packages/@aws-cdk/aws-events-targets/lib/ecs-task.ts index 2bbd119a1e4ab..96a2cdc6994ec 100644 --- a/packages/@aws-cdk/aws-events-targets/lib/ecs-task.ts +++ b/packages/@aws-cdk/aws-events-targets/lib/ecs-task.ts @@ -101,7 +101,7 @@ export interface EcsTaskProps extends TargetBaseProps { * * @default - Tags will not be propagated */ - readonly propagateTags?: boolean + readonly propagateTags?: ecs.PropagatedTagSource /** * The metadata that you apply to the task to help you categorize and organize them. Each tag consists of a key and an optional value, both of which you define. @@ -148,7 +148,12 @@ export class EcsTask implements events.IRuleTarget { this.taskDefinition = props.taskDefinition; this.taskCount = props.taskCount ?? 1; this.platformVersion = props.platformVersion; - this.propagateTags = props.propagateTags === true ? ecs.PropagatedTagSource.TASK_DEFINITION : undefined ; + + const propagateTagsValidValues = [ecs.PropagatedTagSource.TASK_DEFINITION, ecs.PropagatedTagSource.NONE]; + if (props.propagateTags && !propagateTagsValidValues.includes(props.propagateTags)) { + throw new Error('When propagateTags is passed, it must be set to TASK_DEFINITION or NONE.'); + } + this.propagateTags = props.propagateTags; this.role = props.role ?? singletonEventRole(this.taskDefinition); for (const stmt of this.createEventRolePolicyStatements()) { diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/event-rule-target.test.ts b/packages/@aws-cdk/aws-events-targets/test/ecs/event-rule-target.test.ts index 8fbdf78158668..d6498971c8274 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/event-rule-target.test.ts +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/event-rule-target.test.ts @@ -799,7 +799,7 @@ test('sets the propagate tags flag', () => { containerName: 'TheContainer', command: ['echo', events.EventField.fromPath('$.detail.event')], }], - propagateTags: true, + propagateTags: ecs.PropagatedTagSource.TASK_DEFINITION, })); // THEN @@ -814,6 +814,32 @@ test('sets the propagate tags flag', () => { }); }); +test('throws an error when trying to pass a disallowed value for propagateTags', () => { + // GIVEN + const taskDefinition = new ecs.FargateTaskDefinition(stack, 'TaskDef'); + taskDefinition.addContainer('TheContainer', { + image: ecs.ContainerImage.fromRegistry('henk'), + }); + + const rule = new events.Rule(stack, 'Rule', { + schedule: events.Schedule.expression('rate(1 min)'), + }); + + // THEN + expect(() => { + rule.addTarget(new targets.EcsTask({ + cluster, + taskDefinition, + taskCount: 1, + containerOverrides: [{ + containerName: 'TheContainer', + command: ['echo', events.EventField.fromPath('$.detail.event')], + }], + propagateTags: ecs.PropagatedTagSource.SERVICE, // propagateTags must be TASK_DEFINITION or NONE + })); + }).toThrowError('When propagateTags is passed, it must be set to TASK_DEFINITION or NONE.'); +}); + test('sets tag lists', () => { // GIVEN const taskDefinition = new ecs.FargateTaskDefinition(stack, 'TaskDef'); diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.ts b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.ts index 3ea37989f5eac..f57f659a0c9a1 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.ts +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-ec2-task.ts @@ -45,7 +45,7 @@ rule.addTarget(new targets.EcsTask({ ], }], deadLetterQueue, - propagateTags: true, + propagateTags: ecs.PropagatedTagSource.TASK_DEFINITION, tagList: [ { key: 'my-tag', diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.ts b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.ts index 2b717035b16ea..20f38e18fbc23 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.ts +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.ts @@ -41,7 +41,7 @@ rule.addTarget(new targets.EcsTask({ ], }], deadLetterQueue, - propagateTags: true, + propagateTags: ecs.PropagatedTagSource.TASK_DEFINITION, tagList: [ { key: 'my-tag', From acf5cf2c57087a94cf8643fbe026f1fa0fd00f3f Mon Sep 17 00:00:00 2001 From: Ryan Batchelder Date: Tue, 18 Apr 2023 10:07:20 -0400 Subject: [PATCH 5/6] Update docstrings per PR feedback Rename tagList to tags --- .../test/ecs/integ.event-ec2-task.ts | 2 +- .../test/ecs/integ.event-fargate-task.ts | 2 +- .../aws-cdk-lib/aws-events-targets/README.md | 7 ++++++- .../aws-events-targets/lib/ecs-task.ts | 18 ++++++++---------- .../test/ecs/event-rule-target.test.ts | 2 +- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-events-targets/test/ecs/integ.event-ec2-task.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-events-targets/test/ecs/integ.event-ec2-task.ts index cbb5b24998c03..d97ebfdaeac88 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-events-targets/test/ecs/integ.event-ec2-task.ts +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-events-targets/test/ecs/integ.event-ec2-task.ts @@ -46,7 +46,7 @@ rule.addTarget(new targets.EcsTask({ }], deadLetterQueue, propagateTags: ecs.PropagatedTagSource.TASK_DEFINITION, - tagList: [ + tags: [ { key: 'my-tag', value: 'my-tag-value', diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-events-targets/test/ecs/integ.event-fargate-task.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-events-targets/test/ecs/integ.event-fargate-task.ts index bd138a0193557..8afda14fc48d6 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-events-targets/test/ecs/integ.event-fargate-task.ts +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-events-targets/test/ecs/integ.event-fargate-task.ts @@ -42,7 +42,7 @@ rule.addTarget(new targets.EcsTask({ }], deadLetterQueue, propagateTags: ecs.PropagatedTagSource.TASK_DEFINITION, - tagList: [ + tags: [ { key: 'my-tag', value: 'my-tag-value', diff --git a/packages/aws-cdk-lib/aws-events-targets/README.md b/packages/aws-cdk-lib/aws-events-targets/README.md index c353588b78ac1..80a4271e8b60e 100644 --- a/packages/aws-cdk-lib/aws-events-targets/README.md +++ b/packages/aws-cdk-lib/aws-events-targets/README.md @@ -344,6 +344,11 @@ Use the `EcsTask` target to run an ECS Task. The code snippet below creates a scheduled event rule that will run the task described in `taskDefinition` every hour. +By default, ECS tasks run from EventBridge targets will not have tags applied to them. You can set the `propagateTags` +field to propagate the tags set on the task definition to the task initialized by the event trigger. If you want to set +tags independent of those applied to the TaskDefinition, you can use the `tags` array. Both of these fields can be used +together or separately to set tags on the triggered task. + ```ts import * as ecs from "@aws-cdk/aws-ecs" declare const cluster: ecs.ICluster @@ -358,7 +363,7 @@ rule.addTarget( cluster: cluster, taskDefinition: taskDefinition, propagateTags: ecs.PropagatedTagSource.TASK_DEFINITION, - tagList: [ + tags: [ { key: 'my-tag', value: 'my-tag-value', diff --git a/packages/aws-cdk-lib/aws-events-targets/lib/ecs-task.ts b/packages/aws-cdk-lib/aws-events-targets/lib/ecs-task.ts index 3cbc136a28daf..cfe84d9d04e49 100644 --- a/packages/aws-cdk-lib/aws-events-targets/lib/ecs-task.ts +++ b/packages/aws-cdk-lib/aws-events-targets/lib/ecs-task.ts @@ -8,16 +8,16 @@ import { ContainerOverride } from './ecs-task-properties'; import { addToDeadLetterQueueResourcePolicy, bindBaseTargetConfig, singletonEventRole, TargetBaseProps } from './util'; /** - * Tag + * Metadata that you apply to a resource to help categorize and organize the resource. Each tag consists of a key and an optional value, both of which you define. */ export interface Tag { /** - * key to e tagged + * Key is the name of the tag */ readonly key: string; /** - * additional value + * Value is the metadata contents of the tag */ readonly value: string; } @@ -106,9 +106,9 @@ export interface EcsTaskProps extends TargetBaseProps { /** * The metadata that you apply to the task to help you categorize and organize them. Each tag consists of a key and an optional value, both of which you define. * - * @default - No tags are applied to the task + * @default - No additional tags are applied to the task */ - readonly tagList?: Tag[] + readonly tags?: Tag[] } /** @@ -137,7 +137,7 @@ export class EcsTask implements events.IRuleTarget { private readonly role: iam.IRole; private readonly platformVersion?: ecs.FargatePlatformVersion; private readonly propagateTags?: ecs.PropagatedTagSource; - private readonly tagList?: Tag[] + private readonly tags?: Tag[] constructor(private readonly props: EcsTaskProps) { if (props.securityGroup !== undefined && props.securityGroups !== undefined) { @@ -160,9 +160,7 @@ export class EcsTask implements events.IRuleTarget { this.role.addToPrincipalPolicy(stmt); } - if (props.tagList) { - this.tagList = props.tagList; - } + this.tags = props.tags; // Security groups are only configurable with the "awsvpc" network mode. if (this.taskDefinition.networkMode !== ecs.NetworkMode.AWS_VPC) { @@ -200,7 +198,7 @@ export class EcsTask implements events.IRuleTarget { const taskCount = this.taskCount; const taskDefinitionArn = this.taskDefinition.taskDefinitionArn; const propagateTags = this.propagateTags; - const tagList = this.tagList; + const tagList = this.tags; const subnetSelection = this.props.subnetSelection || { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS }; const assignPublicIp = subnetSelection.subnetType === ec2.SubnetType.PUBLIC ? 'ENABLED' : 'DISABLED'; diff --git a/packages/aws-cdk-lib/aws-events-targets/test/ecs/event-rule-target.test.ts b/packages/aws-cdk-lib/aws-events-targets/test/ecs/event-rule-target.test.ts index 0e6f85c05d564..5f4944bd8014a 100644 --- a/packages/aws-cdk-lib/aws-events-targets/test/ecs/event-rule-target.test.ts +++ b/packages/aws-cdk-lib/aws-events-targets/test/ecs/event-rule-target.test.ts @@ -861,7 +861,7 @@ test('sets tag lists', () => { containerName: 'TheContainer', command: ['echo', events.EventField.fromPath('$.detail.event')], }], - tagList: [ + tags: [ { key: 'my-tag', value: 'my-tag-value', From 373ada2d37c0a81df53c305af55475a1438f8f6f Mon Sep 17 00:00:00 2001 From: Ryan Batchelder Date: Tue, 18 Apr 2023 12:00:01 -0400 Subject: [PATCH 6/6] Update organization around tagging comment --- packages/aws-cdk-lib/aws-events-targets/README.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/aws-cdk-lib/aws-events-targets/README.md b/packages/aws-cdk-lib/aws-events-targets/README.md index 80a4271e8b60e..b73e4a905c8f1 100644 --- a/packages/aws-cdk-lib/aws-events-targets/README.md +++ b/packages/aws-cdk-lib/aws-events-targets/README.md @@ -344,10 +344,15 @@ Use the `EcsTask` target to run an ECS Task. The code snippet below creates a scheduled event rule that will run the task described in `taskDefinition` every hour. -By default, ECS tasks run from EventBridge targets will not have tags applied to them. You can set the `propagateTags` -field to propagate the tags set on the task definition to the task initialized by the event trigger. If you want to set -tags independent of those applied to the TaskDefinition, you can use the `tags` array. Both of these fields can be used -together or separately to set tags on the triggered task. +### Tagging Tasks + +By default, ECS tasks run from EventBridge targets will not have tags applied to +them. You can set the `propagateTags` field to propagate the tags set on the task +definition to the task initialized by the event trigger. + +If you want to set tags independent of those applied to the TaskDefinition, you +can use the `tags` array. Both of these fields can be used together or separately +to set tags on the triggered task. ```ts import * as ecs from "@aws-cdk/aws-ecs"