使用Terraform创建Azure Cosmos DB SQL数据库失败(405错误)求助
问题分析与解决方案
错误根源
错误提示Requests for API sql are not supported for this account明确说明:你创建的Cosmos DB账户不支持SQL API操作。这是因为你的Cosmos DB账户配置仅启用了Table API,缺少SQL API的支持。
具体问题点
kind参数配置错误:如果var.cosmosdb_kind的值设置为Table,则账户仅支持Table API,无法创建SQL数据库。SQL API对应的kind值应为GlobalDocumentDB。- Capabilities配置缺失:即使
kind设置正确,若仅添加了EnableTable的capability,未启用SQL API(默认GlobalDocumentDB已包含SQL,但多API场景需显式配置),也会导致该问题。 - (额外提示)你的SQL数据库资源名称存在拼写错误:
comosdbsqldb应为cosmosdbsqldb,虽不是本次错误原因,但建议修正以避免后续混淆。
修复步骤
1. 修正Cosmos DB账户配置
修改azurerm_cosmosdb_account资源的配置,确保支持SQL API:
resource "azurerm_cosmosdb_account" "cosmosaccount" { name = "cosmosdb" location = var.location resource_group_name = var.rg_name offer_type = var.cosmosdb_offer_type # 明确指定SQL API对应的kind值 kind = "GlobalDocumentDB" is_virtual_network_filter_enabled = true ip_range_filter = var.ip_range_filter # 若需同时支持Table和SQL API,保留两个capability;仅需SQL可删除EnableTable capabilities { name = "EnableTable" } capabilities { name = "EnableSql" } enable_automatic_failover = false consistency_policy { consistency_level = var.cosmosdb_consistancy_level max_interval_in_seconds = 5 max_staleness_prefix = 100 } geo_location { location = var.location failover_priority = 0 } virtual_network_rule { id = var.subnet_id ignore_missing_vnet_service_endpoint = true } }
2. 重建Cosmos DB账户
由于Cosmos DB账户的kind和capabilities属性无法在创建后修改,必须先销毁现有账户,再重新部署:
# 销毁现有Cosmos DB账户 terraform destroy -target=azurerm_cosmosdb_account.cosmosaccount # 重新部署所有资源 terraform apply
3. 验证配置
部署完成后,可在Azure门户中检查Cosmos DB账户的API设置,确认已包含SQL API,此时即可正常创建SQL数据库及容器。
内容的提问来源于stack exchange,提问作者asp
相关产品推荐
相关产品推荐

