You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Terraform创建Azure CosmosDB Serverless实例?

如何用Terraform创建Azure CosmosDB无服务器版本?

你的代码创建的是常规预配吞吐量版本的CosmosDB,核心原因是缺少开启无服务器模式的配置块,同时部分参数不符合无服务器的限制要求。以下是修正后的配置及关键说明:

修正后的完整代码

resource "azurerm_cosmosdb_account" "resume-challenge-cosmosdb" {
  name                      = var.cosmosdb-name
  location                  = var.region
  resource_group_name       = azurerm_resource_group.cloud-resume-rg.name
  offer_type                = "Standard"
  kind                      = "GlobalDocumentDB"
  enable_automatic_failover = false
  enable_free_tier          = false # 无服务器模式不支持免费层
  # 开启无服务器核心配置
  serverless_capabilities {
    enabled = true
  }
  consistency_policy {
    consistency_level       = "BoundedStaleness"
    max_interval_in_seconds = 300
    max_staleness_prefix    = 100000
  }
  geo_location {
    location          = var.region # 无服务器仅支持单区域部署,需与账户区域一致
    failover_priority = 0
  }
}

关键修改点说明

  • 添加serverless_capabilities块:这是启用无服务器模式的必要配置,enabled = true会告诉Azure创建无服务器类型的CosmosDB账户。
  • 关闭enable_free_tier:Azure不允许免费层与无服务器模式同时启用,必须二选一。
  • 单区域部署:无服务器CosmosDB仅支持单区域,因此geo_location只能配置一个区域,且需与账户的location保持一致。
  • 保持enable_automatic_failover = false:无服务器模式不支持自动故障转移,该参数必须设为false。

额外注意事项

  • 确保你的Terraform AzureRM Provider版本在3.0及以上,serverless_capabilities特性是较新加入的,旧版本可能不支持。
  • 无服务器模式下无需配置预配吞吐量(throughput),计费按实际使用量计算。

内容的提问来源于stack exchange,提问作者Pedro Ignacio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 09:02:55