AWS Cloudformation - can't add multiple subnet associations to a public routing table
Solution 1:
As @Marcus explained in his response to his own question; it is the lack of the DependsOn attribute when you create an AWS::EC2::Route entry where you specify a Gateway.
For route entries that specify a gateway, you must specify a dependency on the gateway attachment resource.
Having received the same error and scratching my head as to how this failed when the IGW is attached to the VPC it was a simple change in the AWS::EC2::Route
declaration.
Failing CFN:
"VPC" : {
"Type" : "AWS::EC2::VPC",
"Properties" : {"CidrBlock" : "10.1.0.0/16"}
},
"InternetGateway" : {
"Type" : "AWS::EC2::InternetGateway"
},
"InternetGatewayAttachment" : {
"Type" : "AWS::EC2::VPCGatewayAttachment",
"Properties" : {
"VpcId" : {"Ref" : "VPC"},
"InternetGatewayId" : {"Ref" : "InternetGateway"}
}
},
"ManagementRouteTable" : {
"Type" : "AWS::EC2::RouteTable",
"Properties" : {
"VpcId" : {"Ref" : "VPC"}
}
},
"NATDefaultRoute" : {
"Type" : "AWS::EC2::Route",
"Properties" : {
"RouteTableId" : {"Ref" : "ManagementRouteTable"},
"DestinationCidrBlock" : "0.0.0.0/0",
"GatewayId" : {"Ref" : "InternetGateway"}
}
}
Working CFN:
"VPC" : {
"Type" : "AWS::EC2::VPC",
"Properties" : {"CidrBlock" : "10.1.0.0/16"}
},
"InternetGateway" : {
"Type" : "AWS::EC2::InternetGateway"
},
"InternetGatewayAttachment" : {
"Type" : "AWS::EC2::VPCGatewayAttachment",
"Properties" : {
"VpcId" : {"Ref" : "VPC"},
"InternetGatewayId" : {"Ref" : "InternetGateway"}
}
},
"ManagementRouteTable" : {
"Type" : "AWS::EC2::RouteTable",
"Properties" : {
"VpcId" : {"Ref" : "VPC"}
}
},
"NATDefaultRoute" : {
"DependsOn" : "InternetGatewayAttachment",
"Type" : "AWS::EC2::Route",
"Properties" : {
"RouteTableId" : {"Ref" : "ManagementRouteTable"},
"DestinationCidrBlock" : "0.0.0.0/0",
"GatewayId" : {"Ref" : "InternetGateway"}
}
}
Solution 2:
are you sure you've attached the InternetGatway to a VPC (or the same VPC as the route table). In cloud formation this looks something like...
"AttachInternetGateway" : {
"Type" : "AWS::EC2::VPCGatewayAttachment",
"Properties" : {
"VpcId" : { "Ref" : "YourVpc" },
"InternetGatewayId" : { "Ref" : "InternetGateway" }
}
},
Solution 3:
I found a fix. I was on the right track with the Wait Conditions however it turns out I needed to add a DependsOn attribute to the rule so that it depended on the igw being created first.