# vpc/main.tf # Create VPC resource "aws_vpc" "main" { cidr_block = var.cidr_block tags = { Name = var.vpc_name } } # Create internet gateway resource "aws_internet_gateway" "main" { vpc_id = aws_vpc.main.id tags = { Name = "${var.vpc_name}-igw" } } # Create two public subnets in two availability zones resource "aws_subnet" "public" { count = 2 vpc_id = aws_vpc.main.id cidr_block = var.public_subnet_cidr_blocks[count.index] availability_zone = var.availability_zones[count.index] tags = { Name = "${var.vpc_name}-public-${count.index+1}" } } # Create route table for public subnets resource "aws_route_table" "public" { vpc_id = aws_vpc.main.id tags = { Name = "${var.vpc_name}-public-rt" } } # Create route for public subnets to internet gateway resource "aws_route" "public_igw" { route_table_id = aws_route_table.public.id destination_cidr_block = "0.0.0.0/0" gateway_id = aws_internet_gateway.main.id } # Associate public subnets with public route table resource "aws_route_table_association" "public" { count = 2 subnet_id = aws_subnet.public[count.index].id route_table_id = aws_route_table.public.id } # Outputs output "vpc_id" { value = aws_vpc.main.id } output "public_subnet_ids" { value = aws_subnet.public.*.id } output "public_subnet_cidr_blocks" { value = aws_subnet.public.*.cidr_block } output "public_subnet_1_id" { value = element(aws_subnet.public.*.id, 0) } output "public_subnet_2_id" { value = element(aws_subnet.public.*.id, 1) }