@@ -7,6 +7,11 @@ import cdk = require('@aws-cdk/cdk');
77
88import { 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 */
0 commit comments