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?
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
for
expression withazurerm_synapse_sql_pool.synapse_sql_pool
in theoutput
value
instead? It would be more cumbersome, but also much easier. – Matthew Schuchard Commented Jan 31 at 21:03