Search code examples
azureterraformazure-cosmosdbterraform-provider-azureazure-rm

How can I create an Azure CosmosDB Serverless using Terraform?


I've been trying to create the serverless offer of the cosmosdb database using Terraform, but haven't been able to do so.

After reading Microsoft's documentations and the Azurerm terraform registry documentations, I could code this resource:

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          = true
  consistency_policy {
    consistency_level       = "BoundedStaleness"
    max_interval_in_seconds = 300
    max_staleness_prefix    = 100000
  }
  geo_location {
    location          = "brazilsouth"
    failover_priority = 0
  }
}

but it creates the regular version of the cosmosdb.


Solution

  • To make a CosmosDB account serverless using the AzureRM Terraform provider you need to enable the EnableServerless capability on the azurerm_cosmosdb_account. To do this you must add a capabilities block with name = "EnableServerless". Applying this to your above example would look like so:

    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          = true
      consistency_policy {
        consistency_level       = "BoundedStaleness"
        max_interval_in_seconds = 300
        max_staleness_prefix    = 100000
      }
      geo_location {
        location          = "brazilsouth"
        failover_priority = 0
      }
      capabilities {
        name = "EnableServerless"
      }
    }
    

    I found this by searching for the word "serverless" on the azurerm_cosmosdb_account resource docs.