azureterraformhashicorp

Creating azurerm_postgresql_flexible_server_firewall_rule with for_each


Trying for 2 days create firewall rule but cant find the mistake.

I step down step by step and now using an example from hashicorp which also not works.

resource "azurerm_postgresql_flexible_server_firewall_rule" "vpn" {
  for_each = tomap({
    a = "112.234.12.1"
    b = "212.22.111.1",
    c = "22.111.22.1"
  })

  name             = "db-firewall-${each.key}"
  server_id        = azurerm_postgresql_flexible_server.mydb.id
  start_ip_address = each.value
  end_ip_address   = each.value
}

There is no firewall rule create by this code. Whats my bad?


Solution

  • Creating azurerm_postgresql_flexible_server_firewall_rule with for_each

    As per the 'tomap' function, here the 'for_each' meta-argument were passed as a map not as the set of strings so we need to remove the extra comma in the second line of 'tomap'.

    I tried a terraform configuration with reference to hashicorp registry with mentioned changes and I was able to provision the requirement successfully.

    My terraform configuration:

    resource "azurerm_postgresql_flexible_server" "mydb" {
      name                = "testvk-postgres-ser"
      resource_group_name = azurerm_resource_group.example.name
      location            = azurerm_resource_group.example.location
      version             = "12"
      administrator_login = "adminuser"
      administrator_password = "Password1234!"
      sku_name            = "GP_Standard_D4s_v3"
      storage_mb = 32768
      
    
      high_availability {
        mode = "SameZone"
      }
    }
    
    resource "azurerm_postgresql_flexible_server_firewall_rule" "vpn" {
      for_each = tomap({
        a = "112.234.12.1"
        b = "212.22.111.1"
        c = "22.111.22.1"
      })
    
      name             = "vkdb-firewall-${each.key}"
      server_id        = azurerm_postgresql_flexible_server.mydb.id
      start_ip_address = each.value
      end_ip_address   = each.value
    }
    

    Deployment succeeded:

    enter image description here

    enter image description here