Skip to content

Commit 8498740

Browse files
authored
feat(batch-alpha): tag instances launched from your managed CEs (#25643)
`Tags.of()` now applies to `ComputeResources.Tags` ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 6c5b67e commit 8498740

File tree

8 files changed

+420
-21
lines changed

8 files changed

+420
-21
lines changed

packages/@aws-cdk/aws-batch-alpha/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,22 @@ new batch.ManagedEc2EcsComputeEnvironment(this, 'myEc2ComputeEnv', {
204204
});
205205
```
206206

207+
### Tagging Instances
208+
209+
You can tag any instances launched by your managed EC2 ComputeEnvironments by using the CDK `Tags` API:
210+
211+
```ts
212+
import { Tags } from 'aws-cdk-lib';
213+
214+
declare const vpc: ec2.IVpc;
215+
216+
const tagCE = new batch.ManagedEc2EcsComputeEnvironment(this, 'CEThatMakesTaggedInstnaces', {
217+
vpc,
218+
});
219+
220+
Tags.of(tagCE).add('super', 'salamander');
221+
```
222+
207223
Unmanaged `ComputeEnvironment`s do not support `maxvCpus` or `minvCpus` because you must provision and manage the instances yourself;
208224
that is, Batch will not scale them up and down as needed.
209225

packages/@aws-cdk/aws-batch-alpha/lib/managed-compute-environment.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as ec2 from 'aws-cdk-lib/aws-ec2';
22
import * as eks from 'aws-cdk-lib/aws-eks';
33
import * as iam from 'aws-cdk-lib/aws-iam';
44
import { IRole } from 'aws-cdk-lib/aws-iam';
5-
import { ArnFormat, Duration, Lazy, Resource, Stack } from 'aws-cdk-lib';
5+
import { ArnFormat, Duration, ITaggable, Lazy, Resource, Stack, TagManager, TagType } from 'aws-cdk-lib';
66
import { Construct } from 'constructs';
77
import { CfnComputeEnvironment } from 'aws-cdk-lib/aws-batch';
88
import { IComputeEnvironment, ComputeEnvironmentBase, ComputeEnvironmentProps } from './compute-environment-base';
@@ -11,7 +11,7 @@ import { IComputeEnvironment, ComputeEnvironmentBase, ComputeEnvironmentProps }
1111
* Represents a Managed ComputeEnvironment. Batch will provision EC2 Instances to
1212
* meet the requirements of the jobs executing in this ComputeEnvironment.
1313
*/
14-
export interface IManagedComputeEnvironment extends IComputeEnvironment, ec2.IConnectable {
14+
export interface IManagedComputeEnvironment extends IComputeEnvironment, ec2.IConnectable, ITaggable {
1515
/**
1616
* The maximum vCpus this `ManagedComputeEnvironment` can scale up to.
1717
*
@@ -205,6 +205,7 @@ export abstract class ManagedComputeEnvironmentBase extends ComputeEnvironmentBa
205205
public readonly terminateOnUpdate?: boolean;
206206
public readonly securityGroups: ec2.ISecurityGroup[];
207207
public readonly updateToLatestImageVersion?: boolean;
208+
public readonly tags: TagManager = new TagManager(TagType.MAP, 'AWS::Batch::ComputeEnvironment');
208209

209210
public readonly connections: ec2.Connections;
210211

@@ -594,6 +595,7 @@ export class ManagedEc2EcsComputeEnvironment extends ManagedComputeEnvironmentBa
594595
public readonly maxvCpus = 1;
595596
public readonly connections = { } as any;
596597
public readonly securityGroups = [];
598+
public readonly tags: TagManager = new TagManager(TagType.MAP, 'AWS::Batch::ComputeEnvironment');
597599

598600
public addInstanceClass(_instanceClass: ec2.InstanceClass): void {
599601
throw new Error(`cannot add instance class to imported ComputeEnvironment '${id}'`);
@@ -673,6 +675,7 @@ export class ManagedEc2EcsComputeEnvironment extends ManagedComputeEnvironmentBa
673675
};
674676
}),
675677
placementGroup: this.placementGroup?.placementGroupName,
678+
tags: this.tags.renderedTags as any,
676679
},
677680
});
678681

@@ -1019,6 +1022,7 @@ export class ManagedEc2EksComputeEnvironment extends ManagedComputeEnvironmentBa
10191022
};
10201023
}),
10211024
placementGroup: this.placementGroup?.placementGroupName,
1025+
tags: this.tags.renderedTags as any,
10221026
},
10231027
});
10241028

packages/@aws-cdk/aws-batch-alpha/test/integ.managed-compute-environment.js.snapshot/batch-stack.assets.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"version": "31.0.0",
33
"files": {
4-
"f47b7d60111f82dad5c04d0bef76e1b62fe75dc319951520566c9d9bce188d10": {
4+
"6ebdcdec29ca32bb55c4daa83140fbc6af6c8a2663beb1c1a833a3d4c6ee12c0": {
55
"source": {
66
"path": "batch-stack.template.json",
77
"packaging": "file"
88
},
99
"destinations": {
1010
"current_account-current_region": {
1111
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12-
"objectKey": "f47b7d60111f82dad5c04d0bef76e1b62fe75dc319951520566c9d9bce188d10.json",
12+
"objectKey": "6ebdcdec29ca32bb55c4daa83140fbc6af6c8a2663beb1c1a833a3d4c6ee12c0.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-alpha/test/integ.managed-compute-environment.js.snapshot/batch-stack.template.json

+134
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,140 @@
882882
"State": "ENABLED",
883883
"UpdatePolicy": {}
884884
}
885+
},
886+
"taggedCESecurityGroup82CCF59F": {
887+
"Type": "AWS::EC2::SecurityGroup",
888+
"Properties": {
889+
"GroupDescription": "batch-stack/taggedCE/SecurityGroup",
890+
"SecurityGroupEgress": [
891+
{
892+
"CidrIp": "0.0.0.0/0",
893+
"Description": "Allow all outbound traffic by default",
894+
"IpProtocol": "-1"
895+
}
896+
],
897+
"Tags": [
898+
{
899+
"Key": "foo",
900+
"Value": "bar"
901+
},
902+
{
903+
"Key": "super",
904+
"Value": "salamander"
905+
}
906+
],
907+
"VpcId": {
908+
"Ref": "vpcA2121C38"
909+
}
910+
}
911+
},
912+
"taggedCEInstanceProfileRoleC239DAF9": {
913+
"Type": "AWS::IAM::Role",
914+
"Properties": {
915+
"AssumeRolePolicyDocument": {
916+
"Statement": [
917+
{
918+
"Action": "sts:AssumeRole",
919+
"Effect": "Allow",
920+
"Principal": {
921+
"Service": "ec2.amazonaws.com"
922+
}
923+
}
924+
],
925+
"Version": "2012-10-17"
926+
},
927+
"ManagedPolicyArns": [
928+
{
929+
"Fn::Join": [
930+
"",
931+
[
932+
"arn:",
933+
{
934+
"Ref": "AWS::Partition"
935+
},
936+
":iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
937+
]
938+
]
939+
}
940+
],
941+
"Tags": [
942+
{
943+
"Key": "foo",
944+
"Value": "bar"
945+
},
946+
{
947+
"Key": "super",
948+
"Value": "salamander"
949+
}
950+
]
951+
}
952+
},
953+
"taggedCEInstanceProfileB29F2197": {
954+
"Type": "AWS::IAM::InstanceProfile",
955+
"Properties": {
956+
"Roles": [
957+
{
958+
"Ref": "taggedCEInstanceProfileRoleC239DAF9"
959+
}
960+
]
961+
}
962+
},
963+
"taggedCE5029E6F8": {
964+
"Type": "AWS::Batch::ComputeEnvironment",
965+
"Properties": {
966+
"Type": "managed",
967+
"ComputeResources": {
968+
"AllocationStrategy": "BEST_FIT_PROGRESSIVE",
969+
"Ec2Configuration": [
970+
{
971+
"ImageIdOverride": {
972+
"Ref": "SsmParameterValueawsserviceamiamazonlinuxlatestamznamihvmx8664gp2C96584B6F00A464EAD1953AFF4B05118Parameter"
973+
},
974+
"ImageType": "ECS_AL2"
975+
}
976+
],
977+
"InstanceRole": {
978+
"Fn::GetAtt": [
979+
"taggedCEInstanceProfileB29F2197",
980+
"Arn"
981+
]
982+
},
983+
"InstanceTypes": [
984+
"optimal"
985+
],
986+
"MaxvCpus": 256,
987+
"MinvCpus": 0,
988+
"SecurityGroupIds": [
989+
{
990+
"Fn::GetAtt": [
991+
"taggedCESecurityGroup82CCF59F",
992+
"GroupId"
993+
]
994+
}
995+
],
996+
"Subnets": [
997+
{
998+
"Ref": "vpcPrivateSubnet1Subnet934893E8"
999+
},
1000+
{
1001+
"Ref": "vpcPrivateSubnet2Subnet7031C2BA"
1002+
}
1003+
],
1004+
"Tags": {
1005+
"foo": "bar",
1006+
"super": "salamander"
1007+
},
1008+
"Type": "EC2",
1009+
"UpdateToLatestImageVersion": true
1010+
},
1011+
"ReplaceComputeEnvironment": false,
1012+
"State": "ENABLED",
1013+
"Tags": {
1014+
"foo": "bar",
1015+
"super": "salamander"
1016+
},
1017+
"UpdatePolicy": {}
1018+
}
8851019
}
8861020
},
8871021
"Parameters": {

packages/@aws-cdk/aws-batch-alpha/test/integ.managed-compute-environment.js.snapshot/manifest.json

+22-13
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"validateOnSynth": false,
1818
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
1919
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
20-
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/f47b7d60111f82dad5c04d0bef76e1b62fe75dc319951520566c9d9bce188d10.json",
20+
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/6ebdcdec29ca32bb55c4daa83140fbc6af6c8a2663beb1c1a833a3d4c6ee12c0.json",
2121
"requiresBootstrapStackVersion": 6,
2222
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
2323
"additionalDependencies": [
@@ -291,35 +291,44 @@
291291
"data": "SpotEc2A0470C83"
292292
}
293293
],
294-
"/batch-stack/BootstrapVersion": [
294+
"/batch-stack/taggedCE/SecurityGroup/Resource": [
295295
{
296296
"type": "aws:cdk:logicalId",
297-
"data": "BootstrapVersion"
297+
"data": "taggedCESecurityGroup82CCF59F"
298298
}
299299
],
300-
"/batch-stack/CheckBootstrapVersion": [
300+
"/batch-stack/taggedCE/InstanceProfileRole/Resource": [
301301
{
302302
"type": "aws:cdk:logicalId",
303-
"data": "CheckBootstrapVersion"
303+
"data": "taggedCEInstanceProfileRoleC239DAF9"
304304
}
305305
],
306-
"minimalPropsFargate8E9B9556": [
306+
"/batch-stack/taggedCE/InstanceProfile": [
307307
{
308308
"type": "aws:cdk:logicalId",
309-
"data": "minimalPropsFargate8E9B9556",
310-
"trace": [
311-
"!!DESTRUCTIVE_CHANGES: WILL_DESTROY"
312-
]
309+
"data": "taggedCEInstanceProfileB29F2197"
313310
}
314311
],
315-
"maximalPropsFargateA2E688D8": [
312+
"/batch-stack/taggedCE/Resource": [
316313
{
317314
"type": "aws:cdk:logicalId",
318-
"data": "maximalPropsFargateA2E688D8",
315+
"data": "taggedCE5029E6F8",
319316
"trace": [
320-
"!!DESTRUCTIVE_CHANGES: WILL_DESTROY"
317+
"!!DESTRUCTIVE_CHANGES: WILL_REPLACE"
321318
]
322319
}
320+
],
321+
"/batch-stack/BootstrapVersion": [
322+
{
323+
"type": "aws:cdk:logicalId",
324+
"data": "BootstrapVersion"
325+
}
326+
],
327+
"/batch-stack/CheckBootstrapVersion": [
328+
{
329+
"type": "aws:cdk:logicalId",
330+
"data": "CheckBootstrapVersion"
331+
}
323332
]
324333
},
325334
"displayName": "batch-stack"

0 commit comments

Comments
 (0)