Skip to content

Commit

Permalink
tags.of
Browse files Browse the repository at this point in the history
  • Loading branch information
comcalvi committed May 23, 2023
1 parent 5acba60 commit 6484288
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 99 deletions.
10 changes: 4 additions & 6 deletions packages/@aws-cdk/aws-batch-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,20 +206,18 @@ new batch.ManagedEc2EcsComputeEnvironment(this, 'myEc2ComputeEnv', {

### Tagging Instances

You can tag any instances launched by your managed EC2 ComputeEnvironments by specifying the `instanceTags` property or
calling the `addInstanceTag()` method:
You can tag any instances launched by your managed EC2 ComputeEnvironments by using the CDK `Tags` API:

```ts
import { Tags } from 'aws-cdk-lib';

declare const vpc: ec2.IVpc;

const tagCE = new batch.ManagedEc2EcsComputeEnvironment(this, 'CEThatMakesTaggedInstnaces', {
vpc,
instanceTags: {
key: 'value',
},
});

tagCE.addInstanceTag('key2', 'value2');
Tags.of(tagCE).add('super', 'salamander');
```

Unmanaged `ComputeEnvironment`s do not support `maxvCpus` or `minvCpus` because you must provision and manage the instances yourself;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as eks from 'aws-cdk-lib/aws-eks';
import * as iam from 'aws-cdk-lib/aws-iam';
import { IRole } from 'aws-cdk-lib/aws-iam';
import { ArnFormat, Duration, Lazy, Resource, Stack } from 'aws-cdk-lib';
import { ArnFormat, Duration, ITaggable, Lazy, Resource, Stack, TagManager, TagType } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { CfnComputeEnvironment } from 'aws-cdk-lib/aws-batch';
import { IComputeEnvironment, ComputeEnvironmentBase, ComputeEnvironmentProps } from './compute-environment-base';
Expand All @@ -12,7 +12,7 @@ import { IComputeEnvironment, ComputeEnvironmentBase, ComputeEnvironmentProps }
* Represents a Managed ComputeEnvironment. Batch will provision EC2 Instances to
* meet the requirements of the jobs executing in this ComputeEnvironment.
*/
export interface IManagedComputeEnvironment extends IComputeEnvironment, ec2.IConnectable {
export interface IManagedComputeEnvironment extends IComputeEnvironment, ec2.IConnectable, ITaggable {
/**
* The maximum vCpus this `ManagedComputeEnvironment` can scale up to.
*
Expand Down Expand Up @@ -206,6 +206,7 @@ export abstract class ManagedComputeEnvironmentBase extends ComputeEnvironmentBa
public readonly terminateOnUpdate?: boolean;
public readonly securityGroups: ec2.ISecurityGroup[];
public readonly updateToLatestImageVersion?: boolean;
public readonly tags: TagManager = new TagManager(TagType.MAP, 'AWS::Batch::ComputeEnvironment');

public readonly connections: ec2.Connections;

Expand Down Expand Up @@ -337,14 +338,6 @@ export interface IManagedEc2EcsComputeEnvironment extends IManagedComputeEnviron
*/
readonly placementGroup?: ec2.IPlacementGroup;

/**
* Key-value pair tags to be applied to EC2 resources that are launched in the compute environment.
* These tags aren't seen when using the AWS Batch ListTagsForResource API operation.
*
* @default - no instance tags
*/
readonly instanceTags: { [key: string]: string };

/**
* Add an instance type to this compute environment
*/
Expand All @@ -354,11 +347,6 @@ export interface IManagedEc2EcsComputeEnvironment extends IManagedComputeEnviron
* Add an instance class to this compute environment
*/
addInstanceClass(instanceClass: ec2.InstanceClass): void;

/**
* Add an instance tag to this compute environment
*/
addInstanceTag(key: string, value: string): void;
}

/**
Expand Down Expand Up @@ -582,14 +570,6 @@ export interface ManagedEc2EcsComputeEnvironmentProps extends ManagedComputeEnvi
* @default - no placement group
*/
readonly placementGroup?: ec2.IPlacementGroup;

/**
* Key-value pair tags to be applied to EC2 resources that are launched in the compute environment.
* These tags aren't seen when using the AWS Batch ListTagsForResource API operation.
*
* @default - no instance tags
*/
readonly instanceTags?: { [key: string]: string };
}

/**
Expand All @@ -613,20 +593,17 @@ export class ManagedEc2EcsComputeEnvironment extends ManagedComputeEnvironmentBa
public readonly enabled = true;
public readonly instanceClasses = [];
public readonly instanceTypes = [];
public readonly instanceTags = {};
public readonly maxvCpus = 1;
public readonly connections = { } as any;
public readonly securityGroups = [];
public readonly tags: TagManager = new TagManager(TagType.MAP, 'AWS::Batch::ComputeEnvironment');

public addInstanceClass(_instanceClass: ec2.InstanceClass): void {
throw new Error(`cannot add instance class to imported ComputeEnvironment '${id}'`);
}
public addInstanceType(_instanceType: ec2.InstanceType): void {
throw new Error(`cannot add instance type to imported ComputeEnvironment '${id}'`);
}
public addInstanceTag(_instanceTagKey: string, _instanceTagValue: string): void {
throw new Error(`cannot add instance tag to imported ComputeEnvironment '${id}'`);
}
}

return new Import(scope, id);
Expand All @@ -644,7 +621,6 @@ export class ManagedEc2EcsComputeEnvironment extends ManagedComputeEnvironmentBa
public readonly launchTemplate?: ec2.ILaunchTemplate;
public readonly minvCpus?: number;
public readonly placementGroup?: ec2.IPlacementGroup;
public readonly instanceTags: { [key: string]: string };

private readonly instanceProfile: iam.CfnInstanceProfile;

Expand All @@ -671,7 +647,6 @@ export class ManagedEc2EcsComputeEnvironment extends ManagedComputeEnvironmentBa
this.launchTemplate = props.launchTemplate;
this.minvCpus = props.minvCpus ?? DEFAULT_MIN_VCPUS;
this.placementGroup = props.placementGroup;
this.instanceTags = props.instanceTags ?? {};

validateVCpus(id, this.minvCpus, this.maxvCpus);
validateSpotConfig(id, this.spot, this.spotBidPercentage, this.spotFleetRole);
Expand Down Expand Up @@ -701,9 +676,7 @@ export class ManagedEc2EcsComputeEnvironment extends ManagedComputeEnvironmentBa
};
}),
placementGroup: this.placementGroup?.placementGroupName,
tags: Lazy.any({
produce: () => Object.keys(this.instanceTags).length === 0 ? undefined : this.instanceTags,
}) as any,
tags: this.tags.renderedTags as any,
},
});

Expand All @@ -724,10 +697,6 @@ export class ManagedEc2EcsComputeEnvironment extends ManagedComputeEnvironmentBa
public addInstanceClass(instanceClass: ec2.InstanceClass): void {
this.instanceClasses.push(instanceClass);
}

public addInstanceTag(key: string, value: string): void {
this.instanceTags[key] = value;
}
}

/**
Expand Down Expand Up @@ -841,14 +810,6 @@ interface IManagedEc2EksComputeEnvironment extends IManagedComputeEnvironment {
*/
readonly placementGroup?: ec2.IPlacementGroup;

/**
* Key-value pair tags to be applied to EC2 resources that are launched in the compute environment.
* These tags aren't seen when using the AWS Batch ListTagsForResource API operation.
*
* @default - no instance tags
*/
readonly instanceTags?: { [key: string]: string };

/**
* Add an instance type to this compute environment
*/
Expand All @@ -858,11 +819,6 @@ interface IManagedEc2EksComputeEnvironment extends IManagedComputeEnvironment {
* Add an instance class to this compute environment
*/
addInstanceClass(instanceClass: ec2.InstanceClass): void;

/**
* Add an instance tag to this compute environment
*/
addInstanceTag(key: string, value: string): void;
}

/**
Expand Down Expand Up @@ -989,14 +945,6 @@ export interface ManagedEc2EksComputeEnvironmentProps extends ManagedComputeEnvi
* @default - no placement group
*/
readonly placementGroup?: ec2.IPlacementGroup;

/**
* Key-value pair tags to be applied to EC2 resources that are launched in the compute environment.
* These tags aren't seen when using the AWS Batch ListTagsForResource API operation.
*
* @default - no instance tags
*/
readonly instanceTags?: { [key: string]: string };
}

/**
Expand All @@ -1020,7 +968,6 @@ export class ManagedEc2EksComputeEnvironment extends ManagedComputeEnvironmentBa
public readonly launchTemplate?: ec2.ILaunchTemplate;
public readonly minvCpus?: number;
public readonly placementGroup?: ec2.IPlacementGroup;
public readonly instanceTags: { [key: string]: string };

private readonly instanceProfile: iam.CfnInstanceProfile;

Expand All @@ -1046,7 +993,6 @@ export class ManagedEc2EksComputeEnvironment extends ManagedComputeEnvironmentBa
this.launchTemplate = props.launchTemplate;
this.minvCpus = props.minvCpus ?? DEFAULT_MIN_VCPUS;
this.placementGroup = props.placementGroup;
this.instanceTags = props.instanceTags ?? {};

validateVCpus(id, this.minvCpus, this.maxvCpus);
validateSpotConfig(id, this.spot, this.spotBidPercentage);
Expand Down Expand Up @@ -1077,9 +1023,7 @@ export class ManagedEc2EksComputeEnvironment extends ManagedComputeEnvironmentBa
};
}),
placementGroup: this.placementGroup?.placementGroupName,
tags: Lazy.any({
produce: () => Object.keys(this.instanceTags).length === 0 ? undefined : this.instanceTags,
}) as any,
tags: this.tags.renderedTags as any,
},
});

Expand All @@ -1100,10 +1044,6 @@ export class ManagedEc2EksComputeEnvironment extends ManagedComputeEnvironmentBa
public addInstanceClass(instanceClass: ec2.InstanceClass): void {
this.instanceClasses.push(instanceClass);
}

public addInstanceTag(key: string, value: string): void {
this.instanceTags[key] = value;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"version": "31.0.0",
"files": {
"ba920705b83f88b051d5b8d11dbeca350a902eba11541e79056dd8d2c2fbc79a": {
"6ebdcdec29ca32bb55c4daa83140fbc6af6c8a2663beb1c1a833a3d4c6ee12c0": {
"source": {
"path": "batch-stack.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "ba920705b83f88b051d5b8d11dbeca350a902eba11541e79056dd8d2c2fbc79a.json",
"objectKey": "6ebdcdec29ca32bb55c4daa83140fbc6af6c8a2663beb1c1a833a3d4c6ee12c0.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 @@ -894,6 +894,16 @@
"IpProtocol": "-1"
}
],
"Tags": [
{
"Key": "foo",
"Value": "bar"
},
{
"Key": "super",
"Value": "salamander"
}
],
"VpcId": {
"Ref": "vpcA2121C38"
}
Expand Down Expand Up @@ -927,6 +937,16 @@
]
]
}
],
"Tags": [
{
"Key": "foo",
"Value": "bar"
},
{
"Key": "super",
"Value": "salamander"
}
]
}
},
Expand Down Expand Up @@ -982,14 +1002,18 @@
}
],
"Tags": {
"key": "value",
"foo": "bar"
"foo": "bar",
"super": "salamander"
},
"Type": "EC2",
"UpdateToLatestImageVersion": true
},
"ReplaceComputeEnvironment": false,
"State": "ENABLED",
"Tags": {
"foo": "bar",
"super": "salamander"
},
"UpdatePolicy": {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,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}/ba920705b83f88b051d5b8d11dbeca350a902eba11541e79056dd8d2c2fbc79a.json",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/6ebdcdec29ca32bb55c4daa83140fbc6af6c8a2663beb1c1a833a3d4c6ee12c0.json",
"requiresBootstrapStackVersion": 6,
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
"additionalDependencies": [
Expand Down Expand Up @@ -312,7 +312,10 @@
"/batch-stack/taggedCE/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "taggedCE5029E6F8"
"data": "taggedCE5029E6F8",
"trace": [
"!!DESTRUCTIVE_CHANGES: WILL_REPLACE"
]
}
],
"/batch-stack/BootstrapVersion": [
Expand Down
Loading

0 comments on commit 6484288

Please sign in to comment.