Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ec2): EC2 API cleanups #2954

Merged
merged 9 commits into from
Jun 21, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/@aws-cdk/assert/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/@aws-cdk/assets/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 21 additions & 6 deletions packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,16 @@ export interface AutoScalingGroupProps extends CommonAutoScalingGroupProps {
*/
readonly machineImage: ec2.IMachineImageSource;

/**
* Specific UserData to use
*
* The UserData may still be mutated after creation.
*
* @default - A UserData object appropriate for the MachineImage's
* Operating System is created.
*/
readonly userData?: ec2.UserData;

/**
* An IAM role to associate with the instance profile assigned to this Auto Scaling Group.
*
Expand Down Expand Up @@ -354,7 +364,11 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements
*/
public readonly autoScalingGroupArn: string;

private readonly userDataLines = new Array<string>();
/**
* UserData for the instances
*/
public readonly userData: ec2.UserData;

private readonly autoScalingGroup: CfnAutoScalingGroup;
private readonly securityGroup: ec2.ISecurityGroup;
private readonly securityGroups: ec2.ISecurityGroup[] = [];
Expand Down Expand Up @@ -386,7 +400,8 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements

// use delayed evaluation
const machineImage = props.machineImage.getImage(this);
const userDataToken = Lazy.stringValue({ produce: () => Fn.base64((machineImage.os.createUserData(this.userDataLines))) });
this.userData = machineImage.os.createDefaultUserData();
const userDataToken = Lazy.stringValue({ produce: () => Fn.base64(this.userData.render()) });
const securityGroupsToken = Lazy.listValue({ produce: () => this.securityGroups.map(sg => sg.securityGroupId) });

const launchConfig = new CfnLaunchConfiguration(this, 'LaunchConfig', {
Expand All @@ -413,7 +428,7 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements
throw new Error(`Should have minCapacity (${minCapacity}) <= desiredCapacity (${desiredCapacity}) <= maxCapacity (${maxCapacity})`);
}

const { subnetIds } = props.vpc.selectSubnets(props.vpcSubnets);
const { subnetIds, hasPublic } = props.vpc.selectSubnets(props.vpcSubnets);
const asgProps: CfnAutoScalingGroupProps = {
cooldown: props.cooldownSeconds !== undefined ? `${props.cooldownSeconds}` : undefined,
minSize: minCapacity.toString(),
Expand All @@ -436,7 +451,7 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements
vpcZoneIdentifier: subnetIds
};

if (!props.vpc.isPublicSubnets(subnetIds) && props.associatePublicIpAddress) {
if (!hasPublic && props.associatePublicIpAddress) {
throw new Error("To set 'associatePublicIpAddress: true' you must select Public subnets (vpcSubnets: { subnetType: SubnetType.Public })");
}

Expand Down Expand Up @@ -491,8 +506,8 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements
* Add command to the startup script of fleet instances.
* The command must be in the scripting language supported by the fleet's OS (i.e. Linux/Windows).
*/
public addUserData(...scriptLines: string[]) {
scriptLines.forEach(scriptLine => this.userDataLines.push(scriptLine));
public addUserData(...commands: string[]) {
this.userData.addCommands(...commands);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const vpc = new ec2.Vpc(stack, 'VPC', {

new autoscaling.AutoScalingGroup(stack, 'Fleet', {
vpc,
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage({ generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2 }),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const vpc = new ec2.Vpc(stack, 'VPC', {

const asg = new autoscaling.AutoScalingGroup(stack, 'Fleet', {
vpc,
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const vpc = new ec2.Vpc(stack, 'VPC', {

const asg = new autoscaling.AutoScalingGroup(stack, 'Fleet', {
vpc,
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const vpc = new ec2.Vpc(stack, 'VPC', {

const asg = new autoscaling.AutoScalingGroup(stack, 'Fleet', {
vpc,
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage({ generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2 }),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TestStack extends cdk.Stack {
});

new asg.AutoScalingGroup(this, 'ASG', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.T2, ec2.InstanceSize.MICRO),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.T2, ec2.InstanceSize.MICRO),
vpc,
machineImage: new ec2.AmazonLinuxImage(),
role
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const vpc = new ec2.Vpc(stack, 'VPC', {

new autoscaling.AutoScalingGroup(stack, 'Fleet', {
vpc,
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage({ generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2 }),
spotPrice: '0.20'
});
Expand Down
34 changes: 17 additions & 17 deletions packages/@aws-cdk/aws-autoscaling/test/test.auto-scaling-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export = {
const vpc = mockVpc(stack);

new autoscaling.AutoScalingGroup(stack, 'MyFleet', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
vpc
});
Expand Down Expand Up @@ -137,7 +137,7 @@ export = {
const vpc = mockVpc(stack);

new autoscaling.AutoScalingGroup(stack, 'MyFleet', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
minCapacity: 0,
Expand All @@ -162,7 +162,7 @@ export = {

// WHEN
new autoscaling.AutoScalingGroup(stack, 'MyFleet', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
minCapacity: 10
Expand All @@ -186,7 +186,7 @@ export = {

// WHEN
new autoscaling.AutoScalingGroup(stack, 'MyFleet', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
maxCapacity: 10
Expand All @@ -210,7 +210,7 @@ export = {

// WHEN
new autoscaling.AutoScalingGroup(stack, 'MyFleet', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
desiredCapacity: 10
Expand All @@ -232,7 +232,7 @@ export = {
const vpc = mockVpc(stack);

const fleet = new autoscaling.AutoScalingGroup(stack, 'MyFleet', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
vpc
});
Expand Down Expand Up @@ -264,7 +264,7 @@ export = {

// WHEN
new autoscaling.AutoScalingGroup(stack, 'MyFleet', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
updateType: autoscaling.UpdateType.REPLACING_UPDATE,
Expand Down Expand Up @@ -295,7 +295,7 @@ export = {

// WHEN
new autoscaling.AutoScalingGroup(stack, 'MyFleet', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
updateType: autoscaling.UpdateType.ROLLING_UPDATE,
Expand Down Expand Up @@ -327,7 +327,7 @@ export = {

// WHEN
new autoscaling.AutoScalingGroup(stack, 'MyFleet', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
resourceSignalCount: 5,
Expand All @@ -354,7 +354,7 @@ export = {

// WHEN
const asg = new autoscaling.AutoScalingGroup(stack, 'MyFleet', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
});
Expand All @@ -380,7 +380,7 @@ export = {

// WHEN
const asg = new autoscaling.AutoScalingGroup(stack, 'MyFleet', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
updateType: autoscaling.UpdateType.ROLLING_UPDATE,
Expand Down Expand Up @@ -422,7 +422,7 @@ export = {

// WHEN
new autoscaling.AutoScalingGroup(stack, 'MyStack', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
vpc,

Expand All @@ -444,7 +444,7 @@ export = {

// WHEN
new autoscaling.AutoScalingGroup(stack, 'MyStack', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
minCapacity: 0,
Expand All @@ -471,7 +471,7 @@ export = {
// WHEN
test.throws(() => {
new autoscaling.AutoScalingGroup(stack, 'MyStack', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
minCapacity: 0,
Expand All @@ -490,7 +490,7 @@ export = {

// WHEN
new autoscaling.AutoScalingGroup(stack, 'MyStack', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
minCapacity: 0,
Expand All @@ -514,7 +514,7 @@ export = {

// WHEN
new autoscaling.AutoScalingGroup(stack, 'MyStack', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
minCapacity: 0,
Expand Down Expand Up @@ -544,7 +544,7 @@ export = {
// WHEN
const asg = new autoscaling.AutoScalingGroup(stack, 'MyASG', {
vpc,
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
role: importedRole
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export = {
const vpc = new ec2.Vpc(stack, 'VPC');
const asg = new autoscaling.AutoScalingGroup(stack, 'ASG', {
vpc,
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
});

Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-autoscaling/test/test.scaling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class ASGFixture extends cdk.Construct {
this.vpc = new ec2.Vpc(this, 'VPC');
this.asg = new autoscaling.AutoScalingGroup(this, 'ASG', {
vpc: this.vpc,
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
});
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-cloudformation/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const stack = new cdk.Stack(app, 'aws-cdk-codedeploy-server-dg');
const vpc = new ec2.Vpc(stack, 'VPC');

const asg = new autoscaling.AutoScalingGroup(stack, 'ASG', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M5, ec2.InstanceSize.LARGE),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.M5, ec2.InstanceSize.LARGE),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export = {
const stack = new cdk.Stack();

const asg = new autoscaling.AutoScalingGroup(stack, 'ASG', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.STANDARD3, ec2.InstanceSize.SMALL),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.STANDARD3, ec2.InstanceSize.SMALL),
machineImage: new ec2.AmazonLinuxImage(),
vpc: new ec2.Vpc(stack, 'VPC'),
});
Expand All @@ -70,7 +70,7 @@ export = {
const stack = new cdk.Stack();

const asg = new autoscaling.AutoScalingGroup(stack, 'ASG', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.STANDARD3, ec2.InstanceSize.SMALL),
instanceType: ec2.InstanceType.pair(ec2.InstanceClass.STANDARD3, ec2.InstanceSize.SMALL),
machineImage: new ec2.AmazonLinuxImage(),
vpc: new ec2.Vpc(stack, 'VPC'),
});
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading