terraform {
required_version = ">= 0.12, < 0.13"
backend "azurerm" {
resource_group_name = "tstate"
storage_account_name = "tstate9364"
container_name = "tstate"
key = "terraform.tfstate"
}
}
provider "azurerm" {
version = "=2.0.0"
features {}
}
resource "azurerm_resource_group" "state-demo-secure" {
name = "state-demo-resource-group"
location = "eastus"
}
resource "random_integer" "ri" {
min = 10000
max = 99999
}
resource "azurerm_cosmosdb_account" "example" {
name = "example-cosmosdb-account-${random_integer.ri.result}"
resource_group_name = "state-demo-resource-group"
location = azurerm_resource_group.state-demo-secure.location
offer_type = "Standard"
kind = "GlobalDocumentDB"
consistency_policy {
consistency_level = "BoundedStaleness"
max_interval_in_seconds = 400
max_staleness_prefix = 200000
}
geo_location {
location = var.failover_location
failover_priority = 1
}
geo_location {
prefix = "state-demo-cosmos-db-${random_integer.ri.result}-customid"
location = azurerm_resource_group.state-demo-secure.location
failover_priority = 0
}
}
resource "azurerm_cosmosdb_sql_database" "example" {
name = "example-cosmos-mongo-db"
resource_group_name = azurerm_cosmosdb_account.example.resource_group_name
account_name = azurerm_cosmosdb_account.example.name
throughput = 400
}
resource "azurerm_cosmosdb_mongo_database" "example" {
name = "sample-cosmos-mongo-db"
resource_group_name = azurerm_cosmosdb_account.example.resource_group_name
account_name = azurerm_cosmosdb_account.example.name
throughput = 400
}
resource "azurerm_cosmosdb_mongo_collection" "example" {
name = "sample-cosmos-mongo-db-colection"
resource_group_name = azurerm_cosmosdb_mongo_database.example.resource_group_name
account_name = azurerm_cosmosdb_mongo_database.example.name
database_name = azurerm_cosmosdb_mongo_database.example.name
default_ttl_seconds = "777"
shard_key = "uniqueKey"
throughput = 400
}
expected a mongo colection created collection in my account but got an console error:
azurerm_cosmosdb_mongo_database.example: Creating...
Error: Error checking for presence of creating Cosmos Mongo Database sample-cosmos-mongo-db (Account example-cosmosdb-account-32943): documentdb.DatabaseAccountsClient#GetMongoDBDatabase: Failure responding to request: StatusCode=405 -- Original Error: autorest/azure: Service returned an error. Status=405 Code="MethodNotAllowed" Message="Requests for API mongodb are not supported for this account.\r\nActivityId: 29a5aba4-8774-4daf-81d5-635b949fe65d, Microsoft.Azure.Documents.Common/2.9.2"
on main.tf line 65, in resource "azurerm_cosmosdb_mongo_database" "example":
65: resource "azurerm_cosmosdb_mongo_database" "example" {
Releasing state lock. This may take a few moments...
adding
capabilities {
name = "EnableMongo"
}
to azurerm_cosmosdb_account gives similar error.
only changing both kind and capabilities for azurerm_cosmosdb_account worked in the end .
documentation shall include all types of possible combinations that are valid !
working hcl:
resource "azurerm_cosmosdb_account" "example" {
name = "example-cosmosdb-account-${random_integer.ri.result}"
resource_group_name = "state-demo-resource-group"
location = azurerm_resource_group.state-demo-secure.location
offer_type = "Standard"
kind = "MongoDB"
capabilities {
name = "EnableMongo"
}
consistency_policy {
consistency_level = "BoundedStaleness"
max_interval_in_seconds = 400
max_staleness_prefix = 200000
}
geo_location {
location = var.failover_location
failover_priority = 1
}
geo_location {
prefix = "state-demo-cosmos-db-${random_integer.ri.result}-customid"
location = azurerm_resource_group.state-demo-secure.location
failover_priority = 0
}
}
resource "azurerm_cosmosdb_mongo_database" "example" {
name = "sample-cosmos-mongo-db"
resource_group_name = azurerm_cosmosdb_account.example.resource_group_name
account_name = azurerm_cosmosdb_account.example.name
throughput = 400
}
resource "azurerm_cosmosdb_mongo_collection" "example" {
name = "sample-cosmos-mongo-db"
resource_group_name = azurerm_cosmosdb_account.example.resource_group_name
account_name = azurerm_cosmosdb_account.example.name
database_name = azurerm_cosmosdb_mongo_database.example.name
default_ttl_seconds = "777"
shard_key = "uniqueKey"
throughput = 400
}
Most helpful comment
adding
to azurerm_cosmosdb_account gives similar error.
only changing both kind and capabilities for azurerm_cosmosdb_account worked in the end .
documentation shall include all types of possible combinations that are valid !
working hcl: