LoginSignup
0
0

More than 3 years have passed since last update.

aws_subnetをmapで作る

Posted at

なぜ作った

前回作ったリストだと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
- https://github.com/terraform-aws-modules/terraform-aws-vpc/blob/v1.0.0/main.tf

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0