@@ -7,6 +7,11 @@ import cdk = require('@aws-cdk/cdk');
7
7
8
8
import { cloudformation } from './autoscaling.generated' ;
9
9
10
+ /**
11
+ * Name tag constant
12
+ */
13
+ const NAME_TAG : string = 'Name' ;
14
+
10
15
/**
11
16
* Properties of a Fleet
12
17
*/
@@ -124,6 +129,11 @@ export interface AutoScalingGroupProps {
124
129
* @default 300 (5 minutes)
125
130
*/
126
131
resourceSignalTimeoutSec ?: number ;
132
+
133
+ /**
134
+ * The AWS resource tags to associate with the ASG.
135
+ */
136
+ tags ?: cdk . Tags ;
127
137
}
128
138
129
139
/**
@@ -137,7 +147,7 @@ export interface AutoScalingGroupProps {
137
147
*
138
148
* The ASG spans all availability zones.
139
149
*/
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 ,
141
151
elbv2 . IApplicationLoadBalancerTarget , elbv2 . INetworkLoadBalancerTarget {
142
152
/**
143
153
* The type of OS instances of this fleet are running.
@@ -154,6 +164,11 @@ export class AutoScalingGroup extends cdk.Construct implements elb.ILoadBalancer
154
164
*/
155
165
public readonly role : iam . Role ;
156
166
167
+ /**
168
+ * Manage tags for this construct and children
169
+ */
170
+ public readonly tags : cdk . TagManager ;
171
+
157
172
private readonly userDataLines = new Array < string > ( ) ;
158
173
private readonly autoScalingGroup : cloudformation . AutoScalingGroupResource ;
159
174
private readonly securityGroup : ec2 . SecurityGroupRef ;
@@ -167,6 +182,8 @@ export class AutoScalingGroup extends cdk.Construct implements elb.ILoadBalancer
167
182
this . securityGroup = new ec2 . SecurityGroup ( this , 'InstanceSecurityGroup' , { vpc : props . vpc } ) ;
168
183
this . connections = new ec2 . Connections ( { securityGroup : this . securityGroup } ) ;
169
184
this . securityGroups . push ( this . securityGroup ) ;
185
+ this . tags = new TagManager ( this , { initialTags : props . tags } ) ;
186
+ this . tags . setTag ( NAME_TAG , this . path , { overwrite : false } ) ;
170
187
171
188
if ( props . allowAllOutbound !== false ) {
172
189
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
211
228
launchConfigurationName : launchConfig . ref ,
212
229
loadBalancerNames : new cdk . Token ( ( ) => this . loadBalancerNames . length > 0 ? this . loadBalancerNames : undefined ) ,
213
230
targetGroupArns : new cdk . Token ( ( ) => this . targetGroupArns . length > 0 ? this . targetGroupArns : undefined ) ,
231
+ tags : this . tags ,
214
232
} ;
215
233
216
234
if ( props . notificationsTopic ) {
@@ -472,6 +490,16 @@ function renderRollingUpdateConfig(config: RollingUpdateConfiguration = {}): cdk
472
490
} ;
473
491
}
474
492
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
+
475
503
/**
476
504
* Render a number of seconds to a PTnX string.
477
505
*/
0 commit comments