Using Terraform (Don't have access to CDK), I create a group of resources (In this case, azurerm_cosmosdb_mongo_database's) from a count variable. For each of these resources, I want to create a group of 'inner' resources, based on another count variable (In this case, azurerm_cosmosdb_mongo_collection's). I cannot figure out how to create this nested group of resources. GPT and Gemini have been useless, and no amount of Google searches has yielded an answer yet.
variable "database_count" {
description = "Number of databases to build"
type = number
default = 2
}
variable "collection_count" {
description = "Number of collections to build per database"
type = number
default = 5
}
resource "azurerm_cosmosdb_mongo_database" "databases" {
account_name = azurerm_cosmosdb_account.some-account.name
resource_group_name = azurerm_cosmosdb_account.some-account.resource_group_name
count = var.database_count
name = format("database%d", count.index)
}
resource "azurerm_cosmosdb_mongo_collection" "collections" {
account_name = azurerm_cosmosdb_account.some-account.name
resource_group_name = azurerm_cosmosdb_account.some-account.resource_group_name
throughput = 10000
index {
keys = ["_id"]
unique = true
}
(psuedo code) foreach database in databases
create var.collection_count collections with the above common settings
name = format("collection%d", collection_count index)
database_name = (the database we are iterating on)
}
The result should be two databases, each with five collections.
I tried using both count and for_each, but TF doesn't allow both in one resource. I also tried using division to generate the index and only iterating over one collection, but I couldn't find a way to do math and then use the result as an index value / collection name piece, etc. Maybe there is a way with dynamic blocks? I am new to TF.
Generate resources dynamically from another group of dynamically generated resources in Terraform
Issue mainly due to the way you creating nested resources for each database and its collections. while using terraform we can use count and for_each individually but not simultaneously.
To ove come this try making changes by using a combination of for loops and flatten
to structure the data.
This combination definatly helps you achieving the requriement.
Configuration:
resource "azurerm_cosmosdb_mongo_database" "databases" {
count = var.database_count
name = format("database%d", count.index)
account_name = azurerm_cosmosdb_account.main.name
resource_group_name = azurerm_resource_group.main.name
}
locals {
collections = flatten([
for db_index in range(var.database_count) : [
for coll_index in range(var.collection_count) : {
db_name = format("database%d", db_index)
coll_name = format("collection%d", coll_index)
}
]
])
}
resource "azurerm_cosmosdb_mongo_collection" "collections" {
for_each = { for i, v in local.collections : i => v }
name = each.value.coll_name
database_name = each.value.db_name
account_name = azurerm_cosmosdb_account.main.name
resource_group_name = azurerm_resource_group.main.name
throughput = 400
index {
keys = ["_id"]
unique = true
}
}
Deployment:
Refer:
azurerm_cosmosdb_mongo_collection | Resources | hashicorp/azurerm | Terraform | Terraform Registry
Azure CosmosDB (DocumentDB) Mongo Collection - Examples and best practices | Shisho Dojo