Hello everybody!
I hope they’re all right.
I have a map variable that has a list inside it
Example:
my_map = {
map01 = {
name = my-name
localtion = my-location
group = {
group_id = ["group-x", "group-y"]
properties = {
x = "X"
y = "Y"
}
}
}
variable "my_map" {
type = map(any)
default = {}
}
I need get list group_id to get, to this I using locals to identify grou variable for use in the data azurerm_monitor_action_group .
To this I trying use a locals, but I can’t filter group in map.
locals {
name = flatten([
for k, v in my_map : [
for n, s in v :
[s]
]
])
}
As I need to create a loop I think so use for_each to this, but As I need to create a loop I think so use for_each to this, but I cant identify where I’m going wrong.
data "azurerm_monitor_action_group" "example" {
for_each = local.my_map
resource_group_name = "terraform-example-rg"
name = "tfex-actiongroup"
}
type or paste code here
If someone could me any sugestionI thank.
maxb
March 24, 2023, 9:04pm
2
Hi,
Personally, I’m unable to understand what you are asking here.
I recommend you attempt to make your question more understandable.
Hi @maxb ,
Sory by my confusion.
I’m working in a project and I have following variable
variable "my_map" {
type = map(any)
default = {}
}
my_map = {
map01 = {
name = my-name
localtion = my-location
group = {
group_id = ["group-x", "group-y"]
properties = {
x = "X"
y = "Y"
}
}
}
I need get the values into group variable group_id .
group_id = ["group-x", "group-y"]
I need pass to the resource azurerm_monitor_action_group , to get the ID’s of groups into list variable group_id.
To this I used locals variables:
locals {
name = flatten([
for k, v in my_map : [
for n, s in v :
[s]
]
])
}
But local variable It return this output:
local.name = {
map01 = {
group_id = [
"group-x",
"group-y"
]
}
}
I need to get this values (“group x”, “group-y”) and use to get the ID of azurerm_monitor_action_grou p
data "azurerm_monitor_action_group" "example" {
for_each = local.my_map
resource_group_name = "terraform-example-rg"
name = each.value["group_id"]
}
I hope I managed to explain my scenario better and I apologize again for the inconvenience.
I think this tutorial might be a good start point.
As for your specific data source issue, here’s a hint:
for_each = toset(var.my_map.map01.group.group_id)
Not saying this is the best possible approach but should work with a few other code changes.
Hi @macmiranda !
Thank very much, your tip work for me.
I would like shared my code, becouse it help someone.
main.tf
data "azurerm_monitor_action_group" "default" {
for_each = toset(var.my_map.map01.group.group_id)
resource_group_name = "my-rg"
name = each.value
}
variables.tf
variable "my_map" {
type = map(any)
default = {}
}
locals.tf
locals {
id = [ for k in data.azurerm_monitor_action_group.default : k.id ]
}
output.tf
output "id" {
description = "The name of the created namespace."
value = local.id
}
group.tf
module "group" {
source = "./module"
my_map = {
map01 = {
name = "my-name"
localtion = "my-location"
group = {
group_id = ["group-x", "group-y"]
properties = {
x = "X"
y = "Y"
}
}
}
}
}
output.tf
output "id" {
description = "Id of Azure `Resoure group Name`"
value = module.group.id
}