我是 terraform 的新手,坚持做一些我认为可以做但不知道的事情。我想看看是否有一种方法可以使天蓝色子网中的委派块从 if/else 中可选。我尝试使用变量来完成此操作,但不断出现错误。
#variable:
variable "resource_group_name" {
type = string
description = "name of rg v-net is located"
}
variable "subnet_name" {
type = string
description = "name of subnet"
}
variable "subnet_address_prefixes" {
type = list(string)
description = "address space of subnet"
}
variable "service_endpoints" {
type = list(string)
default = null
description = "Service enpoints connected to subnet or vm"
}
variable "vnet_name" {
type = string
description = "name of vnet"
}
variable "delegation_name" {
type = string
default = ""
}
variable "service_delegation" {
type = object({
name = string
action = list(string)
})
default = {
name = "Microsoft.DBforMySQL/flexibleServers"
action = []
}
}
#module
resource "azurerm_subnet" "panynj_subnet" {
name = var.subnet_name
resource_group_name = var.resource_group_name
virtual_network_name = var.vnet_name
address_prefixes = var.subnet_address_prefixes
service_endpoints = var.service_endpoints
delegation {
name = var.delegation
service_delegation {
name = var.service_delegation.name
actions = var.service_delegation.action
}
}
我尝试将 if else 语句应用于变量,但这似乎不起作用,而且我不知道是否可以在动态块中完成。
通常,动态块与运行 0 或 1 次的
for_each
一起用作 if/else。
https://developer.hashicorp.com/terraform/language/expressions/dynamic-blocks
假设只有当
var.delegation_name
不为空时才应添加“委托”。
# your code here
dynamic "delegation" {
for_each = var.delegation_name != "" ? [1] : []
content {
name = var.delegation_name
service_delegation {
name = var.service_delegation.name
actions = var.service_delegation.action
}
}
}
[1]: https://developer.hashicorp.com/terraform/language/expressions/dynamic-blocks