なぜ作った
前回作ったリストだとoutputで情報を受け取るとき不便だった。
コード
main.tf
module mo_subnet {
source = "./modules/subnet"
vpc_id = module.mo_vpc.id
subnets = {
namae1 = {
name = "namae"
cidr_block = "10.0.1.0/24"
availability_zone = "ap-northeast-1a"
}
namae2 = {
name = "namae2"
cidr_block = "10.0.2.0/24"
availability_zone = "ap-northeast-1c"
}
}
}
output subnetdata {
value = module.mo_subnet.subnet.namae2.id
}
output subnetdataall {
value = module.mo_subnet.subnet
}
./modules/subnet/main.tf
# ====================
#
# Subnet
#
# ====================
variable "vpc_id" {
type = string
}
variable "subnets" {
type = map
}
resource aws_subnet this {
for_each = var.subnets
vpc_id = var.vpc_id
cidr_block = each.value.cidr_block
availability_zone = each.value.availability_zone
tags = {
Name = each.value.name
}
}
output "subnet" {
value = aws_subnet.this
}
参考
-https://www.hashicorp.com/blog/hashicorp-terraform-0-12-preview-for-and-for-each