お疲れ様です。福島です。
今回はネットワーク周りの下記の構築を行っていきます。
・VPC
・サブネット
・ルートテーブル
・IGW
■構成図
■VPC
resource "aws_vpc" "vpc" {
cidr_block = "192.168.0.0/20"
instance_tenancy = "default"
enable_dns_support = true
enable_dns_hostnames = true
assign_generated_ipv6_cidr_block = false
tags = {
Name = "kai.fukushima-${var.project}-${var.enviroment}-vpc"
project = var.project
Env = var.enviroment
}
}
■Subnet
resource "aws_subnet" "kaifukushima-public-subnet-1a" {
vpc_id = aws_vpc.vpc.id
availability_zone = "ap-northeast-1a"
cidr_block = "192.168.1.0/24"
map_public_ip_on_launch = true
tags = {
Name = "kaifukushima${var.project}-${var.enviroment}-public-subnet-1a"
Project = var.project
Env = var.enviroment
Type = "public"
}
}
resource "aws_subnet" "kaifukushima-public-subnet-1c" {
vpc_id = aws_vpc.vpc.id
availability_zone = "ap-northeast-1c"
cidr_block = "192.168.2.0/24"
map_public_ip_on_launch = true
tags = {
Name = "kaifukushima${var.project}-${var.enviroment}-public-subnet-1c"
Project = var.project
Env = var.enviroment
Type = "public"
}
}
resource "aws_subnet" "kaifukushima-private-subnet-1a" {
vpc_id = aws_vpc.vpc.id
availability_zone = "ap-northeast-1a"
cidr_block = "192.168.3.0/24"
map_public_ip_on_launch = false
tags = {
Name = "kaifukushima${var.project}-${var.enviroment}-private-subnet-1a"
Project = var.project
Env = var.enviroment
Type = "private"
}
}
resource "aws_subnet" "kaifukushima-private-subnet-1c" {
vpc_id = aws_vpc.vpc.id
availability_zone = "ap-northeast-1c"
cidr_block = "192.168.4.0/24"
map_public_ip_on_launch = false
tags = {
Name = "kaifukushima${var.project}-${var.enviroment}-private-subnet-1c"
Project = var.project
Env = var.enviroment
Type = "private"
}
}
■Route table
resource "aws_route_table" "kaifukushima-public-rt" {
vpc_id = aws_vpc.vpc.id
tags = {
Name = "kaifukushima${var.project}-${var.enviroment}-public-rt"
Project = var.project
Env = var.enviroment
Type = "public"
}
}
resource "aws_route_table_association" "public_rt_1a" {
route_table_id = aws_route_table.kaifukushima-public-rt.id
subnet_id = aws_subnet.kaifukushima-public-subnet-1a.id
}
resource "aws_route_table_association" "public_rt_1c" {
route_table_id = aws_route_table.kaifukushima-public-rt.id
subnet_id = aws_subnet.kaifukushima-public-subnet-1c.id
}
resource "aws_route_table" "kaifukushima-private-rt" {
vpc_id = aws_vpc.vpc.id
tags = {
Name = "kaifukushima${var.project}-${var.enviroment}-private-rt"
Project = var.project
Env = var.enviroment
Type = "private"
}
}
resource "aws_route_table_association" "private_rt_1a" {
route_table_id = aws_route_table.kaifukushima-private-rt.id
subnet_id = aws_subnet.kaifukushima-private-subnet-1a.id
}
resource "aws_route_table_association" "private_rt_1c" {
route_table_id = aws_route_table.kaifukushima-private-rt.id
subnet_id = aws_subnet.kaifukushima-private-subnet-1c.id
}
■Internet Gateway
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.vpc.id
tags = {
Name = "kaifukushima-${var.project}-${var.enviroment}-igw"
Project = var.project
Env = var.enviroment
}
}
resource "aws_route" "public_rt_igw_r" {
route_table_id = aws_route_table.kaifukushima-public-rt.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
■まとめ
今回作成したネットワーク周りはコンソール上でも
何度も作成しているので、コードを書きながらどこの部分の事か
理解しながら書けました!
構築量が増えた来た際には
Terraformは非常に便利だと思いますが
何を書いているか分からなくなった際は
コンソールで構築を行い構造理解をする事が大事ですね。