azure - 如何使用 Terraform 输出 Cosmos DB 连接字符串

标签 azure azure-devops terraform

我正在使用 Terraform 在 Azure 中使用 Mongo API 构建 Cosmos DB,但不知道构建后如何返回或输出连接字符串。

我已经构建了多个 Cosmos DB,但找不到任何有关如何输出连接字符串的 Material 。我正在使用模块来构建调用我的 main.tf 和variable.tf 的资源。如果我知道要在 output.tf 文件中放入什么内容,我会将其放入其中,以便我的模块可以利用它。

模块{ 可变输入 }

我没有任何错误消息要发布,因为我不知道如何调用连接字符串。我确实解析了 Azure Provider,并发布了 Mongo DB Cosmos 部分的 JSON 图片。下面的 JSON 图片: ![来自 Azure TF 提供程序的 Mongo Cosmos DB JSON] /image/XNkfY.png

最佳答案

您需要从 CosmosDB Account 读取数据。它包含一个 connection_strings 数组。“应该看起来像这样:

// Look for this
resource "azurerm_cosmosdb_account" "cosdb"{
   ...
}

output "cosmosdb_connectionstrings" {
   value = azurerm_cosmosdb_account.cosdb.connection_strings
   sensitive   = true
}

您还可以使用字符串插值通过组合主键和端点来构建连接字符串。如果您不使用 terraform 管理帐户,这也适用。您可以使用 CosmosDB Data Source访问 key 。

data "azurerm_cosmosdb_account" "cosdb" {
  name                = "${var.cosmosdbname}"
  resource_group_name = "${var.cosmosdbresourcegroupname}"
}

output "cosmosdb_connectionstrings" {
   value = "AccountEndpoint=${data.azurerm_cosmosdb_account.cosdb.endpoint};AccountKey=${data.azurerm_cosmosdb_account.cosdb.primary_master_key};"
   sensitive   = true
}

关于azure - 如何使用 Terraform 输出 Cosmos DB 连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57937964/

相关文章:

Azure 服务总线替代方案

amazon-web-services - 确定性名称服务器地址

amazon-web-services - 如何在 Terraform 上使用 cloud-init 初始化实例

azure - 如何将管道构建代理连接到私有(private)工件注册表?

terraform - 缺少配置版本 Terraform Cloud

azure - 能否将 Azure Blob 容器设为 Azure 虚拟 Windows 计算机上的虚拟网络驱动器?

azure - 如何在 Visual Studio Online 持续集成中更新 Azure webApp 插槽配置?

powershell - 使用 Powershell RM 列出 Azure 应用服务的实例

azure-devops - Azure 多级管道 : conditionally skip one stage but not the next

linux - OpenShift 中的 vsts-agent