VPCの作り方
必要な情報
code:igw.tf
resource "aws_internet_gateway" "example" {
vpc_id = aws_vpc.example.id
}
code:route-table.tf
resource "aws_route_table" "public" {
vpc_id = aws_vpc.example.id
}
code:route.tf
resource "aws_route" "public"{
route_table_id = aws_route_table.public.id
gateway_id = aws_internet_gateway.example.id
destination_cidr_block = "0.0.0.0/0"
}
code:table_association.tf
resource "aws_route_table_association" "public"{
subnet_id = aws_subnet.public.id
route_table_id = aws_route_table.public.id
}
code:private_subnet.tf
resource "aws_subnet" "private"{
vpc_id = aws_vpc.example.id
cidr_block = "10.0.64.0/24"
availability_zone = "ap-northeast-1a"
map_public_ip_on_launch = false
}
code:route_table.tf
resource "aws_route_table" "private"{
vpc_id = aws_vpc.example.id
}
resource "aws_route_table_association" "private" {
subnet_id = aws_subnet.private.id
route_table_id = aws_route_table.private.id
}
code:eip.tf
resource "aws_eip" "nat_gateway" {
vpc=true
}
code:nat.tf
resource "aws_nat_gateway" "example" {
allocation_id = aws_eip.nat_gateway.id
subnet_id = aws_subnet.public.id
}
NATゲートウェイを用いる場合はnat_gateway_idをgateway_idと間違えないように注意する code:route.tf
resource "aws_route" "private"{
route_table_id = aws_route_table.private.id
nat_gateway_id = aws_nat_gateway.example.id
destination_cidr_block = "0.0.0.0/0"
}