22# AWS Virtual Private Network
33# ------------------------------------------------------------------------------
44resource "aws_vpc" "vpc" {
5- cidr_block = var. vpc_cidr_block # The CIDR block for the VPC.
6- enable_dns_support = true # A boolean flag to enable/disable DNS support in the VPC.
7- enable_dns_hostnames = true # A boolean flag to enable/disable DNS hostnames in the VPC.
5+ cidr_block = var. vpc_cidr_block
6+ # The CIDR block for the VPC.
7+ enable_dns_support = true
8+ # A boolean flag to enable/disable DNS support in the VPC.
9+ enable_dns_hostnames = true
10+ # A boolean flag to enable/disable DNS hostnames in the VPC.
811 tags = {
912 Name = " ${ var . name_prefix } -vpc"
1013 }
@@ -37,7 +40,7 @@ resource "aws_subnet" "public_subnets" {
3740
3841# Elastic IPs for NAT
3942resource "aws_eip" "nat_eip" {
40- count = length (var. availability_zones )
43+ count = var . single_nat ? 1 : length (var. availability_zones )
4144 vpc = true
4245 tags = {
4346 Name = " ${ var . name_prefix } -nat-eip-${ element (var. availability_zones , count. index )} "
@@ -46,13 +49,17 @@ resource "aws_eip" "nat_eip" {
4649
4750# NAT Gateways
4851resource "aws_nat_gateway" "nat_gw" {
49- count = length (var. availability_zones )
50- depends_on = [ aws_internet_gateway . internet_gw ]
51- allocation_id = element (aws_eip . nat_eip . * . id , count. index )
52- subnet_id = element (aws_subnet . public_subnets . * . id , count . index )
52+ count = var . single_nat ? 1 : length (var. availability_zones )
53+ allocation_id = var . single_nat ? aws_eip . nat_eip . 0 . id : element (aws_eip . nat_eip . * . id , count . index )
54+ subnet_id = var . single_nat ? aws_subnet . public_subnets . 0 . id : element (aws_subnet . public_subnets . * . id , count. index )
55+
5356 tags = {
5457 Name = " ${ var . name_prefix } -nat-gw-${ element (var. availability_zones , count. index )} "
5558 }
59+
60+ depends_on = [
61+ aws_internet_gateway . internet_gw
62+ ]
5663}
5764
5865# Public route table
@@ -116,7 +123,7 @@ resource "aws_route" "private_internet_route" {
116123 ]
117124 route_table_id = element (aws_route_table. private_subnets_route_table . * . id , count. index )
118125 destination_cidr_block = " 0.0.0.0/0"
119- nat_gateway_id = element (aws_nat_gateway. nat_gw . * . id , count. index )
126+ nat_gateway_id = var . single_nat ? aws_nat_gateway . nat_gw . 0 . id : element (aws_nat_gateway. nat_gw . * . id , count. index )
120127}
121128
122129# Association of Route Table to Subnets
0 commit comments