Sorin Ionescu
Sorin Ionescu

Reputation: 13

Azure - restore CosmosDB account using Terraform

I am trying to create a new Azure CosmosDB account in terraform account using:

create_mode = "Restore"

Basically I am trying to restore from an existing DB, and the code needs another input attribute, of the source DB:

"source_cosmosdb_account_id" = "/subscriptions/33f91226-e87e-4cdf67a1dae4e/providers/Microsoft.DocumentDB/locations/westeu/restorableDatabaseAccounts/test-source-db-name"

I am following the format indicated by the docs:

The example is /subscriptions/{subscriptionId}/providers/Microsoft.DocumentDB/locations/{location}/restorableDatabaseAccounts/{restorableDatabaseAccountName}

However when I apply the code, I get the following error:

Code="BadRequest" Message="Failed to parse uri /subscriptions/33f91226-e87e-4ca1dae4e/providers/Microsoft.DocumentDB/locations/westeu/restorableDatabaseAccounts/test-source-db-name

The issue seems to be the way I write the location inside the source ID, but I can't find any relevant info on how is the correct way.

I would really appreciate an example of source_cosmosdb_account_id if anyone did this successfully in terraform.

Thanks

Configuration used:

  backup = [
    {
      type                = "Continuous"
      interval_in_minutes = null
      retention_in_hours  = null
      storage_redundancy  = null
    }
  ]
  restore   = [
    {
      "source_cosmosdb_account_id" = "/subscriptions/33f6-e87e-4cdf-9480-7b1dae/providers/Microsoft.DocumentDB/locations/westeu/restorableDatabaseAccounts/test-source-db-name"
      "restore_timestamp_in_utc" = "2022-11-18T14:00:00.00Z"
      "database" = []
    }
  ]

Upvotes: 1

Views: 1049

Answers (2)

Kqly
Kqly

Reputation: 146

I've recently faced same issue and discovered that terraform documentation is outdated.

You need to have create_mode = "Restore"

Backup should be configured like this

  backup = [
    {
      type                = "Continuous"
      interval_in_minutes = 0
      retention_in_hours  = 0
      storage_redundancy  = "Geo"
    }
  ]

Format /subscriptions/{subscriptionId}/providers/Microsoft.DocumentDB/locations/{location}/restorableDatabaseAccounts/{restorableDatabaseAccountName} is not correct

Try with /subscriptions/{subscriptionId}/providers/Microsoft.DocumentDB/locations/{location}/restorableDatabaseAccounts/{**instanceId**}

You can find cosmos instanceId in json view enter image description here

Upvotes: 0

kavya Saraboju
kavya Saraboju

Reputation: 10859

I tried to reproduce the issue in my environment. I got the same error , Failed to parse uri /subscriptions/xxxx/providers/Microsoft.DocumentDB/locations/westeu/restorableDatabaseAccounts/test-source-db-name

with:

resource "azurerm_cosmosdb_account" "db" {
  name                = "tfex-cosmos-db-31960"
  location            = "westus2"
  resource_group_name = data.azurerm_resource_group.example.name
  offer_type          = "Standard"
  kind                = "MongoDB"
 create_mode         =  "Restore"
 

restore {    source_cosmosdb_account_id=data.azurerm_cosmosdb_restorable_database_accounts.example.id
  source_cosmosdb_account_id="/subscriptions/xxxxx/providers/Microsoft.DocumentDB/locations/westeurope/restorableDatabaseAccounts/tfex-cosmos-db-31960?api-version=2022-05-15"
  restore_timestamp_in_utc="2022-11-25T22:06:00Z"
  
}
   

...
}

enter image description here

If I tried with westeu as the location you gave , in my case I am getting below error , as it must be westeurope as supported by azure .

enter image description here

Also please check

Note from cosmosdb_account | terraform registry: Database account with Continuous type (or deleted account in last 30 days) are the restorable accounts and there cannot be Create/Update/Delete operations on the restorable database accounts. They can only be read and be retrieved by azurerm_cosmosdb_restorable_database_accounts.

With azurerm provider , i can only read it through below with continuous backup type :

resource "azurerm_cosmosdb_account" "db" {
  name                = "tfex-cosmos-db-31960"
  location            = "westus2"
  resource_group_name = data.azurerm_resource_group.example.name
  offer_type          = "Standard"
  kind                = "MongoDB"
 

  enable_automatic_failover = true
  

  capabilities {
    name = "EnableAggregationPipeline"
  }

  capabilities {
    name = "mongoEnableDocLevelTTL"
  }

  capabilities {
    name = "MongoDBv3.4"
  }

  capabilities {
    name = "EnableMongo"
  }

  consistency_policy {
    consistency_level       = "BoundedStaleness"
    max_interval_in_seconds = 300
    max_staleness_prefix    = 100000
  }

  geo_location {
    location          = "eastus"
    failover_priority = 0
  }



backup{

type = "Continuous"
//interval_in_minutes=60

}

data "azurerm_cosmosdb_restorable_database_accounts" "example" {
  name     = azurerm_cosmosdb_account.db.name
  location = "West Europe"
}

output "name" {
  value=data.azurerm_cosmosdb_restorable_database_accounts.example.name
  
}
output "id" {
  value = data.azurerm_cosmosdb_restorable_database_accounts.example.id
}

Output:

enter image description here

Try checking the same with azapi_resource block in terraform :

as it requires parameters like api version , source account id which has to come from cosmosdb_restorable_database_accounts list and must form uri like https://management.azure.com/subscriptions/subid/providers/Microsoft.DocumentDB/locations/West US/restorableDatabaseAccounts/d9b2xxx10d?api-version=2022-05-15 which is compatible in api resource.

Upvotes: 0

Related Questions