Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 task done
Jordan-Murray opened this issue May 10, 2023 · 14 comments · Fixed by #26372
Closed
1 task done

Comments

@Jordan-Murray
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Community Note

  • Please vote on this issue by adding a 👍 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

@hd40910

This comment was marked as off-topic.

@paul8989

This comment was marked as off-topic.

@sddev-dotnet

This comment was marked as off-topic.

@binarygroot
Copy link

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.

@websolut
Copy link

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

@liamgib
Copy link

liamgib commented Nov 12, 2023

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.

@1stewart
Copy link

1stewart commented Nov 19, 2023

@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.

@GKrivosheev-rms

This comment was marked as off-topic.

@aaronS7

This comment was marked as off-topic.

@BlaineTaylor

This comment was marked as off-topic.

@RockyMM

This comment was marked as off-topic.

@PrestonR

This comment was marked as off-topic.

@Mjinx

This comment was marked as off-topic.

Copy link

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 github-actions bot locked as resolved and limited conversation to collaborators Jul 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet