Skip to content

Commit 3d48eb2

Browse files
moofish32rix0rrr
authored andcommitted
feat: Tagging support for AutoScaling/SecurityGroup (#766)
Add support for tagging of AutoScalingGroups and Security Groups. BREAKING CHANGE: constructor signature of `TagManager` has changed. `initialTags` is now passed inside a props object.
1 parent 7a8ee2c commit 3d48eb2

File tree

14 files changed

+329
-183
lines changed

14 files changed

+329
-183
lines changed

Diff for: packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts

+29-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import cdk = require('@aws-cdk/cdk');
77

88
import { cloudformation } from './autoscaling.generated';
99

10+
/**
11+
* Name tag constant
12+
*/
13+
const NAME_TAG: string = 'Name';
14+
1015
/**
1116
* Properties of a Fleet
1217
*/
@@ -124,6 +129,11 @@ export interface AutoScalingGroupProps {
124129
* @default 300 (5 minutes)
125130
*/
126131
resourceSignalTimeoutSec?: number;
132+
133+
/**
134+
* The AWS resource tags to associate with the ASG.
135+
*/
136+
tags?: cdk.Tags;
127137
}
128138

129139
/**
@@ -137,7 +147,7 @@ export interface AutoScalingGroupProps {
137147
*
138148
* The ASG spans all availability zones.
139149
*/
140-
export class AutoScalingGroup extends cdk.Construct implements elb.ILoadBalancerTarget, ec2.IConnectable,
150+
export class AutoScalingGroup extends cdk.Construct implements cdk.ITaggable, elb.ILoadBalancerTarget, ec2.IConnectable,
141151
elbv2.IApplicationLoadBalancerTarget, elbv2.INetworkLoadBalancerTarget {
142152
/**
143153
* The type of OS instances of this fleet are running.
@@ -154,6 +164,11 @@ export class AutoScalingGroup extends cdk.Construct implements elb.ILoadBalancer
154164
*/
155165
public readonly role: iam.Role;
156166

167+
/**
168+
* Manage tags for this construct and children
169+
*/
170+
public readonly tags: cdk.TagManager;
171+
157172
private readonly userDataLines = new Array<string>();
158173
private readonly autoScalingGroup: cloudformation.AutoScalingGroupResource;
159174
private readonly securityGroup: ec2.SecurityGroupRef;
@@ -167,6 +182,8 @@ export class AutoScalingGroup extends cdk.Construct implements elb.ILoadBalancer
167182
this.securityGroup = new ec2.SecurityGroup(this, 'InstanceSecurityGroup', { vpc: props.vpc });
168183
this.connections = new ec2.Connections({ securityGroup: this.securityGroup });
169184
this.securityGroups.push(this.securityGroup);
185+
this.tags = new TagManager(this, {initialTags: props.tags});
186+
this.tags.setTag(NAME_TAG, this.path, { overwrite: false });
170187

171188
if (props.allowAllOutbound !== false) {
172189
this.connections.allowTo(new ec2.AnyIPv4(), new ec2.AllConnections(), 'Outbound traffic allowed by default');
@@ -211,6 +228,7 @@ export class AutoScalingGroup extends cdk.Construct implements elb.ILoadBalancer
211228
launchConfigurationName: launchConfig.ref,
212229
loadBalancerNames: new cdk.Token(() => this.loadBalancerNames.length > 0 ? this.loadBalancerNames : undefined),
213230
targetGroupArns: new cdk.Token(() => this.targetGroupArns.length > 0 ? this.targetGroupArns : undefined),
231+
tags: this.tags,
214232
};
215233

216234
if (props.notificationsTopic) {
@@ -472,6 +490,16 @@ function renderRollingUpdateConfig(config: RollingUpdateConfiguration = {}): cdk
472490
};
473491
}
474492

493+
class TagManager extends cdk.TagManager {
494+
protected tagFormatResolve(tagGroups: cdk.TagGroups): any {
495+
const tags = {...tagGroups.nonStickyTags, ...tagGroups.ancestorTags, ...tagGroups.stickyTags};
496+
return Object.keys(tags).map( (key) => {
497+
const propagateAtLaunch = !!tagGroups.propagateTags[key] || !!tagGroups.ancestorTags[key];
498+
return {key, value: tags[key], propagateAtLaunch};
499+
});
500+
}
501+
}
502+
475503
/**
476504
* Render a number of seconds to a PTnX string.
477505
*/

Diff for: packages/@aws-cdk/aws-autoscaling/test/integ.asg-w-classic-loadbalancer.expected.json

+14
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,12 @@
453453
}
454454
],
455455
"SecurityGroupIngress": [],
456+
"Tags": [
457+
{
458+
"Key": "Name",
459+
"Value": "aws-cdk-ec2-integ/Fleet"
460+
}
461+
],
456462
"VpcId": {
457463
"Ref": "VPCB9E5F0B4"
458464
}
@@ -544,6 +550,13 @@
544550
"Ref": "LB8A12904C"
545551
}
546552
],
553+
"Tags": [
554+
{
555+
"Key": "Name",
556+
"PropagateAtLaunch": true,
557+
"Value": "aws-cdk-ec2-integ/Fleet"
558+
}
559+
],
547560
"VPCZoneIdentifier": [
548561
{
549562
"Ref": "VPCPrivateSubnet1Subnet8BCA10E0"
@@ -576,6 +589,7 @@
576589
"ToPort": 80
577590
}
578591
],
592+
"Tags": [],
579593
"VpcId": {
580594
"Ref": "VPCB9E5F0B4"
581595
}

Diff for: packages/@aws-cdk/aws-autoscaling/test/integ.asg-w-elbv2.expected.json

+14
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,12 @@
319319
}
320320
],
321321
"SecurityGroupIngress": [],
322+
"Tags": [
323+
{
324+
"Key": "Name",
325+
"Value": "aws-cdk-ec2-integ/Fleet"
326+
}
327+
],
322328
"VpcId": {
323329
"Ref": "VPCB9E5F0B4"
324330
}
@@ -405,6 +411,13 @@
405411
"LaunchConfigurationName": {
406412
"Ref": "FleetLaunchConfig59F79D36"
407413
},
414+
"Tags": [
415+
{
416+
"Key": "Name",
417+
"PropagateAtLaunch": true,
418+
"Value": "aws-cdk-ec2-integ/Fleet"
419+
}
420+
],
408421
"TargetGroupARNs": [
409422
{
410423
"Ref": "LBListenerTargetGroupF04FCF6D"
@@ -463,6 +476,7 @@
463476
"ToPort": 80
464477
}
465478
],
479+
"Tags": [],
466480
"VpcId": {
467481
"Ref": "VPCB9E5F0B4"
468482
}

0 commit comments

Comments
 (0)