provisioning-2/modules/vpc/main.tf

93 lines
2.6 KiB
HCL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# ---------------------------
# VPC
# ---------------------------
resource "aws_vpc" "vpc" {
cidr_block = var.vpc_cidr_block
enable_dns_hostnames = true # DNSホスト名を有効化
tags = {
Name = "${var.name_prefix}${var.env}-${var.vpc_name}"
}
}
# ---------------------------
# サブネット
# ---------------------------
# パブリックサブネット1
resource "aws_subnet" "public_1a_subnet" {
vpc_id = aws_vpc.vpc.id
cidr_block = var.public_1a_cidr
availability_zone = "ap-northeast-1a"
map_public_ip_on_launch = true
tags = {
Name = "${var.name_prefix}${var.env}-${var.public_1a_name}"
}
}
# パブリックサブネット2
resource "aws_subnet" "public_1c_subnet" {
vpc_id = aws_vpc.vpc.id
cidr_block = var.public_1c_cidr
availability_zone = "ap-northeast-1c"
map_public_ip_on_launch = true
tags = {
Name = "${var.name_prefix}${var.env}-${var.public_1c_name}"
}
}
# プライベートサブネット1
resource "aws_subnet" "private_1a_subnet" {
vpc_id = aws_vpc.vpc.id
cidr_block = var.private_1a_cidr
availability_zone = "ap-northeast-1a"
tags = {
Name = "${var.name_prefix}${var.env}-${var.private_1a_name}"
}
}
# プライベートサブネット2
resource "aws_subnet" "private_1c_subnet" {
vpc_id = aws_vpc.vpc.id
cidr_block = var.private_1c_cidr
availability_zone = "ap-northeast-1c"
tags = {
Name = "${var.name_prefix}${var.env}-${var.private_1c_name}"
}
}
# ---------------------------
# インターネットゲートウェイ
# ---------------------------
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.vpc.id
tags = {
Name = "${var.name_prefix}${var.env}-${var.igw_name}"
}
}
# ---------------------------
# ルートテーブル
# ---------------------------
# パブリックサブネット用のルートテーブル
resource "aws_route_table" "public_rtb" {
vpc_id = aws_vpc.vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = {
Name = "${var.name_prefix}${var.env}-${var.public_rtb_name}"
}
}
# ルートテーブルの関連付けパブリックサブネット1
resource "aws_route_table_association" "public_1a_rtb_associate" {
subnet_id = aws_subnet.public_1a_subnet.id
route_table_id = aws_route_table.public_rtb.id
}
# ルートテーブルの関連付けパブリックサブネット2
resource "aws_route_table_association" "public_1c_rtb_associate" {
subnet_id = aws_subnet.public_1c_subnet.id
route_table_id = aws_route_table.public_rtb.id
}