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-alpha): tag instances launched from your managed CEs #25643

Merged
merged 6 commits into from May 24, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/@aws-cdk/aws-batch-alpha/README.md
Expand Up @@ -204,6 +204,22 @@ new batch.ManagedEc2EcsComputeEnvironment(this, 'myEc2ComputeEnv', {
});
```

### Tagging Instances

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,
});

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

Unmanaged `ComputeEnvironment`s do not support `maxvCpus` or `minvCpus` because you must provision and manage the instances yourself;
that is, Batch will not scale them up and down as needed.

Expand Down
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 @@ -11,7 +11,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 @@ -205,6 +205,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 @@ -594,6 +595,7 @@ export class ManagedEc2EcsComputeEnvironment extends ManagedComputeEnvironmentBa
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}'`);
Expand Down Expand Up @@ -673,6 +675,7 @@ export class ManagedEc2EcsComputeEnvironment extends ManagedComputeEnvironmentBa
};
}),
placementGroup: this.placementGroup?.placementGroupName,
tags: this.tags.renderedTags as any,
},
});

Expand Down Expand Up @@ -1019,6 +1022,7 @@ export class ManagedEc2EksComputeEnvironment extends ManagedComputeEnvironmentBa
};
}),
placementGroup: this.placementGroup?.placementGroupName,
tags: this.tags.renderedTags as any,
},
});

Expand Down
@@ -1,15 +1,15 @@
{
"version": "31.0.0",
"files": {
"f47b7d60111f82dad5c04d0bef76e1b62fe75dc319951520566c9d9bce188d10": {
"6ebdcdec29ca32bb55c4daa83140fbc6af6c8a2663beb1c1a833a3d4c6ee12c0": {
"source": {
"path": "batch-stack.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "f47b7d60111f82dad5c04d0bef76e1b62fe75dc319951520566c9d9bce188d10.json",
"objectKey": "6ebdcdec29ca32bb55c4daa83140fbc6af6c8a2663beb1c1a833a3d4c6ee12c0.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
Expand Down
Expand Up @@ -882,6 +882,140 @@
"State": "ENABLED",
"UpdatePolicy": {}
}
},
"taggedCESecurityGroup82CCF59F": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"GroupDescription": "batch-stack/taggedCE/SecurityGroup",
"SecurityGroupEgress": [
{
"CidrIp": "0.0.0.0/0",
"Description": "Allow all outbound traffic by default",
"IpProtocol": "-1"
}
],
"Tags": [
{
"Key": "foo",
"Value": "bar"
},
{
"Key": "super",
"Value": "salamander"
}
],
"VpcId": {
"Ref": "vpcA2121C38"
}
}
},
"taggedCEInstanceProfileRoleC239DAF9": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
{
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition"
},
":iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
]
]
}
],
"Tags": [
{
"Key": "foo",
"Value": "bar"
},
{
"Key": "super",
"Value": "salamander"
}
]
}
},
"taggedCEInstanceProfileB29F2197": {
"Type": "AWS::IAM::InstanceProfile",
"Properties": {
"Roles": [
{
"Ref": "taggedCEInstanceProfileRoleC239DAF9"
}
]
}
},
"taggedCE5029E6F8": {
"Type": "AWS::Batch::ComputeEnvironment",
"Properties": {
"Type": "managed",
"ComputeResources": {
"AllocationStrategy": "BEST_FIT_PROGRESSIVE",
"Ec2Configuration": [
{
"ImageIdOverride": {
"Ref": "SsmParameterValueawsserviceamiamazonlinuxlatestamznamihvmx8664gp2C96584B6F00A464EAD1953AFF4B05118Parameter"
},
"ImageType": "ECS_AL2"
}
],
"InstanceRole": {
"Fn::GetAtt": [
"taggedCEInstanceProfileB29F2197",
"Arn"
]
},
"InstanceTypes": [
"optimal"
],
"MaxvCpus": 256,
"MinvCpus": 0,
"SecurityGroupIds": [
{
"Fn::GetAtt": [
"taggedCESecurityGroup82CCF59F",
"GroupId"
]
}
],
"Subnets": [
{
"Ref": "vpcPrivateSubnet1Subnet934893E8"
},
{
"Ref": "vpcPrivateSubnet2Subnet7031C2BA"
}
],
"Tags": {
"foo": "bar",
"super": "salamander"
},
"Type": "EC2",
"UpdateToLatestImageVersion": true
},
"ReplaceComputeEnvironment": false,
"State": "ENABLED",
"Tags": {
"foo": "bar",
"super": "salamander"
},
"UpdatePolicy": {}
}
}
},
"Parameters": {
Expand Down
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}/f47b7d60111f82dad5c04d0bef76e1b62fe75dc319951520566c9d9bce188d10.json",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/6ebdcdec29ca32bb55c4daa83140fbc6af6c8a2663beb1c1a833a3d4c6ee12c0.json",
"requiresBootstrapStackVersion": 6,
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
"additionalDependencies": [
Expand Down Expand Up @@ -291,35 +291,44 @@
"data": "SpotEc2A0470C83"
}
],
"/batch-stack/BootstrapVersion": [
"/batch-stack/taggedCE/SecurityGroup/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "BootstrapVersion"
"data": "taggedCESecurityGroup82CCF59F"
}
],
"/batch-stack/CheckBootstrapVersion": [
"/batch-stack/taggedCE/InstanceProfileRole/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "CheckBootstrapVersion"
"data": "taggedCEInstanceProfileRoleC239DAF9"
}
],
"minimalPropsFargate8E9B9556": [
"/batch-stack/taggedCE/InstanceProfile": [
{
"type": "aws:cdk:logicalId",
"data": "minimalPropsFargate8E9B9556",
"trace": [
"!!DESTRUCTIVE_CHANGES: WILL_DESTROY"
]
"data": "taggedCEInstanceProfileB29F2197"
}
],
"maximalPropsFargateA2E688D8": [
"/batch-stack/taggedCE/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "maximalPropsFargateA2E688D8",
"data": "taggedCE5029E6F8",
"trace": [
"!!DESTRUCTIVE_CHANGES: WILL_DESTROY"
"!!DESTRUCTIVE_CHANGES: WILL_REPLACE"
]
}
],
"/batch-stack/BootstrapVersion": [
{
"type": "aws:cdk:logicalId",
"data": "BootstrapVersion"
}
],
"/batch-stack/CheckBootstrapVersion": [
{
"type": "aws:cdk:logicalId",
"data": "CheckBootstrapVersion"
}
]
},
"displayName": "batch-stack"
Expand Down