azure - 在 terraform 中引用对象/ map 类型值中的值的正确方法是什么

标签 azure terraform terraform-provider-azure azure-rm

目标:在创建资源时分配特定的键值

变量.tf:

variable "vnet_address_space" {
    type = map(list(string))
    default = {
        "Dev"  = ["xx.xx.0.0/24","xx.xx.0.0/24","xx.xx.0.0/24","xx.xx.0.0/20"]
        "Stage" = ["xx.xx.0.0/24","xx.xx.0.0/24","xx.xx.0.0/24","xx.xx.0.0/20"]
        "Prod" = ["xx.xx.0.0/24","xx.xx.0.0/24","xx.xx.0.0/24","xx.xx.0.0/20"]
    }
}

现在我使用下面的代码创建具有基于环境(即开发、阶段、产品等)的地址空间的 VNET

resource "azurerm_virtual_network" "vnet" {
  name                = var.hub_vnet_name
  location            = azurerm_resource_group.rg[0].location
  resource_group_name = azurerm_resource_group.rg[0].name
  for_each = {for k,v in var.vnet_address_space: k=>v if k == "Dev"}
  address_space       =  var.vnet_address_space.Dev
  dns_servers         = var.dns_servers
  tags     = {
    environment = "${var.env}"
    costcentre = "14500"
  }
  dynamic "ddos_protection_plan" {
    for_each = local.if_ddos_enabled

    content {
      id     = azurerm_network_ddos_protection_plan.ddos[0].id
      enable = false
    }
  }
}

错误:

Error: Invalid value for input variable
│ 
│   on /home/circleci/workingfiles/*********output.tfvars.json line 15:
│   15:   "vnet_address_space": [
│   16:     "xx.xx.0.0/24",
│   17:     "xx.xx.0.0/20",
│   18:     "xx.xx.0.0/24",
│   19:     "xx.xx.0.0/20"
│   20:   ],
│ 
│ The given value is not suitable for var.vnet_address_space declared at variables.tf:25,1-30: map of list of string required.
╵

我应该如何告诉 Terraform,在变量中获取开发地址空间并创建 vnet?

请帮忙

最佳答案

该错误意味着您的 output.tfvars.json 不正确。它与您的 azurerm_virtual_network 无关。在您的 output.tfvars.json 中,您应该具有:

vnet_address_space = {
        "Dev"  = ["xx.xx.0.0/24","xx.xx.0.0/24","xx.xx.0.0/24","xx.xx.0.0/20"]
        "Stage" = ["xx.xx.0.0/24","xx.xx.0.0/24","xx.xx.0.0/24","xx.xx.0.0/20"]
        "Prod" = ["xx.xx.0.0/24","xx.xx.0.0/24","xx.xx.0.0/24","xx.xx.0.0/20"]
    }

关于azure - 在 terraform 中引用对象/ map 类型值中的值的正确方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73349363/

相关文章:

node.js - 如何使用 Node.js 将 Windows Azure Blob 流式传输到客户端?

c# - Azure 字符串未被识别为有效的日期时间

Terraform:在同一类型上同时使用数据源和资源有什么意义?

azure - 使用 terraform 创建新的 azure 资源组时出错

azure - 有关 Azure (Terraform) 中虚拟机 "Plan"的问题

azure - 诊断设置 - Master”已经存在 - 要通过 Terraform 进行管理,需要将此资源导入到状态中

Azure 存储复制与 Azure 备份

c# - Azure 事件中心 : checkpoint best practices in C# EventProcessorClient

azure - 在 terraform 的 azure data explorer 中使用 eventhub 的默认消费者组

azure - 使用 for_each 时从 Terraform 创建库存文件