Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(batch): add propagate tags prop in job definition #21904

Merged
merged 2 commits into from
Sep 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion packages/@aws-cdk/aws-batch/lib/job-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,18 @@ export interface JobDefinitionProps {
* @default - EC2
*/
readonly platformCapabilities?: PlatformCapabilities[];

/**
* Specifies whether to propagate the tags from the job or job definition to the corresponding Amazon ECS task.
* If no value is specified, the tags aren't propagated.
* Tags can only be propagated to the tasks during task creation. For tags with the same name,
* job tags are given priority over job definitions tags.
* If the total number of combined tags from the job and job definition is over 50, the job is moved to the `FAILED` state.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this validation we can do at synth time instead of letting the job fail at deploy time? If so, it should be added.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not aware of a way to get the tag count of a construct; the current Tag aspect API only offers the ability to add/remove tags. If you have any ideas happy to dig deeper.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bummer, that's OK.

*
* @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-batch-jobdefinition.html#cfn-batch-jobdefinition-propagatetags
* @default - undefined
*/
readonly propagateTags?: boolean;
}

/**
Expand Down Expand Up @@ -458,6 +470,7 @@ export class JobDefinition extends Resource implements IJobDefinition {
attemptDurationSeconds: props.timeout ? props.timeout.toSeconds() : undefined,
},
platformCapabilities: props.platformCapabilities ?? [PlatformCapabilities.EC2],
propagateTags: props.propagateTags,
});

// add read secrets permission to execution role
Expand Down Expand Up @@ -602,4 +615,4 @@ export class JobDefinition extends Resource implements IJobDefinition {
};
});
}
}
}
7 changes: 7 additions & 0 deletions packages/@aws-cdk/aws-batch/test/integ.job-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ new JobDefinition(stack, "JobDefinition", {
},
});

new JobDefinition(stack, "JobDefinitionTags", {
container: {
image: ContainerImage.fromRegistry("docker/whalesay"),
},
propagateTags: true,
});

const integ = new IntegTest(app, "IntegTest-BatchDefaultEnvVarsStack", {
testCases: [stack],
regions: ["us-east-1"],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"version": "21.0.0",
"files": {
"0a50911921bcac3c1775e3a82fc9c4414e3f12ea24b1e2c73bf1fc4c625ee16c": {
"fa4cb3c0495612b2b5dea9687948520b07f859705061dad691367153183cbc90": {
"source": {
"path": "BatchDefaultEnvVarsStack.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "0a50911921bcac3c1775e3a82fc9c4414e3f12ea24b1e2c73bf1fc4c625ee16c.json",
"objectKey": "fa4cb3c0495612b2b5dea9687948520b07f859705061dad691367153183cbc90.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 @@ -41,6 +41,49 @@
},
"Timeout": {}
}
},
"JobDefinitionTags76FA063A": {
"Type": "AWS::Batch::JobDefinition",
"Properties": {
"Type": "container",
"ContainerProperties": {
"Environment": [
{
"Name": "AWS_REGION",
"Value": {
"Ref": "AWS::Region"
}
},
{
"Name": "AWS_ACCOUNT",
"Value": {
"Ref": "AWS::AccountId"
}
}
],
"Image": "docker/whalesay",
"Privileged": false,
"ReadonlyRootFilesystem": false,
"ResourceRequirements": [
{
"Type": "VCPU",
"Value": "1"
},
{
"Type": "MEMORY",
"Value": "4"
}
]
},
"PlatformCapabilities": [
"EC2"
],
"PropagateTags": true,
"RetryStrategy": {
"Attempts": 1
},
"Timeout": {}
}
}
},
"Parameters": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
}
}
},
"de70b340a5b843502197c20700c9328cb928be11584f66762e1ac05dc5380bed": {
"674d442d4a977a672cccae088b5eabcf91e06ee46b2464e4437c0332e04cf1a8": {
"source": {
"path": "IntegTestBatchDefaultEnvVarsStackDefaultTestDeployAssertC15EFFF2.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "de70b340a5b843502197c20700c9328cb928be11584f66762e1ac05dc5380bed.json",
"objectKey": "674d442d4a977a672cccae088b5eabcf91e06ee46b2464e4437c0332e04cf1a8.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 @@ -15,7 +15,7 @@
"status": "ACTIVE"
},
"flattenResponse": "true",
"salt": "1661947601911"
"salt": "1662181496271"
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
Expand All @@ -36,7 +36,7 @@
]
},
"expected": "{\"$StringLike\":\"AWS_REGION\"}",
"salt": "1661947601912"
"salt": "1662181496272"
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,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}/0a50911921bcac3c1775e3a82fc9c4414e3f12ea24b1e2c73bf1fc4c625ee16c.json",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/fa4cb3c0495612b2b5dea9687948520b07f859705061dad691367153183cbc90.json",
"requiresBootstrapStackVersion": 6,
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
"additionalDependencies": [
Expand All @@ -45,6 +45,12 @@
"data": "JobDefinition24FFE3ED"
}
],
"/BatchDefaultEnvVarsStack/JobDefinitionTags/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "JobDefinitionTags76FA063A"
}
],
"/BatchDefaultEnvVarsStack/BootstrapVersion": [
{
"type": "aws:cdk:logicalId",
Expand Down Expand Up @@ -76,7 +82,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}/de70b340a5b843502197c20700c9328cb928be11584f66762e1ac05dc5380bed.json",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/674d442d4a977a672cccae088b5eabcf91e06ee46b2464e4437c0332e04cf1a8.json",
"requiresBootstrapStackVersion": 6,
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
"additionalDependencies": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"path": "Tree",
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.1.85"
"version": "10.1.92"
}
},
"BatchDefaultEnvVarsStack": {
Expand Down Expand Up @@ -91,6 +91,83 @@
"fqn": "@aws-cdk/aws-batch.JobDefinition",
"version": "0.0.0"
}
},
"JobDefinitionTags": {
"id": "JobDefinitionTags",
"path": "BatchDefaultEnvVarsStack/JobDefinitionTags",
"children": {
"Resource-Batch-Task-Definition-Role": {
"id": "Resource-Batch-Task-Definition-Role",
"path": "BatchDefaultEnvVarsStack/JobDefinitionTags/Resource-Batch-Task-Definition-Role",
"constructInfo": {
"fqn": "@aws-cdk/aws-iam.LazyRole",
"version": "0.0.0"
}
},
"Resource-Batch-Job-Container-Definition": {
"id": "Resource-Batch-Job-Container-Definition",
"path": "BatchDefaultEnvVarsStack/JobDefinitionTags/Resource-Batch-Job-Container-Definition",
"constructInfo": {
"fqn": "@aws-cdk/aws-ecs.ContainerDefinition",
"version": "0.0.0"
}
},
"Resource": {
"id": "Resource",
"path": "BatchDefaultEnvVarsStack/JobDefinitionTags/Resource",
"attributes": {
"aws:cdk:cloudformation:type": "AWS::Batch::JobDefinition",
"aws:cdk:cloudformation:props": {
"type": "container",
"containerProperties": {
"environment": [
{
"name": "AWS_REGION",
"value": {
"Ref": "AWS::Region"
}
},
{
"name": "AWS_ACCOUNT",
"value": {
"Ref": "AWS::AccountId"
}
}
],
"image": "docker/whalesay",
"privileged": false,
"readonlyRootFilesystem": false,
"resourceRequirements": [
{
"type": "VCPU",
"value": "1"
},
{
"type": "MEMORY",
"value": "4"
}
]
},
"platformCapabilities": [
"EC2"
],
"propagateTags": true,
"retryStrategy": {
"attempts": 1
},
"timeout": {}
}
},
"constructInfo": {
"fqn": "@aws-cdk/aws-batch.CfnJobDefinition",
"version": "0.0.0"
}
}
},
"constructInfo": {
"fqn": "@aws-cdk/aws-batch.JobDefinition",
"version": "0.0.0"
}
}
},
"constructInfo": {
Expand All @@ -111,7 +188,7 @@
"path": "IntegTest-BatchDefaultEnvVarsStack/DefaultTest/Default",
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.1.85"
"version": "10.1.92"
}
},
"DeployAssert": {
Expand All @@ -131,7 +208,7 @@
"path": "IntegTest-BatchDefaultEnvVarsStack/DefaultTest/DeployAssert/AwsApiCallBatchdescribeJobDefinitions/SdkProvider/AssertionsProvider",
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.1.85"
"version": "10.1.92"
}
}
},
Expand Down Expand Up @@ -171,7 +248,7 @@
"path": "IntegTest-BatchDefaultEnvVarsStack/DefaultTest/DeployAssert/AwsApiCallBatchdescribeJobDefinitions/AssertEqualsBatchdescribeJobDefinitions/AssertionProvider/AssertionsProvider",
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.1.85"
"version": "10.1.92"
}
}
},
Expand Down Expand Up @@ -249,7 +326,7 @@
},
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.1.85"
"version": "10.1.92"
}
}
},
Expand Down
15 changes: 15 additions & 0 deletions packages/@aws-cdk/aws-batch/test/job-definition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,21 @@ describe('Batch Job Definition', () => {
});
});

test('Can propagate tags', () => {
// WHEN
new batch.JobDefinition(stack, 'job-def', {
container: {
image: ecs.ContainerImage.fromRegistry('docker/whalesay'),
},
propagateTags: true,
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Batch::JobDefinition', {
PropagateTags: true,
});
});

test('can use an ecr image', () => {
// WHEN
const repo = new ecr.Repository(stack, 'image-repo');
Expand Down