我如何跳过Terraform资源的创建? 我的Terraform脚本正在为生产中的Web应用程序设置。 作为其中的一部分,我启用了Azure DDOS保护。 但是,与其他基础设施相比,这确实很昂贵。 为...

问题描述 投票:0回答:2

我可以通过一个选项传递到Terraform以跳过此资源吗?

在那里添加一个选项,然后我跳过DDOS资源,如果不存在的vnet的创建会在下面的摘要中失败吗?

#--------------------------------------- # 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
}
将以对象列表而不是单个对象出现。因此,您还需要更改您在虚拟网络配置中引用它的方式。
terraform azure-pipelines
2个回答
3
投票

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


2
投票
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所有资源以外的所有资源。 这是一个非常错过的功能,但似乎更高的头脑决定我们不想要它。

	
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.