如何在 azurerm_app_service 资源块中指定 4 个连接字符串

How to specify 4 connection strings in azurerm_app_service resource block

我在我们的企业中定义了一个模块,用于创建应用程序服务计划以及 Azure Web 应用程序。但现在我想使用 link 中提到的“azurerm_app_service”资源块:https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/app_service 在我们的模块中,连接字符串在参数下定义:

app_settings = {
   AzureAd__ClientSecret            = <Connection String of the App SP stored in Azure KV>
   DbConnection__ConnectionString   = <Azure SQL DB Connection String stored in Azure KV>
   CosmosDb__Account                = <Connection String of the Cosmos DB Account stored in Azure KV>
   CosmosDb__Key                    = <Connection String of the Cosmos DB Account Key stored in Azure KV>
}

现在,根据上面的 URL,在“azurerm_app_service”的资源块中有一个名为 connection_string 的参数,如 URL 所示:

connection_string {
    name  = "Database"
    type  = "SQLServer"
    value = "Server=some-server.mydomain.com;Integrated Security=SSPI"
  }

所以我想知道如何根据“connection_string”参数在资源块中定义我的 4 个连接字符串,以及我应该为每个连接字符串选择哪些类型? 如果我继续定义我的连接字符串,因为它们现在在“app_settings”下的模块中,那会没问题吗?或者如果我在新的资源块结构中这样做会有问题吗?

正在就此寻求帮助

根据您的要求,您可以使用dynamic 块在azurerm_app_service 资源块中定义多个connection_string。此处为您提供的示例代码:

resource "azurerm_app_service" "webapp" {
  ...

  dynamic "connection_string" {
    for_each = var.connection_strings
    content {
      name = each.value.name
      type = each.value.type
      value = each.value.value
    }
  }

  ...
}

所以你看,你最好用一个变量来配置connection_string的所有必要的东西,然后在动态块中使用它。