Skip to content

Commit

Permalink
BREAKING(aws-ec2): move AutoScalingGroup (#608)
Browse files Browse the repository at this point in the history
This construct should have been in the @aws-cdk/aws-autoscaling package,
as that is the CloudFormation namespace name.

This will also help avoid a dependency cycle pending in another PR,
where we want to add a dependency from aws-lambda => aws-ec2.
  • Loading branch information
rix0rrr authored Aug 20, 2018
1 parent a9114c5 commit bca7b85
Show file tree
Hide file tree
Showing 18 changed files with 188 additions and 154 deletions.
5 changes: 3 additions & 2 deletions examples/cdk-examples-typescript/ec2/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import autoscaling = require('@aws-cdk/aws-autoscaling');
import ec2 = require('@aws-cdk/aws-ec2');
import cdk = require('@aws-cdk/cdk');

Expand All @@ -7,7 +8,7 @@ class AppWithVpc extends cdk.Stack {

const vpc = new ec2.VpcNetwork(this, 'MyVpc');

const asg = new ec2.AutoScalingGroup(this, 'MyASG', {
const asg = new autoscaling.AutoScalingGroup(this, 'MyASG', {
vpc,
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.XLarge),
machineImage: new ec2.AmazonLinuxImage()
Expand All @@ -33,7 +34,7 @@ class MyApp extends cdk.Stack {

const vpc = ec2.VpcNetwork.import(this, 'VPC', props.infra.vpc);

const fleet = new ec2.AutoScalingGroup(this, 'MyASG', {
const fleet = new autoscaling.AutoScalingGroup(this, 'MyASG', {
vpc,
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.XLarge),
machineImage: new ec2.AmazonLinuxImage()
Expand Down
1 change: 1 addition & 0 deletions examples/cdk-examples-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"pkglint": "^0.8.2"
},
"dependencies": {
"@aws-cdk/aws-autoscaling": "^0.8.2",
"@aws-cdk/aws-cloudformation": "^0.8.2",
"@aws-cdk/aws-cognito": "^0.8.2",
"@aws-cdk/aws-dynamodb": "^0.8.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"app": "node index",
"context": {
"availability-zones:993655754359:us-east-1": [
"us-east-1a",
"us-east-1b",
"us-east-1c",
"us-east-1d",
"us-east-1e",
"us-east-1f"
],
"availability-zones:585695036304:us-east-1": [
"us-east-1a",
"us-east-1b",
"us-east-1c",
"us-east-1d",
"us-east-1e",
"us-east-1f"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
// support multi-stack deployments since we have no good way of
// ordering stack deployments. So run this test by hand for now
// until we have that.
import autoscaling = require('@aws-cdk/aws-autoscaling');
import ec2 = require('@aws-cdk/aws-ec2');
import cdk = require('@aws-cdk/cdk');
import ec2 = require('../lib');

const app = new cdk.App(process.argv);
const vpcStack = new cdk.Stack(app, 'VPCStack');
Expand All @@ -17,7 +18,7 @@ const appStack = new cdk.Stack(app, 'AppStack');

const importedVpc = ec2.VpcNetworkRef.import(appStack, 'VPC', exportedVpc.export());

const asg = new ec2.AutoScalingGroup(appStack, 'ASG', {
const asg = new autoscaling.AutoScalingGroup(appStack, 'ASG', {
vpc: importedVpc,
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.Burstable2, ec2.InstanceSize.Micro),
machineImage: new ec2.AmazonLinuxImage()
Expand Down
55 changes: 55 additions & 0 deletions packages/@aws-cdk/aws-autoscaling/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,57 @@
## The CDK Construct Library for AWS Auto-Scaling
This module is part of the [AWS Cloud Development Kit](https://github.com/awslabs/aws-cdk) project.

### Fleet

### Auto Scaling Group

An `AutoScalingGroup` represents a number of instances on which you run your code. You
pick the size of the fleet, the instance type and the OS image:

```ts
import ec2 = require('@aws-cdk/aws-ec2');

new ec2.AutoScalingGroup(stack, 'ASG', {
vpc,
instanceType: new ec2.InstanceTypePair(InstanceClass.Burstable2, InstanceSize.Micro),
machineImage: new ec2.LinuxImage({
'us-east-1': 'ami-97785bed'
})
});
```

> NOTE: AutoScalingGroup has an property called `allowAllOutbound` (allowing the instances to contact the
> internet) which is set to `true` by default. Be sure to set this to `false` if you don't want
> your instances to be able to start arbitrary connections.
### AMIs

AMIs control the OS that gets launched when you start your instance.

Depending on the type of AMI, you select it a different way.

The latest version of Windows images are regionally published under labels,
so you can select Windows images like this:

new ec2.WindowsImage(WindowsVersion.WindowsServer2016EnglishNanoBase)

You can select the latest Amazon Linux image like this:

new ec2.AmazonLinuxImage()

Other Linux images are unfortunately not currently published this way, so you have
to supply a region-to-AMI map when creating a Linux image:

machineImage: new ec2.GenericLinuxImage({
'us-east-1': 'ami-97785bed',
'eu-west-1': 'ami-12345678',
// ...
})

> NOTE: Selecting Linux images will change when the information is published in an automatically
> consumable way.
### Allowing Connections

See the documentation of the aws-ec2 package for more information about allowing
connections between resources backed by instances.
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import autoscaling = require('@aws-cdk/aws-autoscaling');
import ec2 = require('@aws-cdk/aws-ec2');
import iam = require('@aws-cdk/aws-iam');
import sns = require('@aws-cdk/aws-sns');
import cdk = require('@aws-cdk/cdk');
import { Connections, IConnectable } from './connections';
import { InstanceType } from './instance-types';
import { ClassicLoadBalancer, IClassicLoadBalancerTarget } from './load-balancer';
import { IMachineImageSource, OperatingSystemType } from './machine-image';
import { SecurityGroup } from './security-group';
import { AllConnections, AnyIPv4 } from './security-group-rule';
import { VpcNetworkRef, VpcPlacementStrategy } from './vpc-ref';

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

/**
* Properties of a Fleet
Expand All @@ -17,7 +12,7 @@ export interface AutoScalingGroupProps {
/**
* Type of instance to launch
*/
instanceType: InstanceType;
instanceType: ec2.InstanceType;

/**
* Minimum number of instances in the fleet
Expand Down Expand Up @@ -46,23 +41,23 @@ export interface AutoScalingGroupProps {
/**
* AMI to launch
*/
machineImage: IMachineImageSource;
machineImage: ec2.IMachineImageSource;

/**
* VPC to launch these instances in.
*/
vpc: VpcNetworkRef;
vpc: ec2.VpcNetworkRef;

/**
* Where to place instances within the VPC
*/
vpcPlacement?: VpcPlacementStrategy;
vpcPlacement?: ec2.VpcPlacementStrategy;

/**
* SNS topic to send notifications about fleet changes
* @default No fleet change notifications will be sent.
*/
notificationsTopic?: sns.cloudformation.TopicResource;
notificationsTopic?: sns.TopicRef;

/**
* Whether the instances can initiate connections to anywhere by default
Expand All @@ -83,35 +78,35 @@ export interface AutoScalingGroupProps {
*
* The ASG spans all availability zones.
*/
export class AutoScalingGroup extends cdk.Construct implements IClassicLoadBalancerTarget, IConnectable {
export class AutoScalingGroup extends cdk.Construct implements ec2.IClassicLoadBalancerTarget, ec2.IConnectable {
/**
* The type of OS instances of this fleet are running.
*/
public readonly osType: OperatingSystemType;
public readonly osType: ec2.OperatingSystemType;

/**
* Allows specify security group connections for instances of this fleet.
*/
public readonly connections: Connections;
public readonly connections: ec2.Connections;

/**
* The IAM role assumed by instances of this fleet.
*/
public readonly role: iam.Role;

private readonly userDataLines = new Array<string>();
private readonly autoScalingGroup: autoscaling.cloudformation.AutoScalingGroupResource;
private readonly securityGroup: SecurityGroup;
private readonly autoScalingGroup: cloudformation.AutoScalingGroupResource;
private readonly securityGroup: ec2.SecurityGroup;
private readonly loadBalancerNames: cdk.Token[] = [];

constructor(parent: cdk.Construct, name: string, props: AutoScalingGroupProps) {
super(parent, name);

this.securityGroup = new SecurityGroup(this, 'InstanceSecurityGroup', { vpc: props.vpc });
this.connections = new Connections({ securityGroup: this.securityGroup });
this.securityGroup = new ec2.SecurityGroup(this, 'InstanceSecurityGroup', { vpc: props.vpc });
this.connections = new ec2.Connections({ securityGroup: this.securityGroup });

if (props.allowAllOutbound !== false) {
this.connections.allowTo(new AnyIPv4(), new AllConnections(), 'Outbound traffic allowed by default');
this.connections.allowTo(new ec2.AnyIPv4(), new ec2.AllConnections(), 'Outbound traffic allowed by default');
}

this.role = new iam.Role(this, 'InstanceRole', {
Expand All @@ -126,7 +121,7 @@ export class AutoScalingGroup extends cdk.Construct implements IClassicLoadBalan
const machineImage = props.machineImage.getImage(this);
const userDataToken = new cdk.Token(() => new cdk.FnBase64((machineImage.os.createUserData(this.userDataLines))));

const launchConfig = new autoscaling.cloudformation.LaunchConfigurationResource(this, 'LaunchConfig', {
const launchConfig = new cloudformation.LaunchConfigurationResource(this, 'LaunchConfig', {
imageId: machineImage.imageId,
keyName: props.keyName,
instanceType: props.instanceType.toString(),
Expand All @@ -141,7 +136,7 @@ export class AutoScalingGroup extends cdk.Construct implements IClassicLoadBalan
const maxSize = props.maxSize || 1;
const desiredCapacity = props.desiredCapacity || 1;

const asgProps: autoscaling.cloudformation.AutoScalingGroupResourceProps = {
const asgProps: cloudformation.AutoScalingGroupResourceProps = {
minSize: minSize.toString(),
maxSize: maxSize.toString(),
desiredCapacity: desiredCapacity.toString(),
Expand All @@ -152,7 +147,7 @@ export class AutoScalingGroup extends cdk.Construct implements IClassicLoadBalan
if (props.notificationsTopic) {
asgProps.notificationConfigurations = [];
asgProps.notificationConfigurations.push({
topicArn: props.notificationsTopic.ref,
topicArn: props.notificationsTopic.topicArn,
notificationTypes: [
"autoscaling:EC2_INSTANCE_LAUNCH",
"autoscaling:EC2_INSTANCE_LAUNCH_ERROR",
Expand All @@ -165,11 +160,11 @@ export class AutoScalingGroup extends cdk.Construct implements IClassicLoadBalan
const subnets = props.vpc.subnets(props.vpcPlacement);
asgProps.vpcZoneIdentifier = subnets.map(n => n.subnetId);

this.autoScalingGroup = new autoscaling.cloudformation.AutoScalingGroupResource(this, 'ASG', asgProps);
this.autoScalingGroup = new cloudformation.AutoScalingGroupResource(this, 'ASG', asgProps);
this.osType = machineImage.os.type;
}

public attachToClassicLB(loadBalancer: ClassicLoadBalancer): void {
public attachToClassicLB(loadBalancer: ec2.ClassicLoadBalancer): void {
this.loadBalancerNames.push(loadBalancer.loadBalancerName);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-autoscaling/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './auto-scaling-group';

// AWS::AutoScaling CloudFormation Resources:
export * from './autoscaling.generated';
6 changes: 5 additions & 1 deletion packages/@aws-cdk/aws-autoscaling/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,15 @@
"devDependencies": {
"@aws-cdk/assert": "^0.8.2",
"cdk-build-tools": "^0.8.2",
"cdk-integ-tools": "^0.8.2",
"cfn2ts": "^0.8.2",
"pkglint": "^0.8.2"
},
"dependencies": {
"@aws-cdk/cdk": "^0.8.2"
"@aws-cdk/cdk": "^0.8.2",
"@aws-cdk/aws-ec2": "^0.8.2",
"@aws-cdk/aws-iam": "^0.8.2",
"@aws-cdk/aws-sns": "^0.8.2"
},
"homepage": "https://github.com/awslabs/aws-cdk"
}
32 changes: 32 additions & 0 deletions packages/@aws-cdk/aws-autoscaling/test/integ.asg-w-loadbalancer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env node
import ec2 = require('@aws-cdk/aws-ec2');
import cdk = require('@aws-cdk/cdk');
import autoscaling = require('../lib');

const app = new cdk.App(process.argv);
const stack = new cdk.Stack(app, 'aws-cdk-ec2-integ');

const vpc = new ec2.VpcNetwork(stack, 'VPC', {
maxAZs: 3
});

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

new ec2.ClassicLoadBalancer(stack, 'LB', {
vpc,
internetFacing: true,
listeners: [{
externalPort: 80,
allowConnectionsFrom: [new ec2.AnyIPv4()]
}],
healthCheck: {
port: 80
},
targets: [asg]
});

process.stdout.write(app.run());
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { expect } from '@aws-cdk/assert';
import { PolicyStatement, Stack } from '@aws-cdk/cdk';
import ec2 = require('@aws-cdk/aws-ec2');
import cdk = require('@aws-cdk/cdk');
import { Test } from 'nodeunit';
import { AmazonLinuxImage, AutoScalingGroup, InstanceClass, InstanceSize, InstanceTypePair, VpcNetwork, VpcNetworkId, VpcSubnetId } from '../lib';
import autoscaling = require('../lib');

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

export = {
'default fleet'(test: Test) {
const stack = new Stack(undefined, 'MyStack', { env: { region: 'us-east-1', account: '1234' }});
const stack = new cdk.Stack(undefined, 'MyStack', { env: { region: 'us-east-1', account: '1234' }});
const vpc = mockVpc(stack);

new AutoScalingGroup(stack, 'MyFleet', {
instanceType: new InstanceTypePair(InstanceClass.M4, InstanceSize.Micro),
machineImage: new AmazonLinuxImage(),
new autoscaling.AutoScalingGroup(stack, 'MyFleet', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.Micro),
machineImage: new ec2.AmazonLinuxImage(),
vpc
});

Expand Down Expand Up @@ -108,16 +109,16 @@ export = {
},

'addToRolePolicy can be used to add statements to the role policy'(test: Test) {
const stack = new Stack(undefined, 'MyStack', { env: { region: 'us-east-1', account: '1234' }});
const stack = new cdk.Stack(undefined, 'MyStack', { env: { region: 'us-east-1', account: '1234' }});
const vpc = mockVpc(stack);

const fleet = new AutoScalingGroup(stack, 'MyFleet', {
instanceType: new InstanceTypePair(InstanceClass.M4, InstanceSize.Micro),
machineImage: new AmazonLinuxImage(),
const fleet = new autoscaling.AutoScalingGroup(stack, 'MyFleet', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.Micro),
machineImage: new ec2.AmazonLinuxImage(),
vpc
});

fleet.addToRolePolicy(new PolicyStatement()
fleet.addToRolePolicy(new cdk.PolicyStatement()
.addAction('*')
.addResource('*'));

Expand Down Expand Up @@ -235,11 +236,11 @@ export = {
},
};

function mockVpc(stack: Stack) {
return VpcNetwork.import(stack, 'MyVpc', {
vpcId: new VpcNetworkId('my-vpc'),
function mockVpc(stack: cdk.Stack) {
return ec2.VpcNetwork.import(stack, 'MyVpc', {
vpcId: new ec2.VpcNetworkId('my-vpc'),
availabilityZones: [ 'az1' ],
publicSubnetIds: [ new VpcSubnetId('pub1') ],
privateSubnetIds: [ new VpcSubnetId('pri1') ],
publicSubnetIds: [ new ec2.VpcSubnetId('pub1') ],
privateSubnetIds: [ new ec2.VpcSubnetId('pri1') ],
});
}
Loading

0 comments on commit bca7b85

Please sign in to comment.