Complete the code to create a public subnet with automatic public IP assignment.
resource "aws_subnet" "public" { vpc_id = aws_vpc.main.id cidr_block = "10.0.1.0/24" map_public_ip_on_launch = [1] availability_zone = "us-east-1a" }
Setting map_public_ip_on_launch to true enables automatic public IP assignment for instances in the subnet, making it public.
Complete the code to create a route in the public subnet's route table that directs internet traffic to the internet gateway.
resource "aws_route" "public_internet_access" { route_table_id = aws_route_table.public.id destination_cidr_block = "0.0.0.0/0" [1] = aws_internet_gateway.main.id }
The correct attribute to specify the internet gateway in a route is gateway_id.
Fix the error in the private subnet route table to route internet traffic through the NAT gateway.
resource "aws_route" "private_nat_gateway" { route_table_id = aws_route_table.private.id destination_cidr_block = "0.0.0.0/0" [1] = aws_nat_gateway.main.id }
For private subnets, internet traffic is routed through the NAT gateway using the nat_gateway_id attribute.
Fill both blanks to create a private subnet without automatic public IP assignment and associate it with the private route table.
resource "aws_subnet" "private" { vpc_id = aws_vpc.main.id cidr_block = "10.0.2.0/24" map_public_ip_on_launch = [1] availability_zone = "us-east-1b" } resource "aws_route_table_association" "private_assoc" { subnet_id = aws_subnet.private.id route_table_id = [2] }
Private subnets do not assign public IPs automatically, so map_public_ip_on_launch is false. They are associated with the private route table.
Fill all three blanks to define a NAT gateway in a public subnet with an Elastic IP and associate it with the private route table.
resource "aws_eip" "nat_eip" { vpc = true } resource "aws_nat_gateway" "main" { allocation_id = aws_eip.nat_eip.id subnet_id = [1] } resource "aws_route" "private_nat_route" { route_table_id = [2] destination_cidr_block = "0.0.0.0/0" [3] = aws_nat_gateway.main.id }
The NAT gateway must be in the public subnet (aws_subnet.public.id), the private route table is used for the route, and the route uses nat_gateway_id to point to the NAT gateway.