terraform-provider-azurerm
terraform-provider-azurerm copied to clipboard
Feature Request: Support for hierarchical partition keys in Azure Cosmos DB containers
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
Did we get any update on this? Was this support added?
Looking for this feature, as azure now supports it and dev teams are trying to implement the same.
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.
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:
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.
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
}
}
}
})
}
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, 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.
One more vote to fix this issue! It is blocking for our team.
This would be super useful. I know @binarygroot provided a work around, but it would be very nice if this wasn't required.
Would love to see this issue fixed! It would simplify our Terraform immensely
Just to chime in that I would also love to see this supported in the provider 🙏
Would love to see support as well. Thank you.
Our team would love to see this supported aswell
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.