My Note

自己理解のためのブログ

Terraform ( v0.12.0 ) でAWSの環境構築 ( VPC・NW )

やったこと

yhidetoshi.hatenablog.com

で書いたコードをTerraformのバージョンを0.11.14から0.12.0アップデートして書き直してみた。 今回構築する環境は以下です。

■ 構築する構成図 (workspace=prod)

f:id:yhidetoshi:20190621223805p:plain

変更点

  • vpcを作成するコードで0.12.0で書き直したところは 変数を以下のように書き換えができる。
    • First-Class Expressions
      • (変更前): "${var.hoge}"
      • (変更後): var.hoge

■ 参考: HashiCorp Terraform 0.12 Preview: First-Class Expressions

Terraformのコード全体

ディレクトリ構造(一部抜粋)

├── modules
│   └── vpc
│       └── main.tf
├── s3-backend.tf
└── vpc.tf

https://yhidetoshi.hatenablog.com/entry/2019/02/27/000000

■ module側( vpc.tf )

yhidetoshi.hatenablog.com

のモジュール側のコードは同じ。

■ resource側( vpc/main.tf )

  • 前回からの追加・修正箇所
    • ssmパラメータにkey: vpc_id value: 作成されたvpc_id を格納するようにした。
resource "aws_ssm_parameter" "vpc_id" {
  name      = "vpc_id"
  type      = "String"
  value     = aws_vpc.mod.id
  overwrite = true

  depends_on = ["aws_vpc.mod"]
}

■ コード全体

variable "name" {}
variable "cidr_block" {}

variable "public_subnets" {
  default = []
}

variable "private_subnets" {
  default = []
}

variable "public_subnets_name" {
  default = []
}

variable "private_subnets_name" {
  default = []
}

variable "availability_zone" {
  default = []
}

variable "enable_dns_hostnames" {
  default = true
}

variable "enable_dns_support" {
  default = true
}

variable "enable_nat_gateway" {
  default = false
}

variable "private_propagating_vgws" {
  default = []
}

variable "map_public_ip_on_launch" {
  default = false
}

variable "public_propagating_vgws" {
  default = []
}

variable "tags" {
  default = {}
}

variable "main" {
  default = {}
}

variable "nat_gateway_num" {
  default = 0
}

variable "service_name" {}
variable "exe_flag_vpcendpoint" {}

variable "dhcp_domain_name" {}

variable "dhcp_tag_name" {}

variable "dhcp_domain_name_servers" {
  default = []
}

resource "aws_vpc" "mod" {
  cidr_block           = var.cidr_block
  enable_dns_hostnames = var.enable_dns_hostnames
  enable_dns_support   = var.enable_dns_support
  tags                 = merge(var.tags, map("Name", format("%s", var.name)))
}

resource "aws_ssm_parameter" "vpc_id" {
  name      = "vpc_id"
  type      = "String"
  value     = aws_vpc.mod.id
  overwrite = true

  depends_on = ["aws_vpc.mod"]
}

resource "aws_vpc_dhcp_options" "dhcp" {
  domain_name         = var.dhcp_domain_name
  domain_name_servers = var.dhcp_domain_name_servers

  tags = {
    Name = var.dhcp_tag_name
  }
}

# InternetGWを作成
resource "aws_internet_gateway" "mod_hoge" {
  vpc_id = "${aws_vpc.mod.id}"
  tags   = merge(var.tags, map("Name", format("%s-igw", var.name)))
}

# Publicのルートテーブルを作成
resource "aws_route_table" "public" {
  vpc_id           = "${aws_vpc.mod.id}"
  propagating_vgws = var.public_propagating_vgws
  tags             = merge(var.tags, map("Name", format("%s--rt-main", var.name)))
}

# Privateのルートテーブルを作成
resource "aws_route_table" "private" {
  vpc_id           = "${aws_vpc.mod.id}"
  propagating_vgws = var.private_propagating_vgws
  count            = var.nat_gateway_num
  tags             = "${merge(var.tags, map("Name", format("%s-nat-%s", var.name, element(var.availability_zone, count.index))))}"

}

