最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

azure - Generate resources dynamically from another group of dynamically generated resources in Terraform - Stack Overflow

programmeradmin1浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 1

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

发布评论

评论列表(0)

  1. 暂无评论