Azure上的Terraform-我尝试在一个资源中调用另一个资源中的for_each块的结果

tcomlyy6  于 2023-02-16  发布在  其他
关注(0)|答案(1)|浏览(157)

Azure上的Terraform-我试图在另一个资源中调用一个资源中的for_each块的结果。
我已经经历了以下&似乎无法理解其中的逻辑。
https://stackoverflow.com/questions/71646136/terraform-reference-a-for-each-resource-from-another-for-each-resource
https://stackoverflow.com/questions/68571073/for-each-loop-in-resource-block-terraform
我有2个资源块[我添加了本地文件]

# Local Variables
#
locals {
  # Create a flattened list for resource groups in multiple regions
  multi_region_rg = flatten([
    for rg_key, rg in var.multiple_resource_groups : [
      for region_key, region in var.regions : {
        rg_name     = var.environment != "" ? format("%s-rg-%s-%s-%s", var.tenant, rg_key, var.environment, region.code) : format("%s-rg-%s-%s", var.tenant, rg_key, region.code)
        region_name = region.name
      }
    ]
  ])
}

# Local Variables
#
locals {
  # Create a flattened list for resource groups in multiple regions
  multi_region_vnet = flatten([
    for vnet_key, vnet in var.multiple_vnets : [
      for region_key, region in var.regions : {
        vnet_name                     = var.environment != "" ? format("%s-rg-%s-%s-%s", var.tenant, vnet_key, var.environment, region.code) : format("%s-rg-%s-%s", var.tenant, vnet_key, region.code)
        region_name                   = region.name
        virtual_network_address_space = vnet.address_space
      }
    ]
  ])
}

TFVARS文件
x一个一个一个一个x一个一个二个一个x一个一个三个一个
这些块在独立运行时工作良好,但我希望能够并发地运行它们,以便将vnet部署到创建的资源组中。
如果我使用resource_group_name = azurerm_resource_group.rg[each.key]这一行,它将抛出此错误

PS C:\PycharmProjects\terraform-projects\my_azure_connectivity_ref_repo\hub-and-spoke\modules\vnet> terraform plan  
╷
│ Error: Invalid index
│ 
│   on main.tf line 69, in resource "azurerm_virtual_network" "vnet":
│   69:   resource_group_name = azurerm_resource_group.rg[each.key]
│     ├────────────────
│     │ azurerm_resource_group.rg is object with 2 attributes
│     │ each.key is "rg-vnetx-ae"
│ 
│ The given key does not identify an element in this collection value.
╵
╷
│ Error: Invalid index
│
│   on main.tf line 69, in resource "azurerm_virtual_network" "vnet":
│   69:   resource_group_name = azurerm_resource_group.rg[each.key]
│     ├────────────────
│     │ azurerm_resource_group.rg is object with 2 attributes
│     │ each.key is "rg-vnetx-ase"
│
│ The given key does not identify an element in this collection value.
╵
PS C:\PycharmProjects\terraform-projects\my_azure_connectivity_ref_repo\hub-and-spoke\modules\vnet>
cdmah0mi

cdmah0mi1#

resource_group_name属性中的resource "azurerm_virtual_network" "vnet"定义存在两个问题。

  • azurerm_resource_group.rg[each.key]表达式中的[each.key]不存在.这里[each.key]是实际来自for_each = { for k, v in local.multi_region_vnet : v.vnet_name => v}的vnet名称,它在资源azurerm_resource_group.rg中不可用
> { for k, v in local.multi_region_vnet : v.vnet_name => v }
{
  "test-rg-vnet_key-dev-ae" = {
    "region_name" = "australiaeast"
    "virtual_network_address_space" = [
      "10.1.0.0/24",
    ]
    "vnet_name" = "test-rg-vnet_key-dev-ae"
  }
  "test-rg-vnet_key-dev-ase" = {
    "region_name" = "australiasoutheast"
    "virtual_network_address_space" = [
      "10.1.0.0/24",
    ]
    "vnet_name" = "test-rg-vnet_key-dev-ase"
  }
}
  • 另一个问题还在于vnet资源的resource_group_name属性与引用。尽管azurerm_resource_group.rg[$(expression)]应为azurerm_resource_group.rg[$(expression)].name

因此,您必须使用resource_group和vnet通用的标准关键字调整任一资源。我已在具有location/region的资源组中完成此操作,因为这两个资源都需要此关键字。
因此,解决上述两个问题的正确代码是。

## key is now region_name in azurerm_resource_group  ##

