####################
# VPC
####################
resource "aws_vpc" "main" {
cidr_block = "172.32.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
instance_tenancy = "default"
tags = {
Name = "foo"
}
}
#####################
# Internet Gateway
#####################
resource "aws_internet_gateway" "example"{
vpc_id = aws_vpc.main.id
}
####################
# subnet
####################
resource "aws_subnet" "public1" {
vpc_id = aws_vpc.main.id
cidr_block = "172.32.16.0/20"
availability_zone = "ap-northeast-1c"
map_public_ip_on_launch = true
}
resource "aws_subnet" "public2" {
vpc_id = aws_vpc.main.id
cidr_block = "172.32.32.0/20"
availability_zone = "ap-northeast-1d"
map_public_ip_on_launch = true
}
######################
# Route table
######################
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
tags = {
Name = "foo"
}
}
resource "aws_route" "foo" {
route_table_id = aws_route_table.public.id
gateway_id = aws_internet_gateway.example.id
destination_cidr_block = "0.0.0.0/0"
}
######################
# Association
######################
resource "aws_main_route_table_association" "foo" {
vpc_id = aws_vpc.main.id # 紐づけたいVPCのIDを指定
route_table_id = aws_route_table.public.id # 紐付けたいルートテーブルのIDを指定
}
resource "aws_route_table_association" "public1" {
subnet_id = aws_subnet.public1.id # 紐づけたいサブネットのIDを指定
route_table_id = aws_route_table.public.id # 紐付けたいルートテーブルのIDを指定
}
resource "aws_route_table_association" "public2" {
subnet_id = aws_subnet.public2.id # 紐づけたいサブネットのIDを指定
route_table_id = aws_route_table.public.id # 紐付けたいルートテーブルのIDを指定
}