# ルートテーブルとInternetGWを結ぶ(ルートテーブルのルールを作るモジュール)
resource "aws_route" "public_internet_gateway" {
  route_table_id         = "${aws_route_table.public.id}"
  destination_cidr_block = "0.0.0.0/0"
  gateway_id             = "${aws_internet_gateway.mod_hoge.id}"
}

# ルートテーブルとNATGWを結ぶ (ルートテーブルのルールを作るモジュール)
resource "aws_route" "private_nat_gateway" {
  route_table_id         = "${element(aws_route_table.private.*.id, count.index)}"
  destination_cidr_block = "0.0.0.0/0"
  nat_gateway_id         = "${element(aws_nat_gateway.natgw.*.id, count.index)}"
  count                  = var.nat_gateway_num
}

# Privateサブネットを作成
resource "aws_subnet" "private" {
  vpc_id            = "${aws_vpc.mod.id}"
  cidr_block        = var.private_subnets[count.index]
  availability_zone = "${element(var.availability_zone, count.index)}"
  count             = "${length(var.private_subnets)}"
  tags              = "${map("Name", element(var.private_subnets_name, count.index))}"
}

# Publicのサブネットを作成
resource "aws_subnet" "public" {
  vpc_id                  = "${aws_vpc.mod.id}"
  cidr_block              = var.public_subnets[count.index]
  availability_zone       = "${element(var.availability_zone, count.index)}"
  count                   = "${length(var.public_subnets)}"
  tags                    = "${map("Name", element(var.public_subnets_name, count.index))}"
  map_public_ip_on_launch = var.map_public_ip_on_launch
}

# NAT-GWにEIPを付与
resource "aws_eip" "nateip" {
  vpc   = true
  count = var.nat_gateway_num
  tags  = "${merge(var.tags, map("Name", format("NATGW-%s-%s", var.name, element(var.availability_zone, count.index))))}"
}

# VPC EndPointの作成
resource "aws_vpc_endpoint" "private_s3" {
  count        = var.exe_flag_vpcendpoint ? 1 : 0
  vpc_id       = aws_vpc.mod.id
  service_name = var.service_name
}

# privateサブネットにs3 endpointを付与する
resource "aws_vpc_endpoint_route_table_association" "private_subnet" {
  count = "${length(var.private_subnets) >= 1 && var.exe_flag_vpcendpoint ? length(var.private_subnets) : 0}"
  //  vpc_endpoint_id = "${aws_vpc_endpoint.private_s3.id}"
  vpc_endpoint_id = "${element(aws_vpc_endpoint.private_s3.*.id, count.index)}"
  route_table_id  = "${element(aws_route_table.private.*.id, count.index)}"
}

# publicサブネットにs3 endopointを付与する
resource "aws_vpc_endpoint_route_table_association" "public_subnet" {
  count           = "${length(var.private_subnets) >= 1 && var.exe_flag_vpcendpoint ? length(var.private_subnets) : 0}"
  vpc_endpoint_id = "${element(aws_vpc_endpoint.private_s3.*.id, count.index)}"
  route_table_id  = "${element(aws_route_table.public.*.id, count.index)}"
}

# NAT GWを作成
resource "aws_nat_gateway" "natgw" {
  allocation_id = "${element(aws_eip.nateip.*.id, count.index)}"
  subnet_id     = "${element(aws_subnet.public.*.id, count.index)}"
  count         = var.nat_gateway_num
  tags          = "${merge(var.tags, map("Name", format("NATGW-%s-%s", var.name, element(var.availability_zone, count.index))))}"
  depends_on    = ["aws_internet_gateway.mod_hoge"]
}

# Privateサブネットとルートテーブルを結ぶ
resource "aws_route_table_association" "private" {
  count          = "${length(var.private_subnets)}"
  subnet_id      = "${element(aws_subnet.private.*.id, count.index)}"
  route_table_id = "${element(aws_route_table.private.*.id, count.index)}"
}

# メインルートテーブル
resource "aws_main_route_table_association" "main" {
  vpc_id         = "${aws_vpc.mod.id}"
  route_table_id = aws_route_table.public.id
}

まとめ

Terraformのバージョンを 0.11.14 から 0.12.0 にバージョンを上げた。 今回は、以前に作成したVPC/NW周りのコードを 0.12.0版に修正した。 変数に "${}" が不要になったので、コードとしてもすっきりするし、書くのが楽になった。