resource "azurerm_resource_group" "rg" {
  for_each = {
    for k, v in local.multi_region_rg : v.region_name => v
  }
  name     = each.value.rg_name
  location = each.key

}

resource "azurerm_virtual_network" "vnet" {
  for_each = {
    for k, v in local.multi_region_vnet : v.vnet_name => v
  }
  name                = each.key
  location            = each.value.region_name
  address_space       = each.value.virtual_network_address_space
  resource_group_name = azurerm_resource_group.rg[(each.value.region_name)].name
}

代码在运行

➜  random_local_tests git:(main) ✗ terraform apply -auto-approve 

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:

  # azurerm_resource_group.rg["australiaeast"] will be created
  + resource "azurerm_resource_group" "rg" {
      + id       = (known after apply)
      + location = "australiaeast"
      + name     = "test-rg-vnet_key-dev-ae"
    }

  # azurerm_resource_group.rg["australiasoutheast"] will be created
  + resource "azurerm_resource_group" "rg" {
      + id       = (known after apply)
      + location = "australiasoutheast"
      + name     = "test-rg-vnet_key-dev-ase"
    }

  # azurerm_virtual_network.vnet["test-rg-vnet_key-dev-ae"] will be created
  + resource "azurerm_virtual_network" "vnet" {
      + address_space       = [
          + "10.1.0.0/24",
        ]
      + dns_servers         = (known after apply)
      + guid                = (known after apply)
      + id                  = (known after apply)
      + location            = "australiaeast"
      + name                = "test-rg-vnet_key-dev-ae"
      + resource_group_name = "test-rg-vnet_key-dev-ae"
      + subnet              = (known after apply)
    }

  # azurerm_virtual_network.vnet["test-rg-vnet_key-dev-ase"] will be created
  + resource "azurerm_virtual_network" "vnet" {
      + address_space       = [
          + "10.1.0.0/24",
        ]
      + dns_servers         = (known after apply)
      + guid                = (known after apply)
      + id                  = (known after apply)
      + location            = "australiasoutheast"
      + name                = "test-rg-vnet_key-dev-ase"
      + resource_group_name = "test-rg-vnet_key-dev-ase"
      + subnet              = (known after apply)
    }

Plan: 4 to add, 0 to change, 0 to destroy.
azurerm_resource_group.rg["australiaeast"]: Creating...
azurerm_resource_group.rg["australiasoutheast"]: Creating...
azurerm_resource_group.rg["australiaeast"]: Creation complete after 5s [id=/subscriptions/xxxxxxxxxxxx/resourceGroups/test-rg-vnet_key-dev-ae]
azurerm_resource_group.rg["australiasoutheast"]: Creation complete after 6s [id=/subscriptions/xxxxxxxxxxxx/resourceGroups/test-rg-vnet_key-dev-ase]
azurerm_virtual_network.vnet["test-rg-vnet_key-dev-ae"]: Creating...
azurerm_virtual_network.vnet["test-rg-vnet_key-dev-ase"]: Creating...
azurerm_virtual_network.vnet["test-rg-vnet_key-dev-ase"]: Still creating... [10s elapsed]
azurerm_virtual_network.vnet["test-rg-vnet_key-dev-ae"]: Still creating... [10s elapsed]
azurerm_virtual_network.vnet["test-rg-vnet_key-dev-ae"]: Creation complete after 18s [id=/subscriptions/xxxxxxxxxxxx/resourceGroups/test-rg-vnet_key-dev-ae/providers/Microsoft.Network/virtualNetworks/test-rg-vnet_key-dev-ae]
azurerm_virtual_network.vnet["test-rg-vnet_key-dev-ase"]: Creation complete after 19s [id=/subscriptions/xxxxxxxxxxxx/resourceGroups/test-rg-vnet_key-dev-ase/providers/Microsoft.Network/virtualNetworks/test-rg-vnet_key-dev-ase]

Apply complete! Resources: 4 added, 0 changed, 0 destroyed.

希望有帮助。
和其他信息,这是不相关的这里太多,我用静态值,而不是一些变量。

rg_name = var.environment != "" ? format("%s-rg-%s-%s-%s", "test", "vnet_key", var.environment, region.code) : format("%s-rg-%s-%s", "test", "vnet_key", region.code)

vnet_name = var.environment != "" ? format("%s-rg-%s-%s-%s", "test", "vnet_key", var.environment, region.code) : format("%s-rg-%s-%s", "test", "vnet_key", region.code)

相关问题