terraform 中一种资源的互斥属性

mutually exclusive attributes to one resource in terraform

我需要一些帮助,我正在尝试为 azure ACI 容器实例编写一个模块。 我发现两个属性是互斥的,dns_name_label 和 network_profile_id。 如果我把ip_address_type设置成public,我想用dns_name_label,但是network_profile_id不能来同一个脚本 反之亦然,如果我设置ip_address_type为Private,我必须定义network_profile_id,但是dns_name_label不能来脚本

如果无论如何都包含 dns_name_label 和 network_profile_id,根据 ip_address_type 判断?

resource "azurerm_container_group" "this" {
  name                = var.name
  location            = var.location
  resource_group_name = var.resource_group_name
  ip_address_type     = var.ip_address_type
  dns_name_label = var.ip_address_type =="Public"&&length(var.dns_name_label)> 0 ? var.dns_name_label :""
   os_type            = var.os_type
  restart_policy     = var.restart_policy
  network_profile_id = var.ip_address_type == "Private" ? azurerm_network_profile.this[0].id : ""

}

注意上面的代码不起作用,我收到错误

"network_profile_id": conflicts with dns_name_label

您可以使用 null 而不是 "",这将从资源中删除属性 null 设置为其值:

resource "azurerm_container_group" "this" {
  name                = var.name
  location            = var.location
  resource_group_name = var.resource_group_name
  ip_address_type     = var.ip_address_type
  dns_name_label = var.ip_address_type =="Public"&&length(var.dns_name_label)> 0 ? var.dns_name_label : null
   os_type            = var.os_type
  restart_policy     = var.restart_policy
  network_profile_id = var.ip_address_type == "Private" ? azurerm_network_profile.this[0].id : null
}