簡體   English   中英

如何使用 terraform 中的 map 創建多個不同大小的 ebs 卷?

[英]How can I create several ebs volumes each of a different size using a map in terraform?

我有一個 terraform tfvars 文件,其中包含 map 的值,如下所示:

name_map = [
  {
    name         = "devbox"
    device_names = ["/dev/xvdg", "/dev/xvdh"]
    volume_size = ["900", "200"]
    group        = "hosts"
    instance_type = "m5a.2xlarge"
  },
  {
    name         = "devbox2"
    device_names = ["/dev/xvdg", "/dev/xvdh"]
    volume_size = ["300", "200"]
    group        = "hosts"
    instance_type = "m5a.2xlarge"
  }
]

] 我的 tf 文件如下所示:

resource "aws_instance" "node" {
  count         = length(var.name_map)

  dynamic "ebs_block_device" {
    for_each = [for device in var.name_map.*.device_names[count.index] : {
      device_name = device,
      volume_size = var.name_map.*.volume_size[count.index]
    }]
    content {
      device_name           = ebs_block_device.value.device_name
      volume_type           = "gp2"
      volume_size           = ebs_block_device.value.volume_size
      delete_on_termination = true
    }
  }

所以基本上對於“devbox”實例,我希望“/dev/xvdg”為 900 gbs,“/dev/xvdh”為 200 gbs。 我希望當前設置可以遍歷每個映射的設備名稱並獲得單個卷大小,但我正在嘗試擴展它以包含每個設備的不同卷大小。

我該怎么做?

我試過嵌套的 for_each 語句,但我不斷收到錯誤。 扁平結構會是這里的解決方案嗎? 我很想看看這會是什么樣子的例子。

我認為您想要做的是:

resource "aws_instance" "node" {
  count = length(var.name_map)

  instance_type = var.name_map[count.index].instance_type
  ami = "..." # Fill in a valid AMI here

  dynamic "ebs_block_device" {
    for_each = [for i, device in var.name_map[count.index].device_names : {
      device_name = device,
      volume_size = var.name_map[count.index].volume_size[i]
    }]
    content {
      device_name           = ebs_block_device.value.device_name
      volume_type           = "gp2"
      volume_size           = ebs_block_device.value.volume_size
      delete_on_termination = true
    }
  }
}

雖然這有效,但我建議您執行以下操作:

variable "name_map" {
  default = [
    {
      name = "devbox"
      devices = [
        {
          device_name = "/dev/xvdg",
          volume_size = 900
        },
        {
          device_name = "/dev/xvdh",
          volume_size = 200
        }
      ]
      group         = "hosts"
      instance_type = "m5a.2xlarge"
    },
    {
      name = "devbox2"
      devices = [
        {
          device_name = "/dev/xvdg",
          volume_size = 900
        },
        {
          device_name = "/dev/xvdh",
          volume_size = 200
        }
      ]
      group         = "hosts"
      instance_type = "m5a.2xlarge"
    }
  ]
}

請注意, device_namevolume_size組合在一起。 現在我們可以使用一個簡單的foor循環,我們不必依賴索引:

resource "aws_instance" "node" {
  count = length(var.name_map)

  instance_type = var.name_map[count.index].instance_type
  ami           = "..." # fill in a valid AMI name

  dynamic "ebs_block_device" {
    # Notice the i variable (index) was dropped here
    for_each = [for device in var.name_map[count.index].devices : {
      device_name = device.device_name,
      volume_size = device.volume_size
    }]
    content {
      device_name           = ebs_block_device.value.device_name
      volume_type           = "gp2"
      volume_size           = ebs_block_device.value.volume_size
      delete_on_termination = true
    }
  }
}

我會進一步嵌套你的 map 來創建這樣的東西:

name_map = [
  {
    name         = "devbox"
    root_block_device = {
      ...settings
    }
    ebs_block_devices = toSet([
      {
        name = "/dev/xvdg"
        size = "900"
      },{
        name = "/dev/xvdh"
        size = "200"
      }
    ])
    group        = "hosts"
    instance_type = "m5a.2xlarge"
  },
  ...
]

然后在您的資源代碼中,您可以遍歷每個實例的集合:

resource "aws_instance" "instance" {
  count = length(var.name_map)

  ...

  root_block_device {
    ...settings from var.name_map[count.index].root_block_device
  }

  dynamic "ebs_block_device" {
    for_each = var.name_map[count.index].ebs_block_devices

    content {
      device_name = ebs_block_device.value.name
      volume_size = ebs_block_device.value.size
    }
  }
}

如果您希望根卷在終止后持續存在,我建議添加一個 EBS 根卷,否則您可以忽略 root_block_device,它將創建一個包含圖像的臨時設備。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM