I have a map of objects in Terraform for a list of networks as follows:
networks = {
"network1" = {
subnet_names = ["subnet-1", "subnet-2", "subnet-3"]
}
"network2" = {
subnet_names = ["subnet-4", "subnet-5", "subnet-6"]
}
I want to create 6 network security groups (1 per subnet) as follows:
resource "azurerm_network_security_group" "example" {
for_each = toset(var.networks[*].subnet_names)
[...]
}
However when I plan with this code I get an error that var_networks doesn’t have an element called subnet_names. I have tried a couple of for loops as well but I think I may need to do something with a nested for loop somewhere. what I want to achieve is a list like this:
nsgs_to_create=["subnet-1", "subnet-2", "subnet-3", "subnet-4", "subnet-5", "subnet-6"]]
Which I can then use to create the NSGs per subnet.
Any suggestions?
Thanks.
Andrew.
2
Answers
This should not be too complicated to achieve. You should use the built-in
flatten
function which will flatten all the lists and return only one list:This returns (using
terraform console
):You can then use
toset
to make it work withfor_each
.[1] https://www.terraform.io/language/functions/flatten
If you want to solve it with a splat expression:
Explanation:
values
function to create an array, which look like this:flatten
for to build a simple array.Additionally, this solution works if you will have other attributes for a subnet, for example:
Output: