Search code examples
terraformip-addresscidr

CIDR to IP range in order to use in a firewall definition, that does not understand CIDRs (like azurerm_sql_firewall_rule)?


I have a bunch of CIDRs which I need to feed to azurerm_sql_firewall_rule. Unfortunately, the latter needs <start IP, end IP> pair.

Is there a way to do it in terraform?


Solution

  • Figured it out:

    locals {
      cidr = "10.12.127.0/20"
      bits = 32 - split("/", local.cidr)[1]
      x = cidrhost(local.cidr, 0)
      y = cidrhost(local.cidr, pow(2, local.bits) - 1)
    }
    
    output "x" {
        value = local.x
    }
    
    output "y" {
        value = local.y
    }
    

    Running it:

    Outputs:
    
    x = 10.12.112.0
    y = 10.12.127.255