Skip to content

Commit

Permalink
feat: Tagging support for AutoScaling/SecurityGroup (#766)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
moofish32 authored and rix0rrr committed Sep 27, 2018
1 parent 7a8ee2c commit 3d48eb2
Show file tree
Hide file tree
Showing 14 changed files with 329 additions and 183 deletions.
30 changes: 29 additions & 1 deletion packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import cdk = require('@aws-cdk/cdk');

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

/**
* Name tag constant
*/
const NAME_TAG: string = 'Name';

/**
* Properties of a Fleet
*/
Expand Down Expand Up @@ -124,6 +129,11 @@ export interface AutoScalingGroupProps {
* @default 300 (5 minutes)
*/
resourceSignalTimeoutSec?: number;

/**
* The AWS resource tags to associate with the ASG.
*/
tags?: cdk.Tags;
}

/**
Expand All @@ -137,7 +147,7 @@ export interface AutoScalingGroupProps {
*
* The ASG spans all availability zones.
*/
export class AutoScalingGroup extends cdk.Construct implements elb.ILoadBalancerTarget, ec2.IConnectable,
export class AutoScalingGroup extends cdk.Construct implements cdk.ITaggable, elb.ILoadBalancerTarget, ec2.IConnectable,
elbv2.IApplicationLoadBalancerTarget, elbv2.INetworkLoadBalancerTarget {
/**
* The type of OS instances of this fleet are running.
Expand All @@ -154,6 +164,11 @@ export class AutoScalingGroup extends cdk.Construct implements elb.ILoadBalancer
*/
public readonly role: iam.Role;

/**
* Manage tags for this construct and children
*/
public readonly tags: cdk.TagManager;

private readonly userDataLines = new Array<string>();
private readonly autoScalingGroup: cloudformation.AutoScalingGroupResource;
private readonly securityGroup: ec2.SecurityGroupRef;
Expand All @@ -167,6 +182,8 @@ export class AutoScalingGroup extends cdk.Construct implements elb.ILoadBalancer
this.securityGroup = new ec2.SecurityGroup(this, 'InstanceSecurityGroup', { vpc: props.vpc });
this.connections = new ec2.Connections({ securityGroup: this.securityGroup });
this.securityGroups.push(this.securityGroup);
this.tags = new TagManager(this, {initialTags: props.tags});
this.tags.setTag(NAME_TAG, this.path, { overwrite: false });

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

if (props.notificationsTopic) {
Expand Down Expand Up @@ -472,6 +490,16 @@ function renderRollingUpdateConfig(config: RollingUpdateConfiguration = {}): cdk
};
}

class TagManager extends cdk.TagManager {
protected tagFormatResolve(tagGroups: cdk.TagGroups): any {
const tags = {...tagGroups.nonStickyTags, ...tagGroups.ancestorTags, ...tagGroups.stickyTags};
return Object.keys(tags).map( (key) => {
const propagateAtLaunch = !!tagGroups.propagateTags[key] || !!tagGroups.ancestorTags[key];
return {key, value: tags[key], propagateAtLaunch};
});
}
}

/**
* Render a number of seconds to a PTnX string.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,12 @@
}
],
"SecurityGroupIngress": [],
"Tags": [
{
"Key": "Name",
"Value": "aws-cdk-ec2-integ/Fleet"
}
],
"VpcId": {
"Ref": "VPCB9E5F0B4"
}
Expand Down Expand Up @@ -544,6 +550,13 @@
"Ref": "LB8A12904C"
}
],
"Tags": [
{
"Key": "Name",
"PropagateAtLaunch": true,
"Value": "aws-cdk-ec2-integ/Fleet"
}
],
"VPCZoneIdentifier": [
{
"Ref": "VPCPrivateSubnet1Subnet8BCA10E0"
Expand Down Expand Up @@ -576,6 +589,7 @@
"ToPort": 80
}
],
"Tags": [],
"VpcId": {
"Ref": "VPCB9E5F0B4"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,12 @@
}
],
"SecurityGroupIngress": [],
"Tags": [
{
"Key": "Name",
"Value": "aws-cdk-ec2-integ/Fleet"
}
],
"VpcId": {
"Ref": "VPCB9E5F0B4"
}
Expand Down Expand Up @@ -405,6 +411,13 @@
"LaunchConfigurationName": {
"Ref": "FleetLaunchConfig59F79D36"
},
"Tags": [
{
"Key": "Name",
"PropagateAtLaunch": true,
"Value": "aws-cdk-ec2-integ/Fleet"
}
],
"TargetGroupARNs": [
{
"Ref": "LBListenerTargetGroupF04FCF6D"
Expand Down Expand Up @@ -463,6 +476,7 @@
"ToPort": 80
}
],
"Tags": [],
"VpcId": {
"Ref": "VPCB9E5F0B4"
}
Expand Down
Loading

0 comments on commit 3d48eb2

Please sign in to comment.