简体   繁体   中英

Adding multiple variable types in for_each in terraform?

I want to add multiple ip addresses to configure access restrictions in azure for an app service and also this should be accomplished only for a few environments, I'm using the dynamic block to achieve this, it was working as long as there was only one IP, but now I want to allow more ip addresses, is there a way to achieve this?

dynamic "ip_restriction" {
  for_each = var.ip_addresses
   content {
     ip_address = ip_restriction.value["ip_address"]
     priority   = ip_restriction.value["priority"]
     name       = ip_restriction.value["name"]
   }
 }

This works perfect but I want to add the environment restriction as well. something like

for_each = var.env == "dev" || var.env == "qa" ? [1] : []

This works perfect but I want to add the environment restriction as well.

You are really close, you just need to change this array: [1] to be the actual array of IP addresses, like so:

for_each = var.env == "dev" || var.env == "qa" ? var.ip_addresses : []

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM