Skip to content

Commit 1bc4526

Browse files
authored
feat(batch): add propagate tags prop in job definition (#21904)
Closes: #21740 Add support for propagateTags to the CDK construct. ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-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 80cb527 commit 1bc4526

9 files changed

+175
-14
lines changed

packages/@aws-cdk/aws-batch/lib/job-definition.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,18 @@ export interface JobDefinitionProps {
303303
* @default - EC2
304304
*/
305305
readonly platformCapabilities?: PlatformCapabilities[];
306+
307+
/**
308+
* Specifies whether to propagate the tags from the job or job definition to the corresponding Amazon ECS task.
309+
* If no value is specified, the tags aren't propagated.
310+
* Tags can only be propagated to the tasks during task creation. For tags with the same name,
311+
* job tags are given priority over job definitions tags.
312+
* If the total number of combined tags from the job and job definition is over 50, the job is moved to the `FAILED` state.
313+
*
314+
* @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-batch-jobdefinition.html#cfn-batch-jobdefinition-propagatetags
315+
* @default - undefined
316+
*/
317+
readonly propagateTags?: boolean;
306318
}
307319

308320
/**
@@ -458,6 +470,7 @@ export class JobDefinition extends Resource implements IJobDefinition {
458470
attemptDurationSeconds: props.timeout ? props.timeout.toSeconds() : undefined,
459471
},
460472
platformCapabilities: props.platformCapabilities ?? [PlatformCapabilities.EC2],
473+
propagateTags: props.propagateTags,
461474
});
462475

463476
// add read secrets permission to execution role
@@ -602,4 +615,4 @@ export class JobDefinition extends Resource implements IJobDefinition {
602615
};
603616
});
604617
}
605-
}
618+
}

packages/@aws-cdk/aws-batch/test/integ.job-definition.ts

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ new JobDefinition(stack, "JobDefinition", {
1414
},
1515
});
1616

17+
new JobDefinition(stack, "JobDefinitionTags", {
18+
container: {
19+
image: ContainerImage.fromRegistry("docker/whalesay"),
20+
},
21+
propagateTags: true,
22+
});
23+
1724
const integ = new IntegTest(app, "IntegTest-BatchDefaultEnvVarsStack", {
1825
testCases: [stack],
1926
regions: ["us-east-1"],

packages/@aws-cdk/aws-batch/test/job-definition.integ.snapshot/BatchDefaultEnvVarsStack.assets.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"version": "21.0.0",
33
"files": {
4-
"0a50911921bcac3c1775e3a82fc9c4414e3f12ea24b1e2c73bf1fc4c625ee16c": {
4+
"fa4cb3c0495612b2b5dea9687948520b07f859705061dad691367153183cbc90": {
55
"source": {
66
"path": "BatchDefaultEnvVarsStack.template.json",
77
"packaging": "file"
88
},
99
"destinations": {
1010
"current_account-current_region": {
1111
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12-
"objectKey": "0a50911921bcac3c1775e3a82fc9c4414e3f12ea24b1e2c73bf1fc4c625ee16c.json",
12+
"objectKey": "fa4cb3c0495612b2b5dea9687948520b07f859705061dad691367153183cbc90.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-batch/test/job-definition.integ.snapshot/BatchDefaultEnvVarsStack.template.json

+43
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,49 @@
4141
},
4242
"Timeout": {}
4343
}
44+
},
45+
"JobDefinitionTags76FA063A": {
46+
"Type": "AWS::Batch::JobDefinition",
47+
"Properties": {
48+
"Type": "container",
49+
"ContainerProperties": {
50+
"Environment": [
51+
{
52+
"Name": "AWS_REGION",
53+
"Value": {
54+
"Ref": "AWS::Region"
55+
}
56+
},
57+
{
58+
"Name": "AWS_ACCOUNT",
59+
"Value": {
60+
"Ref": "AWS::AccountId"
61+
}
62+
}
63+
],
64+
"Image": "docker/whalesay",
65+
"Privileged": false,
66+
"ReadonlyRootFilesystem": false,
67+
"ResourceRequirements": [
68+
{
69+
"Type": "VCPU",
70+
"Value": "1"
71+
},
72+
{
73+
"Type": "MEMORY",
74+
"Value": "4"
75+
}
76+
]
77+
},
78+
"PlatformCapabilities": [
79+
"EC2"
80+
],
81+
"PropagateTags": true,
82+
"RetryStrategy": {
83+
"Attempts": 1
84+
},
85+
"Timeout": {}
86+
}
4487
}
4588
},
4689
"Parameters": {

packages/@aws-cdk/aws-batch/test/job-definition.integ.snapshot/IntegTestBatchDefaultEnvVarsStackDefaultTestDeployAssertC15EFFF2.assets.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
}
1515
}
1616
},
17-
"de70b340a5b843502197c20700c9328cb928be11584f66762e1ac05dc5380bed": {
17+
"674d442d4a977a672cccae088b5eabcf91e06ee46b2464e4437c0332e04cf1a8": {
1818
"source": {
1919
"path": "IntegTestBatchDefaultEnvVarsStackDefaultTestDeployAssertC15EFFF2.template.json",
2020
"packaging": "file"
2121
},
2222
"destinations": {
2323
"current_account-current_region": {
2424
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
25-
"objectKey": "de70b340a5b843502197c20700c9328cb928be11584f66762e1ac05dc5380bed.json",
25+
"objectKey": "674d442d4a977a672cccae088b5eabcf91e06ee46b2464e4437c0332e04cf1a8.json",
2626
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
2727
}
2828
}

packages/@aws-cdk/aws-batch/test/job-definition.integ.snapshot/IntegTestBatchDefaultEnvVarsStackDefaultTestDeployAssertC15EFFF2.template.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"status": "ACTIVE"
1616
},
1717
"flattenResponse": "true",
18-
"salt": "1661947601911"
18+
"salt": "1662181496271"
1919
},
2020
"UpdateReplacePolicy": "Delete",
2121
"DeletionPolicy": "Delete"
@@ -36,7 +36,7 @@
3636
]
3737
},
3838
"expected": "{\"$StringLike\":\"AWS_REGION\"}",
39-
"salt": "1661947601912"
39+
"salt": "1662181496272"
4040
},
4141
"UpdateReplacePolicy": "Delete",
4242
"DeletionPolicy": "Delete"

packages/@aws-cdk/aws-batch/test/job-definition.integ.snapshot/manifest.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"validateOnSynth": false,
2424
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
2525
"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}/0a50911921bcac3c1775e3a82fc9c4414e3f12ea24b1e2c73bf1fc4c625ee16c.json",
26+
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/fa4cb3c0495612b2b5dea9687948520b07f859705061dad691367153183cbc90.json",
2727
"requiresBootstrapStackVersion": 6,
2828
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
2929
"additionalDependencies": [
@@ -45,6 +45,12 @@
4545
"data": "JobDefinition24FFE3ED"
4646
}
4747
],
48+
"/BatchDefaultEnvVarsStack/JobDefinitionTags/Resource": [
49+
{
50+
"type": "aws:cdk:logicalId",
51+
"data": "JobDefinitionTags76FA063A"
52+
}
53+
],
4854
"/BatchDefaultEnvVarsStack/BootstrapVersion": [
4955
{
5056
"type": "aws:cdk:logicalId",
@@ -76,7 +82,7 @@
7682
"validateOnSynth": false,
7783
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
7884
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
79-
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/de70b340a5b843502197c20700c9328cb928be11584f66762e1ac05dc5380bed.json",
85+
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/674d442d4a977a672cccae088b5eabcf91e06ee46b2464e4437c0332e04cf1a8.json",
8086
"requiresBootstrapStackVersion": 6,
8187
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
8288
"additionalDependencies": [

packages/@aws-cdk/aws-batch/test/job-definition.integ.snapshot/tree.json

+82-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"path": "Tree",
1010
"constructInfo": {
1111
"fqn": "constructs.Construct",
12-
"version": "10.1.85"
12+
"version": "10.1.92"
1313
}
1414
},
1515
"BatchDefaultEnvVarsStack": {
@@ -91,6 +91,83 @@
9191
"fqn": "@aws-cdk/aws-batch.JobDefinition",
9292
"version": "0.0.0"
9393
}
94+
},
95+
"JobDefinitionTags": {
96+
"id": "JobDefinitionTags",
97+
"path": "BatchDefaultEnvVarsStack/JobDefinitionTags",
98+
"children": {
99+
"Resource-Batch-Task-Definition-Role": {
100+
"id": "Resource-Batch-Task-Definition-Role",
101+
"path": "BatchDefaultEnvVarsStack/JobDefinitionTags/Resource-Batch-Task-Definition-Role",
102+
"constructInfo": {
103+
"fqn": "@aws-cdk/aws-iam.LazyRole",
104+
"version": "0.0.0"
105+
}
106+
},
107+
"Resource-Batch-Job-Container-Definition": {
108+
"id": "Resource-Batch-Job-Container-Definition",
109+
"path": "BatchDefaultEnvVarsStack/JobDefinitionTags/Resource-Batch-Job-Container-Definition",
110+
"constructInfo": {
111+
"fqn": "@aws-cdk/aws-ecs.ContainerDefinition",
112+
"version": "0.0.0"
113+
}
114+
},
115+
"Resource": {
116+
"id": "Resource",
117+
"path": "BatchDefaultEnvVarsStack/JobDefinitionTags/Resource",
118+
"attributes": {
119+
"aws:cdk:cloudformation:type": "AWS::Batch::JobDefinition",
120+
"aws:cdk:cloudformation:props": {
121+
"type": "container",
122+
"containerProperties": {
123+
"environment": [
124+
{
125+
"name": "AWS_REGION",
126+
"value": {
127+
"Ref": "AWS::Region"
128+
}
129+
},
130+
{
131+
"name": "AWS_ACCOUNT",
132+
"value": {
133+
"Ref": "AWS::AccountId"
134+
}
135+
}
136+
],
137+
"image": "docker/whalesay",
138+
"privileged": false,
139+
"readonlyRootFilesystem": false,
140+
"resourceRequirements": [
141+
{
142+
"type": "VCPU",
143+
"value": "1"
144+
},
145+
{
146+
"type": "MEMORY",
147+
"value": "4"
148+
}
149+
]
150+
},
151+
"platformCapabilities": [
152+
"EC2"
153+
],
154+
"propagateTags": true,
155+
"retryStrategy": {
156+
"attempts": 1
157+
},
158+
"timeout": {}
159+
}
160+
},
161+
"constructInfo": {
162+
"fqn": "@aws-cdk/aws-batch.CfnJobDefinition",
163+
"version": "0.0.0"
164+
}
165+
}
166+
},
167+
"constructInfo": {
168+
"fqn": "@aws-cdk/aws-batch.JobDefinition",
169+
"version": "0.0.0"
170+
}
94171
}
95172
},
96173
"constructInfo": {
@@ -111,7 +188,7 @@
111188
"path": "IntegTest-BatchDefaultEnvVarsStack/DefaultTest/Default",
112189
"constructInfo": {
113190
"fqn": "constructs.Construct",
114-
"version": "10.1.85"
191+
"version": "10.1.92"
115192
}
116193
},
117194
"DeployAssert": {
@@ -131,7 +208,7 @@
131208
"path": "IntegTest-BatchDefaultEnvVarsStack/DefaultTest/DeployAssert/AwsApiCallBatchdescribeJobDefinitions/SdkProvider/AssertionsProvider",
132209
"constructInfo": {
133210
"fqn": "constructs.Construct",
134-
"version": "10.1.85"
211+
"version": "10.1.92"
135212
}
136213
}
137214
},
@@ -171,7 +248,7 @@
171248
"path": "IntegTest-BatchDefaultEnvVarsStack/DefaultTest/DeployAssert/AwsApiCallBatchdescribeJobDefinitions/AssertEqualsBatchdescribeJobDefinitions/AssertionProvider/AssertionsProvider",
172249
"constructInfo": {
173250
"fqn": "constructs.Construct",
174-
"version": "10.1.85"
251+
"version": "10.1.92"
175252
}
176253
}
177254
},
@@ -249,7 +326,7 @@
249326
},
250327
"constructInfo": {
251328
"fqn": "constructs.Construct",
252-
"version": "10.1.85"
329+
"version": "10.1.92"
253330
}
254331
}
255332
},

packages/@aws-cdk/aws-batch/test/job-definition.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,21 @@ describe('Batch Job Definition', () => {
284284
});
285285
});
286286

287+
test('Can propagate tags', () => {
288+
// WHEN
289+
new batch.JobDefinition(stack, 'job-def', {
290+
container: {
291+
image: ecs.ContainerImage.fromRegistry('docker/whalesay'),
292+
},
293+
propagateTags: true,
294+
});
295+
296+
// THEN
297+
Template.fromStack(stack).hasResourceProperties('AWS::Batch::JobDefinition', {
298+
PropagateTags: true,
299+
});
300+
});
301+
287302
test('can use an ecr image', () => {
288303
// WHEN
289304
const repo = new ecr.Repository(stack, 'image-repo');

0 commit comments

Comments
 (0)