asp
asp

Reputation: 835

Issue in creating sqldb in a Cosmos DB account on Azure with Terraform

Objective: Creating a Cosmos DB account and sqldb in Azure with Terraform

What I tried:

resource "azurerm_cosmosdb_account" "cosmosaccount" {
  name                = "cosmosdb"
  location            = var.location
  resource_group_name = var.rg_name
  offer_type          = var.cosmosdb_offer_type
  kind                = var.cosmosdb_kind
  is_virtual_network_filter_enabled = "true"
  ip_range_filter     = var.ip_range_filter

  capabilities {
    name = "EnableTable"
  }

  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
  }
}


resource "azurerm_cosmosdb_sql_database" "comosdbsqldb" {
  name                = "driving"
  resource_group_name = azurerm_cosmosdb_account.cosmosaccount.resource_group_name
  account_name        = azurerm_cosmosdb_account.cosmosaccount.name
  throughput          = 500
  depends_on = [azurerm_cosmosdb_account.cosmosaccount]
}


resource "azurerm_cosmosdb_sql_container" "mdhistcontainer" {
  name                  = "metadata_history"
  resource_group_name   = azurerm_cosmosdb_account.cosmosaccount.resource_group_name
  account_name          = azurerm_cosmosdb_account.cosmosaccount.name
  database_name         = azurerm_cosmosdb_sql_database.comosdbsqldb.name
  partition_key_path    = "/definition/id"
  partition_key_version = 1
  throughput            = 500

  indexing_policy {
    indexing_mode = "consistent"

    included_path {
      path = "/*"
    }

    included_path {
      path = "/included/?"
    }

    excluded_path {
      path = "/excluded/?"
    }
  }

  unique_key {
    paths = ["/definition/idlong", "/definition/idshort"]
  }
  depends_on = [azurerm_cosmosdb_sql_database.comosdbsqldb]
}

The issue I am facing:

It created the Cosmos DB account successfully, but it is failing at creating sqldb

The error I am getting:

Error: checking for presence of Sql Database: (Name "driving" / Database Account Name "cosmosaccount" /
Resource Group "xxxxxxxxxxxx"): documentdb.SQLResourcesClient#GetSQLDatabase:
Failure responding to request: StatusCode=405 -- Original Error: autorest/azure: Service returned an error.
Status=405 Code="MethodNotAllowed" Message="Requests for API sql are not supported for this account.
\r\nActivityId: 2d79ca83-9534-46e8-a7cf-cef1fd76e752, Microsoft.Azure.Documents.Common/2.14.0"
│
│   with module.cosmosdb.azurerm_cosmosdb_sql_database.comosdbsqldb,
│   on ../modules/cosmosdb/main.tf line 46, in resource "azurerm_cosmosdb_sql_database" "comosdbsqldb":
│   46: resource "azurerm_cosmosdb_sql_database" "comosdbsqldb"

I have given DocumentDBContributor role also to service principal but still getting this error. I am following below documentation about syntax.. It seems I am exactly following azurerm_cosmosdb_sql_database.

How can I fix this error?

Upvotes: 0

Views: 1046

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136226

I believe you are getting this error is because you are creating a Cosmos DB account targeting Table API and trying to create a database with custom name in that account.

  capabilities {
    name = "EnableTable"
  }

With Table API, you cannot create a database with any name. There can only be a single database in the account and its name is fixed. The name of the database should be TablesDB.

Upvotes: 3

Related Questions