diff --git a/examples/cdk-examples-typescript/ec2/index.ts b/examples/cdk-examples-typescript/ec2/index.ts index cbefbfc325d11..3153c7270041d 100644 --- a/examples/cdk-examples-typescript/ec2/index.ts +++ b/examples/cdk-examples-typescript/ec2/index.ts @@ -1,3 +1,4 @@ +import autoscaling = require('@aws-cdk/aws-autoscaling'); import ec2 = require('@aws-cdk/aws-ec2'); import cdk = require('@aws-cdk/cdk'); @@ -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() @@ -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() diff --git a/examples/cdk-examples-typescript/package.json b/examples/cdk-examples-typescript/package.json index 25b7b6c879a17..3a2ef91dcbf36 100644 --- a/examples/cdk-examples-typescript/package.json +++ b/examples/cdk-examples-typescript/package.json @@ -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", diff --git a/examples/cdk-examples-typescript/use-vpc-from-another-stack/cdk.json b/examples/cdk-examples-typescript/use-vpc-from-another-stack/cdk.json new file mode 100644 index 0000000000000..b75186eabef85 --- /dev/null +++ b/examples/cdk-examples-typescript/use-vpc-from-another-stack/cdk.json @@ -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" + ] + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-ec2/test/demo.split-stack-vpc.ts b/examples/cdk-examples-typescript/use-vpc-from-another-stack/index.ts similarity index 86% rename from packages/@aws-cdk/aws-ec2/test/demo.split-stack-vpc.ts rename to examples/cdk-examples-typescript/use-vpc-from-another-stack/index.ts index 0c3f21fb92d98..f62a5648d68a3 100644 --- a/packages/@aws-cdk/aws-ec2/test/demo.split-stack-vpc.ts +++ b/examples/cdk-examples-typescript/use-vpc-from-another-stack/index.ts @@ -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'); @@ -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() diff --git a/packages/@aws-cdk/aws-autoscaling/README.md b/packages/@aws-cdk/aws-autoscaling/README.md index 0846a7231d7c0..f22770f712253 100644 --- a/packages/@aws-cdk/aws-autoscaling/README.md +++ b/packages/@aws-cdk/aws-autoscaling/README.md @@ -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. diff --git a/packages/@aws-cdk/aws-ec2/lib/auto-scaling-group.ts b/packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts similarity index 72% rename from packages/@aws-cdk/aws-ec2/lib/auto-scaling-group.ts rename to packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts index 2da8662dff415..f1596d5572d9b 100644 --- a/packages/@aws-cdk/aws-ec2/lib/auto-scaling-group.ts +++ b/packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts @@ -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 @@ -17,7 +12,7 @@ export interface AutoScalingGroupProps { /** * Type of instance to launch */ - instanceType: InstanceType; + instanceType: ec2.InstanceType; /** * Minimum number of instances in the fleet @@ -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 @@ -83,16 +78,16 @@ 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. @@ -100,18 +95,18 @@ export class AutoScalingGroup extends cdk.Construct implements IClassicLoadBalan public readonly role: iam.Role; private readonly userDataLines = new Array(); - 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', { @@ -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(), @@ -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(), @@ -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", @@ -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); } diff --git a/packages/@aws-cdk/aws-autoscaling/lib/index.ts b/packages/@aws-cdk/aws-autoscaling/lib/index.ts index 587f116115a71..242533a57cc84 100644 --- a/packages/@aws-cdk/aws-autoscaling/lib/index.ts +++ b/packages/@aws-cdk/aws-autoscaling/lib/index.ts @@ -1,2 +1,4 @@ +export * from './auto-scaling-group'; + // AWS::AutoScaling CloudFormation Resources: export * from './autoscaling.generated'; diff --git a/packages/@aws-cdk/aws-autoscaling/package.json b/packages/@aws-cdk/aws-autoscaling/package.json index ee345dd26cf23..d5d132ceab7c6 100644 --- a/packages/@aws-cdk/aws-autoscaling/package.json +++ b/packages/@aws-cdk/aws-autoscaling/package.json @@ -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" } diff --git a/packages/@aws-cdk/aws-ec2/test/integ.everything.expected.json b/packages/@aws-cdk/aws-autoscaling/test/integ.asg-w-loadbalancer.expected.json similarity index 100% rename from packages/@aws-cdk/aws-ec2/test/integ.everything.expected.json rename to packages/@aws-cdk/aws-autoscaling/test/integ.asg-w-loadbalancer.expected.json diff --git a/packages/@aws-cdk/aws-autoscaling/test/integ.asg-w-loadbalancer.ts b/packages/@aws-cdk/aws-autoscaling/test/integ.asg-w-loadbalancer.ts new file mode 100644 index 0000000000000..5a31c3c95700d --- /dev/null +++ b/packages/@aws-cdk/aws-autoscaling/test/integ.asg-w-loadbalancer.ts @@ -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()); diff --git a/packages/@aws-cdk/aws-ec2/test/test.fleet.ts b/packages/@aws-cdk/aws-autoscaling/test/test.auto-scaling-group.ts similarity index 87% rename from packages/@aws-cdk/aws-ec2/test/test.fleet.ts rename to packages/@aws-cdk/aws-autoscaling/test/test.auto-scaling-group.ts index 71314266b7f01..207ab9ffcd817 100644 --- a/packages/@aws-cdk/aws-ec2/test/test.fleet.ts +++ b/packages/@aws-cdk/aws-autoscaling/test/test.auto-scaling-group.ts @@ -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 }); @@ -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('*')); @@ -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') ], }); } diff --git a/packages/@aws-cdk/aws-autoscaling/test/test.autoscaling.ts b/packages/@aws-cdk/aws-autoscaling/test/test.autoscaling.ts deleted file mode 100644 index 2222a288762d5..0000000000000 --- a/packages/@aws-cdk/aws-autoscaling/test/test.autoscaling.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { Test, testCase } from 'nodeunit'; -import {} from '../lib'; - -exports = testCase({ - notTested(test: Test) { - test.ok(true, 'No tests are specified for this package.'); - test.done(); - } -}); diff --git a/packages/@aws-cdk/aws-ec2/README.md b/packages/@aws-cdk/aws-ec2/README.md index a90085d5ee690..a357f5a93558f 100644 --- a/packages/@aws-cdk/aws-ec2/README.md +++ b/packages/@aws-cdk/aws-ec2/README.md @@ -172,55 +172,6 @@ The `VpcNetwork` above will have the exact same subnet definitions as listed above. However, this time the VPC will have only 1 NAT Gateway and all Application subnets will route to the NAT Gateway. -### 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. ### Load Balancer diff --git a/packages/@aws-cdk/aws-ec2/lib/index.ts b/packages/@aws-cdk/aws-ec2/lib/index.ts index c293d73b5da50..28bd413c5a9cf 100644 --- a/packages/@aws-cdk/aws-ec2/lib/index.ts +++ b/packages/@aws-cdk/aws-ec2/lib/index.ts @@ -1,4 +1,3 @@ -export * from './auto-scaling-group'; export * from './connections'; export * from './instance-types'; export * from './load-balancer'; diff --git a/packages/@aws-cdk/aws-ec2/package.json b/packages/@aws-cdk/aws-ec2/package.json index f1d3ec2c0d6ce..02896585ccfa6 100644 --- a/packages/@aws-cdk/aws-ec2/package.json +++ b/packages/@aws-cdk/aws-ec2/package.json @@ -53,10 +53,8 @@ "pkglint": "^0.8.2" }, "dependencies": { - "@aws-cdk/aws-autoscaling": "^0.8.2", "@aws-cdk/aws-elasticloadbalancing": "^0.8.2", "@aws-cdk/aws-iam": "^0.8.2", - "@aws-cdk/aws-sns": "^0.8.2", "@aws-cdk/cdk": "^0.8.2", "@aws-cdk/util": "^0.8.2" }, diff --git a/packages/@aws-cdk/aws-ec2/test/integ.everything.ts b/packages/@aws-cdk/aws-ec2/test/integ.everything.ts deleted file mode 100644 index 2c8e2369570bf..0000000000000 --- a/packages/@aws-cdk/aws-ec2/test/integ.everything.ts +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env node -import { App, Stack } from '@aws-cdk/cdk'; -import { AmazonLinuxImage, AnyIPv4, AutoScalingGroup, ClassicLoadBalancer, InstanceClass, - InstanceSize, InstanceTypePair, VpcNetwork } from '../lib'; - -const app = new App(process.argv); -const stack = new Stack(app, 'aws-cdk-ec2-integ'); - -const vpc = new VpcNetwork(stack, 'VPC', { - maxAZs: 3 -}); - -const asg = new AutoScalingGroup(stack, 'Fleet', { - vpc, - instanceType: new InstanceTypePair(InstanceClass.Burstable2, InstanceSize.Micro), - machineImage: new AmazonLinuxImage(), -}); - -new ClassicLoadBalancer(stack, 'LB', { - vpc, - internetFacing: true, - listeners: [{ - externalPort: 80, - allowConnectionsFrom: [new AnyIPv4()] - }], - healthCheck: { - port: 80 - }, - targets: [asg] -}); - -process.stdout.write(app.run()); diff --git a/packages/@aws-cdk/aws-ec2/test/test.connections.ts b/packages/@aws-cdk/aws-ec2/test/test.connections.ts index 5f9e3d0e36045..c6cbbb6ae7d40 100644 --- a/packages/@aws-cdk/aws-ec2/test/test.connections.ts +++ b/packages/@aws-cdk/aws-ec2/test/test.connections.ts @@ -1,8 +1,7 @@ import { expect, haveResource } from '@aws-cdk/assert'; import { Stack } from '@aws-cdk/cdk'; import { Test } from 'nodeunit'; -import { AmazonLinuxImage, AutoScalingGroup, Connections, IConnectable, InstanceType, SecurityGroup, - SecurityGroupId, SecurityGroupRef, TcpAllPorts, TcpPort, VpcNetwork } from '../lib'; +import { Connections, IConnectable, SecurityGroup, SecurityGroupId, SecurityGroupRef, TcpAllPorts, TcpPort, VpcNetwork } from '../lib'; export = { 'peering between two security groups does not recursive infinitely'(test: Test) { @@ -13,8 +12,8 @@ export = { const sg1 = new SecurityGroup(stack, 'SG1', { vpc }); const sg2 = new SecurityGroup(stack, 'SG2', { vpc }); - const conn1 = new ConnectionsHolder(new Connections({ securityGroup: sg1 })); - const conn2 = new ConnectionsHolder(new Connections({ securityGroup: sg2 })); + const conn1 = new SomethingConnectable(new Connections({ securityGroup: sg1 })); + const conn2 = new SomethingConnectable(new Connections({ securityGroup: sg2 })); // WHEN conn1.connections.allowTo(conn2, new TcpPort(80), 'Test'); @@ -27,20 +26,17 @@ export = { // GIVEN const stack = new Stack(); const vpc = new VpcNetwork(stack, 'VPC'); - const asg = new AutoScalingGroup(stack, 'ASG', { - instanceType: new InstanceType('t-1000'), - machineImage: new AmazonLinuxImage(), - vpc - }); + const sg1 = new SecurityGroup(stack, 'SomeSecurityGroup', { vpc }); + const somethingConnectable = new SomethingConnectable(new Connections({ securityGroup: sg1 })); const securityGroup = SecurityGroupRef.import(stack, 'ImportedSG', { securityGroupId: new SecurityGroupId('sg-12345') }); // WHEN - asg.connections.allowTo(securityGroup, new TcpAllPorts(), 'Connect there'); + somethingConnectable.connections.allowTo(securityGroup, new TcpAllPorts(), 'Connect there'); // THEN: rule to generated security group to connect to imported expect(stack).to(haveResource("AWS::EC2::SecurityGroupEgress", { - GroupId: { "Fn::GetAtt": [ "ASGInstanceSecurityGroup0525485D", "GroupId" ] }, + GroupId: { "Fn::GetAtt": [ "SomeSecurityGroupEF219AD6", "GroupId" ] }, IpProtocol: "tcp", Description: "Connect there", DestinationSecurityGroupId: "sg-12345", @@ -54,7 +50,7 @@ export = { Description: "Connect there", FromPort: 0, GroupId: "sg-12345", - SourceSecurityGroupId: { "Fn::GetAtt": [ "ASGInstanceSecurityGroup0525485D", "GroupId" ] }, + SourceSecurityGroupId: { "Fn::GetAtt": [ "SomeSecurityGroupEF219AD6", "GroupId" ] }, ToPort: 65535 })); @@ -62,7 +58,7 @@ export = { } }; -class ConnectionsHolder implements IConnectable { +class SomethingConnectable implements IConnectable { constructor(public readonly connections: Connections) { } } diff --git a/packages/@aws-cdk/aws-ec2/test/test.vpc.ts b/packages/@aws-cdk/aws-ec2/test/test.vpc.ts index 81c51eeba65c9..64204e1873016 100644 --- a/packages/@aws-cdk/aws-ec2/test/test.vpc.ts +++ b/packages/@aws-cdk/aws-ec2/test/test.vpc.ts @@ -1,5 +1,5 @@ import { countResources, expect, haveResource } from '@aws-cdk/assert'; -import { AvailabilityZoneProvider, Stack } from '@aws-cdk/cdk'; +import { AvailabilityZoneProvider, resolve, Stack } from '@aws-cdk/cdk'; import { Test } from 'nodeunit'; import { DefaultInstanceTenancy, SubnetType, VpcNetwork } from '../lib'; @@ -257,6 +257,24 @@ export = { } }, + + 'export/import'(test: Test) { + // GIVEN + const stack1 = getTestStack(); + const stack2 = getTestStack(); + + const vpc1 = new VpcNetwork(stack1, 'TheVPC', { cidr: '192.168.0.0/16' }); + + // WHEN + const vpc2 = VpcNetwork.import(stack2, 'VPC2', vpc1.export()); + + // THEN + test.deepEqual(resolve(vpc2.vpcId), { + 'Fn::ImportValue': 'TestStack:TheVPCVpcIdD346CDBA' + }); + + test.done(); + } }; function getTestStack(): Stack {