Is there a function to transfer ip to cidr_block format in terraform

8,008 views
Skip to first unread message

hanks

unread,
Sep 16, 2016, 1:20:49 AM9/16/16
to Terraform
like "${ip2cidr(ec2_instance.private_ip, 32)}", "172.21.3.211" -> "172.21.3.211/32"

David Maze

unread,
Sep 16, 2016, 7:21:00 AM9/16/16
to Terraform
On Friday, September 16, 2016 at 1:20:49 AM UTC-4, hanks wrote:
like "${ip2cidr(ec2_instance.private_ip, 32)}", "172.21.3.211" -> "172.21.3.211/32"

variable "addr" { default = "172.21.3.211" }

variable "bits" { default = "25" }

resource "template_file" "cidr" {

  template = "$${network}/${var.bits}"

  vars {

    network = "${cidrhost("${var.addr}/${var.bits}", 0)}"

  }

}

output "cidr" { value = "${template_file.cidr.rendered}" }


David Adams

unread,
Sep 16, 2016, 9:36:13 AM9/16/16
to terrafo...@googlegroups.com
Here's a shorter version that can work around the need for a template resource:

    variable "addr" { default = "172.21.3.211" }
    variable "bits" { default = "32" }
    output "cidr" {
      value = "${cidrhost("${var.addr}/${var.bits}", 0)}/${var.bits}"
    }


--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/aefc8469-810f-42bc-9a7a-b1983fd72752%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Martin Atkins

unread,
Sep 20, 2016, 2:49:24 PM9/20/16
to Terraform
If you know you always want it to be a /32 prefix then I would literally write "${aws_instance.foo.private_ip}/32" ... might as well keep it simple, no?

Terraform has a selection of functions for manipulting CIDR prefixes, but they are focused on extending the prefix (subnets/host numbering given a prefix) rather than truncating it (calculating a CIDR mask given a host address and a prefix length). As David noted, you can (ab)use the cidrhost function to do this sort of truncation by exploiting the fact that it will replace any bits that extend beyond the given prefix with the given host number, which can be zero... but perhaps we could make that more explicit by having a first-class function for doing this.

Trevor Robinson

unread,
Jan 12, 2017, 4:26:08 PM1/12/17
to Terraform
And for lists, you can use formatlist to append the "/32":

${formatlist("%s/32", aws_eip.stuff.*.public_ip)}

Or for multiple lists:

${formatlist("%s/32", concat(aws_eip.stuff.*.public_ip, aws_eip.things.*.public_ip))}

This need arises when putting IPs into AWS security group rules, since they require CIDR notation. Since that's not uncommon, a predefined function might be warranted.

-Trevor
Reply all
Reply to author
Forward
0 new messages