@@ -149,7 +149,7 @@ def build(self):
149149
150150 def __build_template (self ):
151151 internet_gateway = self .__build_internet_gateway (self .__vpc )
152- nat_gateway = None
152+ nat_gateway_per_az_map = {}
153153 subnets = []
154154 subnet_refs = []
155155 bastion_subnet_ref = None
@@ -158,8 +158,10 @@ def __build_template(self):
158158 subnet = self .__build_subnet (subnet_config , self .__vpc , self .__additional_vpc_cidr_blocks )
159159 subnets .append (subnet )
160160 subnet_refs .append (Ref (subnet ))
161- if subnet_config .has_nat_gateway and nat_gateway is None :
162- nat_gateway = self .__build_nat_gateway (subnet_config , subnet )
161+ if subnet_config .has_nat_gateway and nat_gateway_per_az_map .get (subnet_config .availability_zone ) is None :
162+ nat_gateway_per_az_map [subnet_config .availability_zone ] = self .__build_nat_gateway (
163+ subnet_config , subnet
164+ )
163165 if subnet_config .default_gateway == Gateways .INTERNET_GATEWAY :
164166 bastion_subnet_ref = Ref (subnet )
165167 if subnet_config .default_gateway == Gateways .NONE :
@@ -168,7 +170,11 @@ def __build_template(self):
168170 route_tables_refs = []
169171 for subnet_config , subnet in zip (self .__vpc_subnets , subnets ):
170172 route_tables_refs .append (
171- Ref (self .__build_route_table (subnet_config , subnet , self .__vpc , internet_gateway , nat_gateway ))
173+ Ref (
174+ self .__build_route_table (
175+ subnet_config , subnet , self .__vpc , internet_gateway , nat_gateway_per_az_map
176+ )
177+ )
172178 )
173179
174180 if self .__create_vpc_endpoints :
@@ -389,7 +395,7 @@ def __build_nat_gateway(self, subnet_config: SubnetConfig, subnet_ref: Subnet):
389395 )
390396
391397 def __build_route_table (
392- self , subnet_config : SubnetConfig , subnet_ref : Subnet , vpc : VPC , internet_gateway , nat_gateway : NatGateway
398+ self , subnet_config : SubnetConfig , subnet_ref : Subnet , vpc : VPC , internet_gateway , nat_gateway_per_az_map : dict
393399 ):
394400 internet_gateway = If (self .__create_ig , internet_gateway , self .__gateway_id )
395401 route_table = self .__template .add_resource (
@@ -430,7 +436,7 @@ def __build_route_table(
430436 "NatRoute" + subnet_config .name ,
431437 RouteTableId = Ref (route_table ),
432438 DestinationCidrBlock = "0.0.0.0/0" ,
433- NatGatewayId = Ref (nat_gateway ),
439+ NatGatewayId = Ref (nat_gateway_per_az_map . get ( subnet_config . availability_zone ) ),
434440 )
435441 )
436442
0 commit comments