terraform-provider-azurerm icon indicating copy to clipboard operation
terraform-provider-azurerm copied to clipboard

Feature Request: Support for hierarchical partition keys in Azure Cosmos DB containers

Open Jordan-Murray opened this issue 1 year ago • 13 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Community Note

  • Please vote on this issue by adding a :thumbsup: reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Description

As a Terraform user, I'd like to be able to create Azure Cosmos DB containers with hierarchical partition keys. Currently, Terraform only supports creating containers with a single partition key path. Supporting hierarchical partition keys would improve usability and performance when working with complex data models that require partitioning based on multiple attributes.

New or Affected Resource(s)/Data Source(s)

azurerm_cosmosdb_sql_container

Potential Terraform Configuration

resource "azurerm_cosmosdb_sql_container" "example" {
  name                = "example-container"
  resource_group_name = azurerm_resource_group.example.name
  account_name        = azurerm_cosmosdb_account.example.name
  database_name       = azurerm_cosmosdb_sql_database.example.name

  partition_key_paths = ["/category", "/subcategory"]
  throughput          = 400
}

References

https://learn.microsoft.com/en-us/azure/cosmos-db/hierarchical-partition-keys

Jordan-Murray avatar May 10 '23 14:05 Jordan-Murray

Did we get any update on this? Was this support added?

hd40910 avatar Aug 03 '23 03:08 hd40910

Looking for this feature, as azure now supports it and dev teams are trying to implement the same.

paul8989 avatar Aug 04 '23 07:08 paul8989

Definitely important for my team also. We have a fairly large CosmosDB instance and we require IAC for our environment. Really need the ability to do this either through CLI or Terraform so we can deploy a new version of our DB.

sddev-dotnet avatar Aug 06 '23 17:08 sddev-dotnet

We're looking forward to this feature, too.

Meanwhile we decided to deploy via Terraform everything we can(CosmosDB account, database, "regular" containers) and utilize ARM template to deploy containers with multiple partition keys using azurerm_resource_group_template_deployment, something like:

resource "azurerm_resource_group_template_deployment" "multihash_containers_template_deployment" {
  name                = "${local.name_prefix}-arm"
  resource_group_name = data.azurerm_resource_group.resource_group.name
  deployment_mode     = "Incremental"

  template_content = file("arm/cosmosdb-multihash-containers.json")

  parameters_content = jsonencode({
    "cosmosdb_account_name" : {
      "value" : azurerm_cosmosdb_account.cosmosdb_account.name
    },
    "cosmosdb_database_name" : {
      "value" : azurerm_cosmosdb_sql_database.cosmosdb.name
    }
  })

  depends_on = [
    azurerm_cosmosdb_sql_database.cosmosdb
  ]
}

To get ARM template content you can run "Export template" action for your CosmosDB in Azure Portal and copy the part with container you need. Let's say we have only one 'products' container with multiple partition keys, so cosmosdb-multihash-containers.json file will look like this:

image

This way Terraform takes care about everything as far as possible. The only thing we'll need to do is Terraform import command for this container once multiple partition support is available.

Hope this may come in handy.

binarygroot avatar Aug 08 '23 19:08 binarygroot

It seems we can deploy with azapi

resource "azapi_resource" "sql_container" {

  for_each = var.cdb_containers

  type      = "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers@2023-04-15"
  name      = each.key
  location  = var.cdb_location
  parent_id = azurerm_cosmosdb_sql_database.sql_database["${each.value.db_name}"].id

  body = jsonencode({
    properties = {
      resource = {
        id                   = each.key
        analyticalStorageTtl = var.cdb_analytical_storage_enabled ? each.value.analytical_storage_ttl : null
        partitionKey = {
          kind    = each.value.partition_key_kind
          paths   = each.value.partition_key_path
          version = each.value.partition_key_version
        }
      }
    }
  })
}

websolut avatar Aug 25 '23 08:08 websolut

https://github.com/hashicorp/terraform-provider-azurerm/blob/main/internal/services/cosmos/cosmosdb_sql_container_resource.go#L182

This may be possible by setting the partition key version =2, and sending the partition key paths in as a string array to the partionKeyPaths property.

liamgib avatar Nov 12 '23 23:11 liamgib

@liamgib, assuming I'm understanding you correctly, it doesn't work:

main.tf partition_key_path = "/FilePath,/CompanyId,/id"

terraform plan

+ partition_key_path    = "/FilePath,/CompanyId,/id"
...
Plan: 2 to add, 0 to change, 0 to destroy.

terraform apply

{\\\"Errors\\\":[\\\"The partition key component definition path '\\\\/FilePath,\\\\/CompanyId,\\\\/id' could not be accepted, failed near position '9'. Partition key paths must contain only valid characters and not contain a trailing slash or wildcard character.\\\"]}

Variants like `"["/FilePath","/CompanyId","/id"]",etc either end up executed with jsonencode or the same error from escaping.

1stewart avatar Nov 19 '23 17:11 1stewart

One more vote to fix this issue! It is blocking for our team.

GKrivosheev-rms avatar Dec 07 '23 17:12 GKrivosheev-rms

This would be super useful. I know @binarygroot provided a work around, but it would be very nice if this wasn't required.

aaronS7 avatar Dec 08 '23 20:12 aaronS7

Would love to see this issue fixed! It would simplify our Terraform immensely

BlaineTaylor avatar Dec 20 '23 15:12 BlaineTaylor

Just to chime in that I would also love to see this supported in the provider 🙏

RockyMM avatar Mar 27 '24 10:03 RockyMM

Would love to see support as well. Thank you.

PrestonR avatar Apr 04 '24 23:04 PrestonR

Our team would love to see this supported aswell

Mjinx avatar May 10 '24 08:05 Mjinx

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

github-actions[bot] avatar Jul 22 '24 02:07 github-actions[bot]