admin管理员组文章数量:1332728
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.
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.
Share Improve this question edited Nov 21, 2024 at 3:25 Venkat V 7,8532 gold badges4 silver badges15 bronze badges Recognized by Microsoft Azure Collective asked Nov 20, 2024 at 17:39 user17331277user17331277 375 bronze badges 1- you can try using a combination of for_each with a map or list to iterate over both databases and collections. Flatten the combinations using for in a local variable and loop over it @user17331277 – Vinay B Commented Nov 21, 2024 at 3:41
1 Answer
Reset to default 1Generate 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
本文标签:
版权声明:本文标题:azure - Generate resources dynamically from another group of dynamically generated resources in Terraform - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742340218a2456452.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论