目标:在创建资源时分配特定的键值
变量.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/