Skip to content

Commit

Permalink
feat(autoscaling): allow setting autoscaling group name (#8853)
Browse files Browse the repository at this point in the history
* feat(autoscaling): allow setting autoscaling group name

Co-authored-by: Neta Nir <neta@amazon.com>
  • Loading branch information
NetaNir and Neta Nir authored Jul 7, 2020
1 parent 2961ef9 commit 38d8414
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 34 deletions.
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-autoscaling/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ nyc.config.js
.LAST_PACKAGE
*.snk
!.eslintrc.js

!jest.config.js
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-autoscaling/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ tsconfig.json
.eslintrc.js

# exclude cdk artifacts
**/cdk.out
**/cdk.out
jest.config.js
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-autoscaling/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const baseConfig = require('../../../tools/cdk-build-tools/config/jest.config');
module.exports = baseConfig;
15 changes: 13 additions & 2 deletions packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,14 @@ export interface CommonAutoScalingGroupProps {
* @default - Monitoring.DETAILED
*/
readonly instanceMonitoring?: Monitoring;

/**
* The name of the Auto Scaling group. This name must be unique per Region per account.
*
* @default - Auto generated by CloudFormation
*/
readonly autoScalingGroupName?: string;

}

/**
Expand Down Expand Up @@ -480,7 +488,9 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements
private readonly notifications: NotificationConfiguration[] = [];

constructor(scope: Construct, id: string, props: AutoScalingGroupProps) {
super(scope, id);
super(scope, id, {
physicalName: props.autoScalingGroupName,
});

this.securityGroup = props.securityGroup || new ec2.SecurityGroup(this, 'InstanceSecurityGroup', {
vpc: props.vpc,
Expand Down Expand Up @@ -576,6 +586,7 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements

const { subnetIds, hasPublic } = props.vpc.selectSubnets(props.vpcSubnets);
const asgProps: CfnAutoScalingGroupProps = {
autoScalingGroupName: this.physicalName,
cooldown: props.cooldown !== undefined ? props.cooldown.toSeconds().toString() : undefined,
minSize: Tokenization.stringifyNumber(minCapacity),
maxSize: Tokenization.stringifyNumber(maxCapacity),
Expand All @@ -596,7 +607,7 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements

this.autoScalingGroup = new CfnAutoScalingGroup(this, 'ASG', asgProps);
this.osType = imageConfig.osType;
this.autoScalingGroupName = this.autoScalingGroup.ref;
this.autoScalingGroupName = this.getResourceNameAttribute(this.autoScalingGroup.ref),
this.autoScalingGroupArn = Stack.of(this).formatArn({
service: 'autoscaling',
resource: 'autoScalingGroup:*:autoScalingGroupName',
Expand Down
6 changes: 3 additions & 3 deletions packages/@aws-cdk/aws-autoscaling/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"compat": "cdk-compat"
},
"cdk-build": {
"cloudformation": "AWS::AutoScaling"
"cloudformation": "AWS::AutoScaling",
"jest": true
},
"keywords": [
"aws",
Expand All @@ -64,11 +65,10 @@
"devDependencies": {
"@aws-cdk/assert": "0.0.0",
"@aws-cdk/cx-api": "0.0.0",
"@types/nodeunit": "^0.0.31",
"cdk-build-tools": "0.0.0",
"cdk-integ-tools": "0.0.0",
"cfn2ts": "0.0.0",
"nodeunit": "^0.11.3",
"nodeunit-shim": "0.0.0",
"pkglint": "0.0.0",
"@aws-cdk/cloud-assembly-schema": "0.0.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import * as iam from '@aws-cdk/aws-iam';
import * as sns from '@aws-cdk/aws-sns';
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import * as cdk from '@aws-cdk/core';
import { Test } from 'nodeunit';
import { nodeunitShim, Test } from 'nodeunit-shim';
import * as autoscaling from '../lib';

// tslint:disable:object-literal-key-quotes

export = {
nodeunitShim({
'default fleet'(test: Test) {
const stack = getTestStack();
const vpc = mockVpc(stack);
Expand Down Expand Up @@ -695,7 +695,7 @@ export = {
});

// THEN
test.same(asg.role, importedRole);
test.equal(asg.role, importedRole);
expect(stack).to(haveResource('AWS::IAM::InstanceProfile', {
'Roles': ['HelloDude'],
}));
Expand Down Expand Up @@ -1105,7 +1105,7 @@ export = {
topic,
}],
});
}, 'Can not set notificationsTopic and notifications, notificationsTopic is deprected use notifications instead');
}, 'Cannot set \'notificationsTopic\' and \'notifications\', \'notificationsTopic\' is deprecated use \'notifications\' instead');
test.done();
},

Expand Down Expand Up @@ -1226,7 +1226,7 @@ export = {
test.deepEqual(Object.values(autoscaling.ScalingEvent).length - 1, autoscaling.ScalingEvents.ALL._types.length);
test.done();
},
};
});

function mockVpc(stack: cdk.Stack) {
return ec2.Vpc.fromVpcAttributes(stack, 'MyVpc', {
Expand All @@ -1238,6 +1238,25 @@ function mockVpc(stack: cdk.Stack) {
});
}

test('Can set autoScalingGroupName', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = mockVpc(stack);

// WHEN
new autoscaling.AutoScalingGroup(stack, 'MyASG', {
instanceType: ec2.InstanceType.of(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
autoScalingGroupName: 'MyAsg',
});

// THEN
expect(stack).to(haveResourceLike('AWS::AutoScaling::AutoScalingGroup', {
AutoScalingGroupName: 'MyAsg',
}));
});

function mockSecurityGroup(stack: cdk.Stack) {
return ec2.SecurityGroup.fromSecurityGroupId(stack, 'MySG', 'most-secure');
}
Expand Down
9 changes: 9 additions & 0 deletions packages/@aws-cdk/aws-autoscaling/test/cron.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as autoscaling from '../lib';

test('test utc cron, hour only', () => {
expect(autoscaling.Schedule.cron({ hour: '18', minute: '0' }).expressionString).toEqual('0 18 * * *');
});

test('test utc cron, hour and minute', () => {
expect(autoscaling.Schedule.cron({ hour: '18', minute: '24' }).expressionString).toEqual('24 18 * * *');
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { expect, haveResource, ResourcePart } from '@aws-cdk/assert';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as iam from '@aws-cdk/aws-iam';
import * as cdk from '@aws-cdk/core';
import { Test } from 'nodeunit';
import { nodeunitShim, Test } from 'nodeunit-shim';
import * as autoscaling from '../lib';

export = {
nodeunitShim({
'we can add a lifecycle hook to an ASG'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down Expand Up @@ -68,7 +68,7 @@ export = {

test.done();
},
};
});

class FakeNotificationTarget implements autoscaling.ILifecycleHookTarget {
public bind(_scope: cdk.Construct, lifecycleHook: autoscaling.ILifecycleHook): autoscaling.LifecycleHookTargetConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as elbv2 from '@aws-cdk/aws-elasticloadbalancingv2';
import * as cdk from '@aws-cdk/core';
import { Test } from 'nodeunit';
import { nodeunitShim, Test } from 'nodeunit-shim';
import * as autoscaling from '../lib';

export = {
nodeunitShim({
'target tracking policies': {
'cpu utilization'(test: Test) {
// GIVEN
Expand Down Expand Up @@ -221,7 +221,7 @@ export = {

test.done();
},
};
});

class ASGFixture extends cdk.Construct {
public readonly vpc: ec2.Vpc;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { expect, haveResource, MatchStyle } from '@aws-cdk/assert';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as cdk from '@aws-cdk/core';
import { Test } from 'nodeunit';
import { nodeunitShim, Test } from 'nodeunit-shim';
import * as autoscaling from '../lib';

export = {
nodeunitShim({
'can schedule an action'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down Expand Up @@ -105,7 +105,7 @@ export = {

test.done();
},
};
});

function makeAutoScalingGroup(scope: cdk.Construct) {
const vpc = new ec2.Vpc(scope, 'VPC');
Expand Down
14 changes: 0 additions & 14 deletions packages/@aws-cdk/aws-autoscaling/test/test.cron.ts

This file was deleted.

0 comments on commit 38d8414

Please sign in to comment.