Skip to content

Commit

Permalink
fix(aws-ec2): add dependency on gateway attachment for public routes (#…
Browse files Browse the repository at this point in the history
…1142)

Currently there is a race condition when creating the
public subnet routes for the ec2.VpcNetwork construct. CloudFormation
can attempt to create the public subnet routes to the IGW before the IGW
is attached to the VPC. This change adds a dependency to the public
routes on the IGW attachment.

Fixes #1140. 

BREAKING CHANGE: Method signature of
VpcPublicSubnet.addDefaultIGWRouteEntry changed in order to add a
dependency on gateway attachment completing before creating the public
route to the gateway. Instead of passing a gateway ID string, pass in a
cloudformation.InternetGatewayResource object and a
cloudformation.VPCGatewayAttachmentResource object.
  • Loading branch information
clareliguori authored and rix0rrr committed Nov 13, 2018
1 parent bb952e7 commit 15b255c
Show file tree
Hide file tree
Showing 17 changed files with 129 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
},
"VPCPublicSubnet1DefaultRoute91CEF279": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VPCVPCGW99B986DC"
],
"Properties": {
"RouteTableId": {
"Ref": "VPCPublicSubnet1RouteTableFEE4B781"
Expand Down Expand Up @@ -139,6 +142,9 @@
},
"VPCPublicSubnet2DefaultRouteB7481BBA": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VPCVPCGW99B986DC"
],
"Properties": {
"RouteTableId": {
"Ref": "VPCPublicSubnet2RouteTable6F1A15F1"
Expand Down Expand Up @@ -411,4 +417,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@
},
"VPCPublicSubnet1DefaultRoute91CEF279": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VPCVPCGW99B986DC"
],
"Properties": {
"RouteTableId": {
"Ref": "VPCPublicSubnet1RouteTableFEE4B781"
Expand Down Expand Up @@ -165,6 +168,9 @@
},
"VPCPublicSubnet2DefaultRouteB7481BBA": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VPCVPCGW99B986DC"
],
"Properties": {
"RouteTableId": {
"Ref": "VPCPublicSubnet2RouteTable6F1A15F1"
Expand Down Expand Up @@ -245,6 +251,9 @@
},
"VPCPublicSubnet3DefaultRouteA0D29D46": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VPCVPCGW99B986DC"
],
"Properties": {
"RouteTableId": {
"Ref": "VPCPublicSubnet3RouteTable98AE0E14"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@
},
"VPCPublicSubnet1DefaultRoute91CEF279": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VPCVPCGW99B986DC"
],
"Properties": {
"RouteTableId": {
"Ref": "VPCPublicSubnet1RouteTableFEE4B781"
Expand Down Expand Up @@ -165,6 +168,9 @@
},
"VPCPublicSubnet2DefaultRouteB7481BBA": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VPCVPCGW99B986DC"
],
"Properties": {
"RouteTableId": {
"Ref": "VPCPublicSubnet2RouteTable6F1A15F1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
},
"VPCPublicSubnet1DefaultRoute91CEF279": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VPCVPCGW99B986DC"
],
"Properties": {
"RouteTableId": {
"Ref": "VPCPublicSubnet1RouteTableFEE4B781"
Expand Down Expand Up @@ -139,6 +142,9 @@
},
"VPCPublicSubnet2DefaultRouteB7481BBA": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VPCVPCGW99B986DC"
],
"Properties": {
"RouteTableId": {
"Ref": "VPCPublicSubnet2RouteTable6F1A15F1"
Expand Down Expand Up @@ -219,6 +225,9 @@
},
"VPCPublicSubnet3DefaultRouteA0D29D46": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VPCVPCGW99B986DC"
],
"Properties": {
"RouteTableId": {
"Ref": "VPCPublicSubnet3RouteTable98AE0E14"
Expand Down
24 changes: 17 additions & 7 deletions packages/@aws-cdk/aws-ec2/lib/vpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ export class VpcNetwork extends VpcNetworkRef implements cdk.ITaggable {
this.dependencyElements.push(igw, att);

(this.publicSubnets as VpcPublicSubnet[]).forEach(publicSubnet => {
publicSubnet.addDefaultIGWRouteEntry(igw.ref);
publicSubnet.addDefaultIGWRouteEntry(igw, att);
});

// if gateways are needed create them
Expand Down Expand Up @@ -520,12 +520,19 @@ export class VpcSubnet extends VpcSubnetRef implements cdk.ITaggable {
});
}

protected addDefaultRouteToIGW(gatewayId: string) {
new cloudformation.RouteResource(this, `DefaultRoute`, {
/**
* Create a default route that points to a passed IGW, with a dependency
* on the IGW's attachment to the VPC.
*/
protected addDefaultRouteToIGW(
gateway: cloudformation.InternetGatewayResource,
gatewayAttachment: cloudformation.VPCGatewayAttachmentResource) {
const route = new cloudformation.RouteResource(this, `DefaultRoute`, {
routeTableId: this.routeTableId,
destinationCidrBlock: '0.0.0.0/0',
gatewayId
gatewayId: gateway.ref
});
route.addDependency(gatewayAttachment);
}
}

Expand All @@ -538,10 +545,13 @@ export class VpcPublicSubnet extends VpcSubnet {
}

/**
* Create a default route that points to a passed IGW
* Create a default route that points to a passed IGW, with a dependency
* on the IGW's attachment to the VPC.
*/
public addDefaultIGWRouteEntry(gatewayId: string) {
this.addDefaultRouteToIGW(gatewayId);
public addDefaultIGWRouteEntry(
gateway: cloudformation.InternetGatewayResource,
gatewayAttachment: cloudformation.VPCGatewayAttachmentResource) {
this.addDefaultRouteToIGW(gateway, gatewayAttachment);
}

/**
Expand Down
9 changes: 9 additions & 0 deletions packages/@aws-cdk/aws-ec2/test/integ.vpc.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@
},
"MyVpcPublicSubnet1DefaultRoute95FDF9EB": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"MyVpcVPCGW488ACE0D"
],
"Properties": {
"RouteTableId": {
"Ref": "MyVpcPublicSubnet1RouteTableC46AB2F4"
Expand Down Expand Up @@ -165,6 +168,9 @@
},
"MyVpcPublicSubnet2DefaultRoute052936F6": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"MyVpcVPCGW488ACE0D"
],
"Properties": {
"RouteTableId": {
"Ref": "MyVpcPublicSubnet2RouteTable1DF17386"
Expand Down Expand Up @@ -245,6 +251,9 @@
},
"MyVpcPublicSubnet3DefaultRoute3A83AB36": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"MyVpcVPCGW488ACE0D"
],
"Properties": {
"RouteTableId": {
"Ref": "MyVpcPublicSubnet3RouteTable15028F08"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
},
"VpcPublicSubnet1DefaultRoute3DA9E72A": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VpcVPCGWBF912B6E"
],
"Properties": {
"RouteTableId": {
"Ref": "VpcPublicSubnet1RouteTable6C95E38E"
Expand Down Expand Up @@ -139,6 +142,9 @@
},
"VpcPublicSubnet2DefaultRoute97F91067": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VpcVPCGWBF912B6E"
],
"Properties": {
"RouteTableId": {
"Ref": "VpcPublicSubnet2RouteTable94F7E489"
Expand Down Expand Up @@ -722,4 +728,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
},
"VpcPublicSubnet1DefaultRoute3DA9E72A": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VpcVPCGWBF912B6E"
],
"Properties": {
"RouteTableId": {
"Ref": "VpcPublicSubnet1RouteTable6C95E38E"
Expand Down Expand Up @@ -139,6 +142,9 @@
},
"VpcPublicSubnet2DefaultRoute97F91067": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VpcVPCGWBF912B6E"
],
"Properties": {
"RouteTableId": {
"Ref": "VpcPublicSubnet2RouteTable94F7E489"
Expand Down Expand Up @@ -685,4 +691,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
},
"VpcPublicSubnet1DefaultRoute3DA9E72A": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VpcVPCGWBF912B6E"
],
"Properties": {
"RouteTableId": {
"Ref": "VpcPublicSubnet1RouteTable6C95E38E"
Expand Down Expand Up @@ -139,6 +142,9 @@
},
"VpcPublicSubnet2DefaultRoute97F91067": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VpcVPCGWBF912B6E"
],
"Properties": {
"RouteTableId": {
"Ref": "VpcPublicSubnet2RouteTable94F7E489"
Expand Down Expand Up @@ -801,4 +807,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
},
"VpcPublicSubnet1DefaultRoute3DA9E72A": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VpcVPCGWBF912B6E"
],
"Properties": {
"RouteTableId": {
"Ref": "VpcPublicSubnet1RouteTable6C95E38E"
Expand Down Expand Up @@ -139,6 +142,9 @@
},
"VpcPublicSubnet2DefaultRoute97F91067": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VpcVPCGWBF912B6E"
],
"Properties": {
"RouteTableId": {
"Ref": "VpcPublicSubnet2RouteTable94F7E489"
Expand Down Expand Up @@ -631,4 +637,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@
},
"VPCPublicSubnet1DefaultRoute91CEF279": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VPCVPCGW99B986DC"
],
"Properties": {
"RouteTableId": {
"Ref": "VPCPublicSubnet1RouteTableFEE4B781"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
},
"VPCPublicSubnet1DefaultRoute91CEF279": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VPCVPCGW99B986DC"
],
"Properties": {
"RouteTableId": {
"Ref": "VPCPublicSubnet1RouteTableFEE4B781"
Expand Down Expand Up @@ -139,6 +142,9 @@
},
"VPCPublicSubnet2DefaultRouteB7481BBA": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VPCVPCGW99B986DC"
],
"Properties": {
"RouteTableId": {
"Ref": "VPCPublicSubnet2RouteTable6F1A15F1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@
},
"VPCPublicSubnet1DefaultRoute91CEF279": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VPCVPCGW99B986DC"
],
"Properties": {
"RouteTableId": {
"Ref": "VPCPublicSubnet1RouteTableFEE4B781"
Expand Down Expand Up @@ -165,6 +168,9 @@
},
"VPCPublicSubnet2DefaultRouteB7481BBA": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VPCVPCGW99B986DC"
],
"Properties": {
"RouteTableId": {
"Ref": "VPCPublicSubnet2RouteTable6F1A15F1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@
},
"VPCPublicSubnet1DefaultRoute91CEF279": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VPCVPCGW99B986DC"
],
"Properties": {
"RouteTableId": {
"Ref": "VPCPublicSubnet1RouteTableFEE4B781"
Expand Down Expand Up @@ -165,6 +168,9 @@
},
"VPCPublicSubnet2DefaultRouteB7481BBA": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VPCVPCGW99B986DC"
],
"Properties": {
"RouteTableId": {
"Ref": "VPCPublicSubnet2RouteTable6F1A15F1"
Expand Down Expand Up @@ -362,4 +368,4 @@
]
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
},
"VPCPublicSubnet1DefaultRoute91CEF279": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VPCVPCGW99B986DC"
],
"Properties": {
"RouteTableId": {
"Ref": "VPCPublicSubnet1RouteTableFEE4B781"
Expand Down Expand Up @@ -139,6 +142,9 @@
},
"VPCPublicSubnet2DefaultRouteB7481BBA": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VPCVPCGW99B986DC"
],
"Properties": {
"RouteTableId": {
"Ref": "VPCPublicSubnet2RouteTable6F1A15F1"
Expand Down
Loading

0 comments on commit 15b255c

Please sign in to comment.