-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(aws-ec2): add VPC context provider
Add a context provider for looking up existing VPCs in an account. This is useful if the VPC is defined outside of your CDK app, such as in a different CDK app, by hand or in a CloudFormation template. Addresses some need of #1095.
- Loading branch information
Showing
20 changed files
with
621 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import cdk = require('@aws-cdk/cdk'); | ||
import cxapi = require('@aws-cdk/cx-api'); | ||
import { VpcNetworkRefProps } from './vpc-ref'; | ||
|
||
/** | ||
* Properties for looking up an existing VPC. | ||
* | ||
* The combination of properties must specify filter down to exactly one | ||
* non-default VPC, otherwise an error is raised. | ||
*/ | ||
export interface VpcNetworkProviderProps { | ||
/** | ||
* The ID of the VPC | ||
* | ||
* If given, will import exactly this VPC. | ||
* | ||
* @default Don't filter on vpcId | ||
*/ | ||
vpcId?: string; | ||
|
||
/** | ||
* The name of the VPC | ||
* | ||
* If given, will import the VPC with this name. | ||
* | ||
* @default Don't filter on vpcName | ||
*/ | ||
vpcName?: string; | ||
|
||
/** | ||
* Tags on the VPC | ||
* | ||
* The VPC must have all of these tags | ||
* | ||
* @default Don't filter on tags | ||
*/ | ||
tags?: {[key: string]: string}; | ||
|
||
/** | ||
* Whether to match the default VPC | ||
* | ||
* @default Don't care whether we return the default VPC | ||
*/ | ||
isDefault?: boolean; | ||
} | ||
|
||
/** | ||
* Context provider to discover and import existing VPCs | ||
*/ | ||
export class VpcNetworkProvider { | ||
private provider: cdk.ContextProvider; | ||
|
||
constructor(context: cdk.Construct, props: VpcNetworkProviderProps) { | ||
this.provider = new cdk.ContextProvider(context, cxapi.VPC_PROVIDER, props as cxapi.VpcContextQuery); | ||
} | ||
|
||
/** | ||
* Return the VPC import props matching the filter | ||
*/ | ||
public get vpcProps(): VpcNetworkRefProps { | ||
const ret: cxapi.VpcContextResponse = this.provider.getValue(DUMMY_VPC_PROPS); | ||
return ret; | ||
} | ||
} | ||
|
||
/** | ||
* There are returned when the provider has not supplied props yet | ||
* | ||
* It's only used for testing and on the first run-through. | ||
*/ | ||
const DUMMY_VPC_PROPS: cxapi.VpcContextResponse = { | ||
availabilityZones: ['dummy-1a', 'dummy-1b'], | ||
vpcId: 'vpc-12345', | ||
publicSubnetIds: ['s-12345', 's-67890'], | ||
privateSubnetIds: ['p-12345', 'p-67890'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import cdk = require('@aws-cdk/cdk'); | ||
import ec2 = require("../lib"); | ||
|
||
const app = new cdk.App(); | ||
|
||
/// !show | ||
class Stack1 extends cdk.Stack { | ||
public readonly vpcProps: ec2.VpcNetworkRefProps; | ||
|
||
constructor(parent: cdk.App, id: string, props?: cdk.StackProps) { | ||
super(parent, id, props); | ||
|
||
const vpc = new ec2.VpcNetwork(this, 'VPC'); | ||
|
||
// Export the VPC to a set of properties | ||
this.vpcProps = vpc.export(); | ||
} | ||
} | ||
|
||
interface Stack2Props extends cdk.StackProps { | ||
vpcProps: ec2.VpcNetworkRefProps; | ||
} | ||
|
||
class Stack2 extends cdk.Stack { | ||
constructor(parent: cdk.App, id: string, props: Stack2Props) { | ||
super(parent, id, props); | ||
|
||
// Import the VPC from a set of properties | ||
const vpc = ec2.VpcNetworkRef.import(this, 'VPC', props.vpcProps); | ||
} | ||
} | ||
|
||
const stack1 = new Stack1(app, 'Stack1'); | ||
const stack2 = new Stack2(app, 'Stack2', { | ||
vpcProps: stack1.vpcProps | ||
}); | ||
/// !hide | ||
|
||
app.run(); | ||
|
33 changes: 33 additions & 0 deletions
33
packages/@aws-cdk/aws-ec2/test/integ.import-default-vpc.lit.expected.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"Resources": { | ||
"SecurityGroupDD263621": { | ||
"Type": "AWS::EC2::SecurityGroup", | ||
"Properties": { | ||
"GroupDescription": "aws-cdk-ec2-import/SecurityGroup", | ||
"SecurityGroupEgress": [ | ||
{ | ||
"CidrIp": "0.0.0.0/0", | ||
"Description": "Allow all outbound traffic by default", | ||
"IpProtocol": "-1" | ||
} | ||
], | ||
"SecurityGroupIngress": [], | ||
"VpcId": "vpc-60900905" | ||
} | ||
} | ||
}, | ||
"Outputs": { | ||
"PublicSubnets": { | ||
"Value": "ids:subnet-e19455ca,subnet-e0c24797,subnet-ccd77395", | ||
"Export": { | ||
"Name": "aws-cdk-ec2-import:PublicSubnets" | ||
} | ||
}, | ||
"PrivateSubnets": { | ||
"Value": "ids:", | ||
"Export": { | ||
"Name": "aws-cdk-ec2-import:PrivateSubnets" | ||
} | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
packages/@aws-cdk/aws-ec2/test/integ.import-default-vpc.lit.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import cdk = require('@aws-cdk/cdk'); | ||
import ec2 = require("../lib"); | ||
|
||
const app = new cdk.App(); | ||
const stack = new cdk.Stack(app, 'aws-cdk-ec2-import'); | ||
|
||
/// !show | ||
const vpc = ec2.VpcNetworkRef.importFromContext(stack, 'VPC', { | ||
// This imports the default VPC but you can also | ||
// specify a 'vpcName' or 'tags'. | ||
isDefault: true | ||
}); | ||
/// !hide | ||
|
||
// The only thing in this library that takes a VPC as an argument :) | ||
new ec2.SecurityGroup(stack, 'SecurityGroup', { | ||
vpc | ||
}); | ||
|
||
// Try subnet selection | ||
new cdk.Output(stack, 'PublicSubnets', { value: 'ids:' + vpc.subnets({ subnetsToUse: ec2.SubnetType.Public }).map(s => s.subnetId).join(',') }); | ||
new cdk.Output(stack, 'PrivateSubnets', { value: 'ids:' + vpc.subnets({ subnetsToUse: ec2.SubnetType.Private }).map(s => s.subnetId).join(',') }); | ||
|
||
app.run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export const VPC_PROVIDER = 'vpc-provider'; | ||
|
||
export interface VpcContextQuery { | ||
region: string; | ||
account: string; | ||
vpcId?: string; | ||
vpcName?: string; | ||
tags?: {[key: string]: string}; | ||
isDefault?: boolean; | ||
} | ||
|
||
export interface VpcContextResponse { | ||
vpcId: string; | ||
availabilityZones: string[]; | ||
publicSubnetIds?: string[]; | ||
publicSubnetNames?: string[]; | ||
privateSubnetIds?: string[]; | ||
privateSubnetNames?: string[]; | ||
isolatedSubnetIds?: string[]; | ||
isolatedSubnetNames?: string[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './cxapi'; | ||
export * from './environment'; | ||
export * from './context/vpc'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/aws-cdk/lib/context-providers/availability-zones.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Mode, SDK } from '../api'; | ||
import { debug } from '../logging'; | ||
import { ContextProviderPlugin } from './provider'; | ||
|
||
/** | ||
* Plugin to retrieve the Availability Zones for the current account | ||
*/ | ||
export class AZContextProviderPlugin implements ContextProviderPlugin { | ||
constructor(private readonly aws: SDK) { | ||
} | ||
|
||
public async getValue(args: {[key: string]: any}) { | ||
const region = args.region; | ||
const account = args.account; | ||
debug(`Reading AZs for ${account}:${region}`); | ||
const ec2 = await this.aws.ec2(account, region, Mode.ForReading); | ||
const response = await ec2.describeAvailabilityZones().promise(); | ||
if (!response.AvailabilityZones) { return []; } | ||
const azs = response.AvailabilityZones.filter(zone => zone.State === 'available').map(zone => zone.ZoneName); | ||
return azs; | ||
} | ||
} |
Oops, something went wrong.