👶

【Terraform】for_eachを使ったボリューム生成とアタッチ処理

2021/10/24に公開

いきなり結論

# main.tf
data "aws_ami" "latest_amzn_linux" {
  owners      = ["137112412989"]
  most_recent = true

  filter {
    name   = "name"
    values = ["amzn2-ami-hvm-*-x86_64-gp2"]
  }
}

data "aws_availability_zones" "AZs" {}

resource "aws_instance" "web" {
  ami = data.aws_ami.latest_amzn_linux.id
  instance_type = "t2.micro"

  root_block_device {
    encrypted = true
    volume_size = 10
  }
}

# 以下から本題
resource "aws_ebs_volume" "web" {
  for_each = var.volume_info
  availability_zone = aws_instance.web.availability_zone
  size = each.value["volume_size"]
}

resource "aws_volume_attachment" "web" {
  for_each = var.volume_info
  instance_id = aws_instance.web.id
  volume_id = aws_ebs_volume.web[each.key].id
  device_name = each.value["device_name"]
}
# variables.tf
variable "volume_info" {
  type = map(any)
  default = {
    f = {
      volume_size = 10
      device_name = "/dev/sdf"
    }
    g = {
      volume_size = 30
      device_name = "/dev/sdg"
    }
    h = {
      volume_size = 40
      device_name = "/dev/sdh"
    }
    i = {
      volume_size = 50
      device_name = "/dev/sdi"
    }
    j = {
      volume_size = 60
      device_name = "/dev/sdj"
    }
  }
}
実行結果
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_ebs_volume.web["f"] will be created
  + resource "aws_ebs_volume" "web" {
      + arn               = (known after apply)
      + availability_zone = "ap-northeast-1a"
      + encrypted         = (known after apply)
      + id                = (known after apply)
      + iops              = (known after apply)
      + kms_key_id        = (known after apply)
      + size              = 10
      + snapshot_id       = (known after apply)
      + tags_all          = (known after apply)
      + throughput        = (known after apply)
      + type              = (known after apply)
    }

  # aws_ebs_volume.web["g"] will be created
  + resource "aws_ebs_volume" "web" {
      + arn               = (known after apply)
      + availability_zone = "ap-northeast-1a"
      + encrypted         = (known after apply)
      + id                = (known after apply)
      + iops              = (known after apply)
      + kms_key_id        = (known after apply)
      + size              = 30
      + snapshot_id       = (known after apply)
      + tags_all          = (known after apply)
      + throughput        = (known after apply)
      + type              = (known after apply)
    }

  # aws_ebs_volume.web["h"] will be created
  + resource "aws_ebs_volume" "web" {
      + arn               = (known after apply)
      + availability_zone = "ap-northeast-1a"
      + encrypted         = (known after apply)
      + id                = (known after apply)
      + iops              = (known after apply)
      + kms_key_id        = (known after apply)
      + size              = 40
      + snapshot_id       = (known after apply)
      + tags_all          = (known after apply)
      + throughput        = (known after apply)
      + type              = (known after apply)
    }

  # aws_ebs_volume.web["i"] will be created
  + resource "aws_ebs_volume" "web" {
      + arn               = (known after apply)
      + availability_zone = "ap-northeast-1a"
      + encrypted         = (known after apply)
      + id                = (known after apply)
      + iops              = (known after apply)
      + kms_key_id        = (known after apply)
      + size              = 50
      + snapshot_id       = (known after apply)
      + tags_all          = (known after apply)
      + throughput        = (known after apply)
      + type              = (known after apply)
    }

  # aws_ebs_volume.web["j"] will be created
  + resource "aws_ebs_volume" "web" {
      + arn               = (known after apply)
      + availability_zone = "ap-northeast-1a"
      + encrypted         = (known after apply)
      + id                = (known after apply)
      + iops              = (known after apply)
      + kms_key_id        = (known after apply)
      + size              = 60
      + snapshot_id       = (known after apply)
      + tags_all          = (known after apply)
      + throughput        = (known after apply)
      + type              = (known after apply)
    }

  # aws_instance.web will be created
  + resource "aws_instance" "web" {
      + ami                                  = "ami-02d36247c5bc58c23"
      + arn                                  = (known after apply)
      + associate_public_ip_address          = (known after apply)
      + availability_zone                    = (known after apply)
      + cpu_core_count                       = (known after apply)
      + cpu_threads_per_core                 = (known after apply)
      + disable_api_termination              = (known after apply)
      + ebs_optimized                        = (known after apply)
      + get_password_data                    = false
      + host_id                              = (known after apply)
      + id                                   = (known after apply)
      + instance_initiated_shutdown_behavior = (known after apply)
      + instance_state                       = (known after apply)
      + instance_type                        = "t2.micro"
      + ipv6_address_count                   = (known after apply)
      + ipv6_addresses                       = (known after apply)
      + key_name                             = (known after apply)
      + monitoring                           = (known after apply)
      + outpost_arn                          = (known after apply)
      + password_data                        = (known after apply)
      + placement_group                      = (known after apply)
      + placement_partition_number           = (known after apply)
      + primary_network_interface_id         = (known after apply)
      + private_dns                          = (known after apply)
      + private_ip                           = (known after apply)
      + public_dns                           = (known after apply)
      + public_ip                            = (known after apply)
      + secondary_private_ips                = (known after apply)
      + security_groups                      = (known after apply)
      + source_dest_check                    = true
      + subnet_id                            = (known after apply)
      + tags_all                             = (known after apply)
      + tenancy                              = (known after apply)
      + user_data                            = (known after apply)
      + user_data_base64                     = (known after apply)
      + vpc_security_group_ids               = (known after apply)

      + capacity_reservation_specification {
          + capacity_reservation_preference = (known after apply)

          + capacity_reservation_target {
              + capacity_reservation_id = (known after apply)
            }
        }

      + ebs_block_device {
          + delete_on_termination = (known after apply)
          + device_name           = (known after apply)
          + encrypted             = (known after apply)
          + iops                  = (known after apply)
          + kms_key_id            = (known after apply)
          + snapshot_id           = (known after apply)
          + tags                  = (known after apply)
          + throughput            = (known after apply)
          + volume_id             = (known after apply)
          + volume_size           = (known after apply)
          + volume_type           = (known after apply)
        }

      + enclave_options {
          + enabled = (known after apply)
        }

      + ephemeral_block_device {
          + device_name  = (known after apply)
          + no_device    = (known after apply)
          + virtual_name = (known after apply)
        }

      + metadata_options {
          + http_endpoint               = (known after apply)
          + http_put_response_hop_limit = (known after apply)
          + http_tokens                 = (known after apply)
        }

      + network_interface {
          + delete_on_termination = (known after apply)
          + device_index          = (known after apply)
          + network_interface_id  = (known after apply)
        }

      + root_block_device {
          + delete_on_termination = true
          + device_name           = (known after apply)
          + encrypted             = true
          + iops                  = (known after apply)
          + kms_key_id            = (known after apply)
          + throughput            = (known after apply)
          + volume_id             = (known after apply)
          + volume_size           = 10
          + volume_type           = (known after apply)
        }
    }

  # aws_volume_attachment.web["f"] will be created
  + resource "aws_volume_attachment" "web" {
      + device_name = "/dev/sdf"
      + id          = (known after apply)
      + instance_id = (known after apply)
      + volume_id   = (known after apply)
    }

  # aws_volume_attachment.web["g"] will be created
  + resource "aws_volume_attachment" "web" {
      + device_name = "/dev/sdg"
      + id          = (known after apply)
      + instance_id = (known after apply)
      + volume_id   = (known after apply)
    }

  # aws_volume_attachment.web["h"] will be created
  + resource "aws_volume_attachment" "web" {
      + device_name = "/dev/sdh"
      + id          = (known after apply)
      + instance_id = (known after apply)
      + volume_id   = (known after apply)
    }

  # aws_volume_attachment.web["i"] will be created
  + resource "aws_volume_attachment" "web" {
      + device_name = "/dev/sdi"
      + id          = (known after apply)
      + instance_id = (known after apply)
      + volume_id   = (known after apply)
    }

  # aws_volume_attachment.web["j"] will be created
  + resource "aws_volume_attachment" "web" {
      + device_name = "/dev/sdj"
      + id          = (known after apply)
      + instance_id = (known after apply)
      + volume_id   = (known after apply)
    }

Plan: 11 to add, 0 to change, 0 to destroy.

終わりに

※variables.tfのtypeの方がmap(any)とまだわかっていない部分なので、わかり次第更新します。
この記事が誰かの参考になれば幸いです😊

Discussion