Terraform 嵌套列表中的地形嵌套循环

Terraform 嵌套列表中的地形嵌套循环,terraform,terraform-provider-azure,terraform0.12+,Terraform,Terraform Provider Azure,Terraform0.12+,我正在尝试创建一个动态方法,以便在最终用户将配置的多个环境中创建VM。 尝试使用嵌套循环进行循环。压平函数、计数等,但尚未找到实现目标的方法。 我有以下结构的terrafrom.tfvars: Bigip_devices = { main_hub = { region = "eastus" azs = ["1"] #Azure availabilty

我正在尝试创建一个动态方法,以便在最终用户将配置的多个环境中创建VM。 尝试使用嵌套循环进行循环。压平函数、计数等,但尚未找到实现目标的方法。 我有以下结构的terrafrom.tfvars:

Bigip_devices = {
    main_hub = {
      region             = "eastus"
      azs                = ["1"]            #Azure availabilty zones
      vnet_name               = "vnet-main"   # Vnet name to deploy to
      bigip_instance_count = 2            # Number of instnaces to delpoy
      cluster             = "yes"         # Deploy as a cluster or stand alone device
      version             = ""            # Leave blank for default value
      sku                 = ""            # Leave blank for default value - f5-bigip-virtual-edition-25m-best-hourly
      offer               = ""            # Leave blank for default value - f5-big-ip-best
      instance_type       = ""            # Leave blank for default value - Standard_DS3_v2
      disable_password_authentication = ""     #Leave blank for default value
      tags                = ""
    }
    spoke = {
      region             = "eastus"
      azs                = ["1","2"]            #Azure availabilty zones
      vnet_name               = "vnet-spoke"   # Vnet name to deploy to
      bigip_instance_count = 4            # Number of instnaces to delpoy
      cluster             = "yes"         # Deploy as a cluster or stand alone device
      version             = ""            # Leave blank for default value
      sku                 = ""            # Leave blank for default value - f5-bigip-virtual-edition-25m-best-hourly
      offer               = ""            # Leave blank for default value - f5-big-ip-best
      instance_type       = ""            # Leave blank for default value - Standard_DS3_v2
      disable_password_authentication = ""     #Leave blank for default value
      tags                = ""
    }
  }
迭代列表中的每个键(在本例中为2个键-主轴辐键)并创建与bigip\u instance\u count设置相对应的虚拟机数量的正确方法是什么。 在上面的示例中,我想创建两个环境,一个有2个设备,另一个有4个设备。
有什么方法可以实现它吗?

如果您将上述复杂的JSON转换成一个集合,每个要创建的资源都有一个元素,那将非常方便。要实现这一点,您可以使用
展平
功能

locals {
  # A list of objects with one object per instance.
  flattened_values = flatten([
    for ip_key, ip in var.Bigip_devices : [
      for index in range(ip.bigip_instance_count) : {
        region  = ip.region
        azs            = ip.azs
        ip_index      = index
        ip_key        = ip_key
        cluster         = ip.cluster
        version        = ip.version
        sku   = ip.sku
        offer = ip.offer
        instance_type = ip.instance_type
        disable_password_authentication = ip.disable_password_authentication
        tags = ip.tags
      }
    ]
  ])
}
使用上面的
展平
功能,您可以在下面获得您想要创建的资源集合列表

flattened_value_output = [
  {
    "azs" = [
      "1",
    ]
    "cluster" = "yes"
    "disable_password_authentication" = ""
    "instance_type" = ""
    "ip_index" = 0
    "ip_key" = "main_hub"
    "offer" = ""
    "region" = "eastus"
    "sku" = ""
    "tags" = ""
    "version" = ""
  },
  {
    "azs" = [
      "1",
    ]
    "cluster" = "yes"
    "disable_password_authentication" = ""
    "instance_type" = ""
    "ip_index" = 1
    "ip_key" = "main_hub"
    "offer" = ""
    "region" = "eastus"
    "sku" = ""
    "tags" = ""
    "version" = ""
  },
  {
    "azs" = [
      "1",
      "2",
    ]
    "cluster" = "yes"
    "disable_password_authentication" = ""
    "instance_type" = ""
    "ip_index" = 0
    "ip_key" = "spoke"
    "offer" = ""
    "region" = "eastus"
    "sku" = ""
    "tags" = ""
    "version" = ""
  },
  {
    "azs" = [
      "1",
      "2",
    ]
    "cluster" = "yes"
    "disable_password_authentication" = ""
    "instance_type" = ""
    "ip_index" = 1
    "ip_key" = "spoke"
    "offer" = ""
    "region" = "eastus"
    "sku" = ""
    "tags" = ""
    "version" = ""
  },
  {
    "azs" = [
      "1",
      "2",
    ]
    "cluster" = "yes"
    "disable_password_authentication" = ""
    "instance_type" = ""
    "ip_index" = 2
    "ip_key" = "spoke"
    "offer" = ""
    "region" = "eastus"
    "sku" = ""
    "tags" = ""
    "version" = ""
  },
  {
    "azs" = [
      "1",
      "2",
    ]
    "cluster" = "yes"
    "disable_password_authentication" = ""
    "instance_type" = ""
    "ip_index" = 3
    "ip_key" = "spoke"
    "offer" = ""
    "region" = "eastus"
    "sku" = ""
    "tags" = ""
    "version" = ""
  },
]
从上面的集合中,您可以迭代并创建具有唯一键的资源,如下所示:

resource "some_resource" "example" {
  for_each = {
    # Generate a unique string identifier for each instance
    for ip in local.flattened_value_output : format("%s-%02d", ip.ip_key, ip.ip_index + 1) => ip
  }
}
这样,可以保证资源的创建或更新,因为每个资源都使用唯一的密钥


有关更多详细信息,请参阅我与Hashicorp人员的交流。

非常棒的解决方案!谢谢,很有魅力。这个例子和解释也很好。