Terraform’s built-in functions for manipulating IP addresses (
cidrhost, etc) are oriented around CIDR prefixes rather than arbitrary ranges, because that is typically how network equipment (both physical and virtual) expects to be configured. Terraform doesn’t have any functions for working with arbitrary ranges of IP addresses.
The closest approach I can imagine to what you described would be to use the
cidrhost function with a network prefix and a range of host numbers under that prefix, perhaps something like this:
cidr_block = "192.168.1.0/24"
host_numbers = range(10, 21)
ip_addresses = [
for n in local.host_numbers : cidrhost(local.cidr_block, n)
With the above configuration,
local.ip_addresses should have the following value, which I think matches your requirement:
This approach only works if all of the IP addresses you want to generate are consecutive host numbers under a particular CIDR prefix, which was true for your illustrative example but perhaps isn’t true for all situations your module will deal with. If your requirements aren’t compatible with CIDR-based address calcuations then I’d suggest to calculate the necessary list of IP addresses outside of Terraform and write the resulting list directly in Terraform, or (as @tbugfinder suggested) use the
external data source to run some external code to perform the address calculations you need.