azure - Terraform - AzureRM - Calling a Module in a For_each Loop and returning and using a set of Resource Ids - Stack Overflow

admin2025-04-17  3

I have a for_each loop that calls a module that creates a 'synapse sql pool'...

module "create_synapse_pools" {
  depends_on                 = [module.create_synapse]
  source                     = "../../resource_modules/synapse_pool"
  for_each                   = { for obj in var.synapse_pools : obj.name => obj }
  name                       = each.value.name
  synapse_pool_sku_name      = each.value.sku_name
...
}

Within the module being called I try and return the 'ids' of the synapse pools...

resource "azurerm_synapse_sql_pool" "synapse_sql_pool" {
  name                      = var.name
  sku_name                  = var.synapse_pool_sku_name
  ....
}

output "synapse_sql_pool_ids" {
  value = values(azurerm_synapse_sql_pool.synapse_sql_pool).*.id
}

I am looking to use the new created the set of 'synapse pool resource ids' and loop through them...

module "grant_rbac_on_pools_to_automation_account" {
  source       = "../../resource_modules/role_assignment"
  for_each     = module.create_synapse_pools.synapse_sql_pool_ids
   ...
}

I am getting the below errors...

Error: Unsupported attribute │ │ on ../../resource_modules/synapse_pool/main.tf line 59, in output "synapse_sql_pool_ids": │ 59: value = values(azurerm_synapse_sql_pool.synapse_sql_pool).*.id │ │ Can't access attributes on a primitive-typed value (string). ╵ ╷ │ Error:

Missing map element │ │ on ../../resource_modules/synapse_pool/main.tf line 59, in output "synapse_sql_pool_ids": │ 59: value = values(azurerm_synapse_sql_pool.synapse_sql_pool).*.id │ │ This map does not have an element with the key "id".

I have looked at the below but still can't figure out my issue...

/

Get outputs instance_id from loop another module terraform

modules + output from for_each

How to get output for for_each loop condition within terraform?

I have a for_each loop that calls a module that creates a 'synapse sql pool'...

module "create_synapse_pools" {
  depends_on                 = [module.create_synapse]
  source                     = "../../resource_modules/synapse_pool"
  for_each                   = { for obj in var.synapse_pools : obj.name => obj }
  name                       = each.value.name
  synapse_pool_sku_name      = each.value.sku_name
...
}

Within the module being called I try and return the 'ids' of the synapse pools...

resource "azurerm_synapse_sql_pool" "synapse_sql_pool" {
  name                      = var.name
  sku_name                  = var.synapse_pool_sku_name
  ....
}

output "synapse_sql_pool_ids" {
  value = values(azurerm_synapse_sql_pool.synapse_sql_pool).*.id
}

I am looking to use the new created the set of 'synapse pool resource ids' and loop through them...

module "grant_rbac_on_pools_to_automation_account" {
  source       = "../../resource_modules/role_assignment"
  for_each     = module.create_synapse_pools.synapse_sql_pool_ids
   ...
}

I am getting the below errors...

Error: Unsupported attribute │ │ on ../../resource_modules/synapse_pool/main.tf line 59, in output "synapse_sql_pool_ids": │ 59: value = values(azurerm_synapse_sql_pool.synapse_sql_pool).*.id │ │ Can't access attributes on a primitive-typed value (string). ╵ ╷ │ Error:

Missing map element │ │ on ../../resource_modules/synapse_pool/main.tf line 59, in output "synapse_sql_pool_ids": │ 59: value = values(azurerm_synapse_sql_pool.synapse_sql_pool).*.id │ │ This map does not have an element with the key "id".

I have looked at the below but still can't figure out my issue...

https://www.reddit.com/r/Terraform/comments/jyz0dm/working_with_for_each_for_outputs_in_modules/

Get outputs instance_id from loop another module terraform

modules + output from for_each

How to get output for for_each loop condition within terraform?

Share Improve this question edited Feb 3 at 11:20 Vinay B 2,8242 gold badges3 silver badges12 bronze badges Recognized by Microsoft Azure Collective asked Jan 31 at 19:52 Ian CarrickIan Carrick 3861 gold badge6 silver badges25 bronze badges 2
  • Have you considered a for expression with azurerm_synapse_sql_pool.synapse_sql_pool in the output value instead? It would be more cumbersome, but also much easier. – Matthew Schuchard Commented Jan 31 at 21:03
  • In fact your linked question "modules + output from for_each" answers what is essentially the same question with the same solution I suggest in the above comment. I would really recommend using that path forward. – Matthew Schuchard Commented Jan 31 at 21:32
Add a comment  | 

1 Answer 1

Reset to default 0

Calling a Module in a For_each Loop and returning and using a set of Resource Ids using terraform

Blocker seems to be the way you refer the output using values which do not suit for this requirment and also id attribute cannot be accessed because the resource is not being treated as a list.

I tried a configuration below that matches the requirement that refers the output from the module in a correct way.

updated configuration:

main.tf

resource "azurerm_synapse_workspace" "synapse_workspace" {
  name                = "vksbsypace"
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_resource_group.rg.location

  storage_data_lake_gen2_filesystem_id = azurerm_storage_data_lake_gen2_filesystem.example.id

  identity {
    type = "SystemAssigned"
  }
}


module "create_synapse_pools" {
  depends_on             = [azurerm_synapse_workspace.synapse_workspace]
  source                 = "./modules/synapse_pool"
  for_each               = { for obj in var.synapse_pools : obj.name => obj }
  name                   = each.value.name
  synapse_pool_sku_name  = each.value.sku_name
  synapse_workspace_id   = azurerm_synapse_workspace.synapse_workspace.id
}

module "grant_rbac_on_pools_to_automation_account" {
  source  = "./modules/role_assignment"
  for_each = module.create_synapse_pools
  synapse_pool_id      = each.value.synapse_sql_pool_id
  role_definition_name = "Contributor"
  principal_id         = var.automation_account_principal_id
}

output "synapse_sql_pool_ids" {
  value = { for k, v in module.create_synapse_pools : k => v.synapse_sql_pool_id }
}

modules/synapse_pool

resource "azurerm_synapse_sql_pool" "synapse_sql_pool" {
  name                = var.name
  synapse_workspace_id = var.synapse_workspace_id
  sku_name            = var.synapse_pool_sku_name
  storage_account_type = "GRS"
}

output "synapse_sql_pool_id" {
  value = azurerm_synapse_sql_pool.synapse_sql_pool.id
}

modules/roleassignment:

resource "azurerm_role_assignment" "role_assignment" {
  scope                = var.synapse_pool_id
  role_definition_name = var.role_definition_name
  principal_id         = var.principal_id
}

output "role_assignment_id" {
  value = azurerm_role_assignment.role_assignment.id
}

Deployement:

Refer:

https://developer.hashicorp.com/terraform/language/modules/develop

https://discuss.hashicorp.com/t/missing-attribute-value-when-creating-map-of-maps/23989

Terraform missing map element answered by Grzegorz Oledzki

转载请注明原文地址:http://anycun.com/QandA/1744848759a88470.html