0

I am using the "Hetzner" provider with Terraform.
My plan is to have something like this in fx .auto.tfvars :

fw_openings = [
   {
      port = "22",
      protocol = "tcp",
      subnet = "100.100.100.100/32"
   },
   {
      port = "80",
      protocol = "tcp",
      subnet = "0.0.0.0/0"
   }
]

Based on the fw_openings I would like to generate something like this:

resource "hcloud_firewall" "firewall" {
   rule {
      direction = "in"
      protocol = "tcp"
      port = "22"
      source_ips = [
         "100.100.100.100/32"
      ]
   }
   rule {
      direction = "in"
      protocol = "tcp"
      port = "80"
      source_ips = [
         "0.0.0.0/0"
      ]
   }
}

I'm sure it's possible (since it seems very trivial). But I seem to keep tripping over the looping options with Terraform.
What would be the proper solution?

Marcin
  • 215,873
  • 14
  • 235
  • 294
ZiGGi
  • 3
  • 1
  • 2

1 Answers1

2

You can use dynamic blocks:

resource "hcloud_firewall" "firewall" {
   dynamic "rule" {
   
      for_each = var.fw_openings
   
      content {
            direction = "in"
            protocol = rule.value.protocol
            port = rule.value.port
            source_ips = [
                rule.value.subnet
            ]
        }
   }
}
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • @ZiGGi No problem. If the answer helped, its [acceptance](https://meta.stackexchange.com/a/86979) would be appreciated. – Marcin Mar 03 '22 at 08:01
  • 1
    I think I got carried away and completely forgot to respond. I'm sorry for that! Especially since it worked like a charm – ZiGGi Apr 11 '22 at 14:19