【问题标题】:How to skip certain part of a resource from being provisioned using Terraform如何跳过使用 Terraform 配置资源的某些部分
【发布时间】:2021-08-24 08:45:34
【问题描述】:
# main.tf
resource "azurerm_api_management" "apim_demo" {
  name                = var.apim_instance_name
  location            = azurerm_resource_group.apim_rg.location
  resource_group_name = azurerm_resource_group.apim_rg.name
  publisher_name      = var.apim_publisher_name
  publisher_email     = var.apim_publisher_email

  sku_name = var.apim_sku_name

  identity {
    type = "SystemAssigned"
  }
  hostname_configuration {
    proxy {
      default_ssl_binding          = true
      host_name                    = "qtech"
      key_vault_id                 = "https://ssl-key-test789.vault.azure.net/secrets/my-ssl-certificate"
      negotiate_client_certificate = true
    }
    proxy {
      default_ssl_binding          = false
      host_name                    = "ftech"
      key_vault_id                 = "https://ssl-key-test789.vault.azure.net/secrets/my-ssl-certificate2"
      negotiate_client_certificate = true
      #custom                       = var.custom_block
      #count                       = var.test_condition ? 1 : 0
    }

  }


}

# variables.tf

variable "apim_instance_name" {}

variable "apim_publisher_name" {}

variable "apim_publisher_email" {}

variable "apim_sku_name" {}

variable "tenant_id" {
  #  description "Enter Tenant ID"
}

variable "client_id" {
  #  description "Enter Tenant ID"
}

variable "subscription_id" {
  #  description "Enter Subscription ID"
}

variable "client_secret" {
  #  description "Enter client secret"
}



variable "apim_resource_group_name" {
  #  description "RG-2"
}

variable "apim_location" {
  type = map(any)
  default = {
    location1 = "eastus"
    location2 = "westus"
  }
}

#variable "subnets" {
#  type = "list"
#  default = ["10.0.1.0/24", "10.0.2.0/24"]
#}


variable "test_condition" {
  type    = bool
  default = true
}

variable "custom_block" {
  default = null
}



从上面的 terraform 代码中,我想避免/跳过资源的以下(第二个代理块)部分被配置

    proxy {
      default_ssl_binding          = false
      host_name                    = "ftech"
      key_vault_id                 = "https://ssl-key-test789.vault.azure.net/secrets/my-ssl-certificate2"
      negotiate_client_certificate = true
#      custom                       = var.custom_block
#      count                       = var.test_condition ? 1 : 0
    }

我确实尝试使用计数逻辑来避免,但我猜它会在完整的资源块上工作,而不是在资源块的某个部分。无论如何,我使用计数逻辑收到以下错误

Error: Unsupported argument
│ 
│   on apim-instance.tf line 35, in resource "azurerm_api_management" "apim_demo":
│   35:       count                       = var.test_condition ? 1 : 0
│ 
│ An argument named "count" is not expected here.
╵

我也尝试使用空逻辑来避免,但我想它也适用于完整的资源块,而不是资源块的某个部分。无论如何,我使用空逻辑得到了以下错误。

│ Error: Unsupported argument
│ 
│   on apim-instance.tf line 34, in resource "azurerm_api_management" "apim_demo":
│   34:       custom                       = var.custom_block
│ 
│ An argument named "custom" is not expected here.
╵


【问题讨论】:

  • 我们需要在这里查看不同的信息来提供帮助。请提供您正在迭代的输入变量以及您希望“跳过”的内容。
  • 对不起,如果这有点不清楚。我只是希望在我们运行 terraform apply 时使用某些逻辑(目前不知道该使用什么)跳过(未配置)主机配置的第二个代理块。
  • 好的;当您获得有关您输入的信息时,请更新问题,我们可以提供答案。

标签: terraform azure-rm


【解决方案1】:

使用动态块。它将解决您的查询。

https://www.terraform.io/docs/language/expressions/dynamic-blocks.html

variable "proxy" {
  type        = any
  default     = [
    {
      default_ssl_binding          = true
      host_name                    = "qtech"
      key_vault_id                 = "https://ssl-key-test789.vault.azure.net/secrets/my-ssl-certificate"
      negotiate_client_certificate = true
    }
    {
     default_ssl_binding           = true
      host_name                    = "qtech"
      key_vault_id                 = "https://ssl-key-test789.vault.azure.net/secrets/my-ssl-certificate"
      negotiate_client_certificate = true
    }
  ]

}

像下面这样使用。

hostname_configuration {
 dynamic "proxy" {
    for_each = var.proxy
    content {
      default_ssl_binding          = proxy.value.default_ssl_binding
      host_name                    = proxy.value.host_name
      key_vault_id                 = proxy.value.key_vault_id
      negotiate_client_certificate = proxy.value.negotiate_client_certificate
    }
  }
}

【讨论】:

  • 您的代码与我的代码非常相似(提供两个代理块)。实际上,我有三个环境(dev、staging n prod)。我不想在 staging & prod 环境中提供第二个代理块,而只在 dev 环境中提供。那么如何确保只配置了第一个代理块而没有配置第二个代理块呢?
  • 您可以使用动态块逻辑并将其与 terraform 命名空间相结合,将 prod、dev 和 staging 分成 3 个 terraform 命名空间,用于创建一组代理,而其他则不这样做。
  • 是的,如果您不想使用命名空间,那么您可以使用 diff 变量文件,该文件将根据您的环境包含您的变量。
【解决方案2】:

如果您只是将此代码块分成每个阶段的模块怎么办?

【讨论】:

    猜你喜欢
    • 2021-12-21
    • 2019-11-17
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 2020-04-22
    • 2021-12-17
    • 2021-07-05
    • 2019-02-01
    相关资源
    最近更新 更多