Skip to content

Commit 15b255c

Browse files
clareliguoririx0rrr
authored andcommitted
fix(aws-ec2): add dependency on gateway attachment for public routes (#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.
1 parent bb952e7 commit 15b255c

17 files changed

+129
-14
lines changed

packages/@aws-cdk/aws-autoscaling/test/integ.amazonlinux2.expected.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
},
6060
"VPCPublicSubnet1DefaultRoute91CEF279": {
6161
"Type": "AWS::EC2::Route",
62+
"DependsOn": [
63+
"VPCVPCGW99B986DC"
64+
],
6265
"Properties": {
6366
"RouteTableId": {
6467
"Ref": "VPCPublicSubnet1RouteTableFEE4B781"
@@ -139,6 +142,9 @@
139142
},
140143
"VPCPublicSubnet2DefaultRouteB7481BBA": {
141144
"Type": "AWS::EC2::Route",
145+
"DependsOn": [
146+
"VPCVPCGW99B986DC"
147+
],
142148
"Properties": {
143149
"RouteTableId": {
144150
"Ref": "VPCPublicSubnet2RouteTable6F1A15F1"
@@ -411,4 +417,4 @@
411417
}
412418
}
413419
}
414-
}
420+
}

packages/@aws-cdk/aws-autoscaling/test/integ.asg-w-classic-loadbalancer.expected.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@
8585
},
8686
"VPCPublicSubnet1DefaultRoute91CEF279": {
8787
"Type": "AWS::EC2::Route",
88+
"DependsOn": [
89+
"VPCVPCGW99B986DC"
90+
],
8891
"Properties": {
8992
"RouteTableId": {
9093
"Ref": "VPCPublicSubnet1RouteTableFEE4B781"
@@ -165,6 +168,9 @@
165168
},
166169
"VPCPublicSubnet2DefaultRouteB7481BBA": {
167170
"Type": "AWS::EC2::Route",
171+
"DependsOn": [
172+
"VPCVPCGW99B986DC"
173+
],
168174
"Properties": {
169175
"RouteTableId": {
170176
"Ref": "VPCPublicSubnet2RouteTable6F1A15F1"
@@ -245,6 +251,9 @@
245251
},
246252
"VPCPublicSubnet3DefaultRouteA0D29D46": {
247253
"Type": "AWS::EC2::Route",
254+
"DependsOn": [
255+
"VPCVPCGW99B986DC"
256+
],
248257
"Properties": {
249258
"RouteTableId": {
250259
"Ref": "VPCPublicSubnet3RouteTable98AE0E14"

packages/@aws-cdk/aws-autoscaling/test/integ.asg-w-elbv2.expected.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@
8585
},
8686
"VPCPublicSubnet1DefaultRoute91CEF279": {
8787
"Type": "AWS::EC2::Route",
88+
"DependsOn": [
89+
"VPCVPCGW99B986DC"
90+
],
8891
"Properties": {
8992
"RouteTableId": {
9093
"Ref": "VPCPublicSubnet1RouteTableFEE4B781"
@@ -165,6 +168,9 @@
165168
},
166169
"VPCPublicSubnet2DefaultRouteB7481BBA": {
167170
"Type": "AWS::EC2::Route",
171+
"DependsOn": [
172+
"VPCVPCGW99B986DC"
173+
],
168174
"Properties": {
169175
"RouteTableId": {
170176
"Ref": "VPCPublicSubnet2RouteTable6F1A15F1"

packages/@aws-cdk/aws-codedeploy/test/integ.deployment-group.expected.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
},
6060
"VPCPublicSubnet1DefaultRoute91CEF279": {
6161
"Type": "AWS::EC2::Route",
62+
"DependsOn": [
63+
"VPCVPCGW99B986DC"
64+
],
6265
"Properties": {
6366
"RouteTableId": {
6467
"Ref": "VPCPublicSubnet1RouteTableFEE4B781"
@@ -139,6 +142,9 @@
139142
},
140143
"VPCPublicSubnet2DefaultRouteB7481BBA": {
141144
"Type": "AWS::EC2::Route",
145+
"DependsOn": [
146+
"VPCVPCGW99B986DC"
147+
],
142148
"Properties": {
143149
"RouteTableId": {
144150
"Ref": "VPCPublicSubnet2RouteTable6F1A15F1"
@@ -219,6 +225,9 @@
219225
},
220226
"VPCPublicSubnet3DefaultRouteA0D29D46": {
221227
"Type": "AWS::EC2::Route",
228+
"DependsOn": [
229+
"VPCVPCGW99B986DC"
230+
],
222231
"Properties": {
223232
"RouteTableId": {
224233
"Ref": "VPCPublicSubnet3RouteTable98AE0E14"

packages/@aws-cdk/aws-ec2/lib/vpc.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ export class VpcNetwork extends VpcNetworkRef implements cdk.ITaggable {
319319
this.dependencyElements.push(igw, att);
320320

321321
(this.publicSubnets as VpcPublicSubnet[]).forEach(publicSubnet => {
322-
publicSubnet.addDefaultIGWRouteEntry(igw.ref);
322+
publicSubnet.addDefaultIGWRouteEntry(igw, att);
323323
});
324324

325325
// if gateways are needed create them
@@ -520,12 +520,19 @@ export class VpcSubnet extends VpcSubnetRef implements cdk.ITaggable {
520520
});
521521
}
522522

523-
protected addDefaultRouteToIGW(gatewayId: string) {
524-
new cloudformation.RouteResource(this, `DefaultRoute`, {
523+
/**
524+
* Create a default route that points to a passed IGW, with a dependency
525+
* on the IGW's attachment to the VPC.
526+
*/
527+
protected addDefaultRouteToIGW(
528+
gateway: cloudformation.InternetGatewayResource,
529+
gatewayAttachment: cloudformation.VPCGatewayAttachmentResource) {
530+
const route = new cloudformation.RouteResource(this, `DefaultRoute`, {
525531
routeTableId: this.routeTableId,
526532
destinationCidrBlock: '0.0.0.0/0',
527-
gatewayId
533+
gatewayId: gateway.ref
528534
});
535+
route.addDependency(gatewayAttachment);
529536
}
530537
}
531538

@@ -538,10 +545,13 @@ export class VpcPublicSubnet extends VpcSubnet {
538545
}
539546

540547
/**
541-
* Create a default route that points to a passed IGW
548+
* Create a default route that points to a passed IGW, with a dependency
549+
* on the IGW's attachment to the VPC.
542550
*/
543-
public addDefaultIGWRouteEntry(gatewayId: string) {
544-
this.addDefaultRouteToIGW(gatewayId);
551+
public addDefaultIGWRouteEntry(
552+
gateway: cloudformation.InternetGatewayResource,
553+
gatewayAttachment: cloudformation.VPCGatewayAttachmentResource) {
554+
this.addDefaultRouteToIGW(gateway, gatewayAttachment);
545555
}
546556

547557
/**

packages/@aws-cdk/aws-ec2/test/integ.vpc.expected.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@
8585
},
8686
"MyVpcPublicSubnet1DefaultRoute95FDF9EB": {
8787
"Type": "AWS::EC2::Route",
88+
"DependsOn": [
89+
"MyVpcVPCGW488ACE0D"
90+
],
8891
"Properties": {
8992
"RouteTableId": {
9093
"Ref": "MyVpcPublicSubnet1RouteTableC46AB2F4"
@@ -165,6 +168,9 @@
165168
},
166169
"MyVpcPublicSubnet2DefaultRoute052936F6": {
167170
"Type": "AWS::EC2::Route",
171+
"DependsOn": [
172+
"MyVpcVPCGW488ACE0D"
173+
],
168174
"Properties": {
169175
"RouteTableId": {
170176
"Ref": "MyVpcPublicSubnet2RouteTable1DF17386"
@@ -245,6 +251,9 @@
245251
},
246252
"MyVpcPublicSubnet3DefaultRoute3A83AB36": {
247253
"Type": "AWS::EC2::Route",
254+
"DependsOn": [
255+
"MyVpcVPCGW488ACE0D"
256+
],
248257
"Properties": {
249258
"RouteTableId": {
250259
"Ref": "MyVpcPublicSubnet3RouteTable15028F08"

packages/@aws-cdk/aws-ecs/test/ec2/integ.lb-awsvpc-nw.expected.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
},
6060
"VpcPublicSubnet1DefaultRoute3DA9E72A": {
6161
"Type": "AWS::EC2::Route",
62+
"DependsOn": [
63+
"VpcVPCGWBF912B6E"
64+
],
6265
"Properties": {
6366
"RouteTableId": {
6467
"Ref": "VpcPublicSubnet1RouteTable6C95E38E"
@@ -139,6 +142,9 @@
139142
},
140143
"VpcPublicSubnet2DefaultRoute97F91067": {
141144
"Type": "AWS::EC2::Route",
145+
"DependsOn": [
146+
"VpcVPCGWBF912B6E"
147+
],
142148
"Properties": {
143149
"RouteTableId": {
144150
"Ref": "VpcPublicSubnet2RouteTable94F7E489"
@@ -722,4 +728,4 @@
722728
}
723729
}
724730
}
725-
}
731+
}

packages/@aws-cdk/aws-ecs/test/ec2/integ.lb-bridge-nw.expected.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
},
6060
"VpcPublicSubnet1DefaultRoute3DA9E72A": {
6161
"Type": "AWS::EC2::Route",
62+
"DependsOn": [
63+
"VpcVPCGWBF912B6E"
64+
],
6265
"Properties": {
6366
"RouteTableId": {
6467
"Ref": "VpcPublicSubnet1RouteTable6C95E38E"
@@ -139,6 +142,9 @@
139142
},
140143
"VpcPublicSubnet2DefaultRoute97F91067": {
141144
"Type": "AWS::EC2::Route",
145+
"DependsOn": [
146+
"VpcVPCGWBF912B6E"
147+
],
142148
"Properties": {
143149
"RouteTableId": {
144150
"Ref": "VpcPublicSubnet2RouteTable94F7E489"
@@ -685,4 +691,4 @@
685691
}
686692
}
687693
}
688-
}
694+
}

packages/@aws-cdk/aws-ecs/test/fargate/integ.asset-image.expected.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
},
6060
"VpcPublicSubnet1DefaultRoute3DA9E72A": {
6161
"Type": "AWS::EC2::Route",
62+
"DependsOn": [
63+
"VpcVPCGWBF912B6E"
64+
],
6265
"Properties": {
6366
"RouteTableId": {
6467
"Ref": "VpcPublicSubnet1RouteTable6C95E38E"
@@ -139,6 +142,9 @@
139142
},
140143
"VpcPublicSubnet2DefaultRoute97F91067": {
141144
"Type": "AWS::EC2::Route",
145+
"DependsOn": [
146+
"VpcVPCGWBF912B6E"
147+
],
142148
"Properties": {
143149
"RouteTableId": {
144150
"Ref": "VpcPublicSubnet2RouteTable94F7E489"
@@ -801,4 +807,4 @@
801807
}
802808
}
803809
}
804-
}
810+
}

packages/@aws-cdk/aws-ecs/test/fargate/integ.lb-awsvpc-nw.expected.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
},
6060
"VpcPublicSubnet1DefaultRoute3DA9E72A": {
6161
"Type": "AWS::EC2::Route",
62+
"DependsOn": [
63+
"VpcVPCGWBF912B6E"
64+
],
6265
"Properties": {
6366
"RouteTableId": {
6467
"Ref": "VpcPublicSubnet1RouteTable6C95E38E"
@@ -139,6 +142,9 @@
139142
},
140143
"VpcPublicSubnet2DefaultRoute97F91067": {
141144
"Type": "AWS::EC2::Route",
145+
"DependsOn": [
146+
"VpcVPCGWBF912B6E"
147+
],
142148
"Properties": {
143149
"RouteTableId": {
144150
"Ref": "VpcPublicSubnet2RouteTable94F7E489"
@@ -631,4 +637,4 @@
631637
}
632638
}
633639
}
634-
}
640+
}

0 commit comments

Comments
 (0)