我可以通过一个选项传递到Terraform以跳过此资源吗?
#---------------------------------------
# DDOS Protection Plan Definition
#---------------------------------------
resource "azurerm_network_ddos_protection_plan" "ddos" {
name = var.ddos_plan_name
location = var.location
resource_group_name = azurerm_resource_group.rg.name
}
#---------------------------------------
# vNet Definition
#---------------------------------------
resource "azurerm_virtual_network" "vnet" {
name = lower("${local.vNet_id}-${var.location_id}-${var.env_id}-1")
resource_group_name = azurerm_resource_group.rg.name
location = var.location
address_space = var.address_space
ddos_protection_plan {
id = azurerm_network_ddos_protection_plan.ddos.id
enable = true
}
depends_on = [
azurerm_resource_group.rg
]
}
您可以使用
meta-argument
动态选择要创建的特定资源的数量,包括可能选择创建其中的Zero,因此有效地完全禁用了资源:
count
variable "enable_ddos_protection" {
type = bool
default = true
}
resource "azurerm_network_ddos_protection_plan" "ddos" {
count = var.enable_ddos_protection ? 1 : 0
name = var.ddos_plan_name
location = var.location
resource_group_name = azurerm_resource_group.rg.name
}
将以对象列表而不是单个对象出现。因此,您还需要更改您在虚拟网络配置中引用它的方式。
azurerm_network_ddos_protection_plan.ddos
块
告诉Terraform,每个资源的实例都会生成一个
dynamic
块,因此,如果没有保护计划,则不会有该类型的块实例:
ddos_protection_plan
(我在此处删除了
resource "azurerm_virtual_network" "vnet" {
name = lower("${local.vNet_id}-${var.location_id}-${var.env_id}-1")
resource_group_name = azurerm_resource_group.rg.name
location = var.location
address_space = var.address_space
dynamic "ddos_protection_plan" {
for_each = azurerm_network_ddos_protection_plan.ddos
content {
id = ddos_protection_plan.value.id
enable = true
}
}
}
声明,因为它在
depends_on
参数中是多余的,但是resource_group_name
块是此示例的要点。))
我这样做的方法是使用
dynamic
meta-argument [1]。例如,使用名称count
创建一个变量,将其设置为类型create_ddos_protection_plan
,默认情况下将其设置为
bool
::
false
variable "create_ddos_protection_plan" {
description = "Whether to create DDoS resource or not."
type = bool
default = false
}
resource "azurerm_network_ddos_protection_plan" "ddos" {
count = var.create_ddos_protection_plan ? 1 : 0
name = var.ddos_plan_name
location = var.location
resource_group_name = azurerm_resource_group.rg.name
}
或完全删除
true
meta-argument。
如果基于当前设置不存在资源,则创建将失败。
[1]https://www.terraform.io/language/meta-arguments/count 您想要的是负面的。 不,不幸的是不存在。
只有替代方案是-Arget所有资源以外的所有资源。 这是一个非常错过的功能,但似乎更高的头脑决定我们不想要它。