I want to identify the public ip of the terraform execution environment and add it to the security group

There's an easier way to do that without any scripts. The trick is having a website such as icanhazip.com which retrieve your IP, so set it in your terraform file as data:

data "http" "myip" {
  url = "http://ipv4.icanhazip.com"
}

And whenever you want to place your IP just use data.http.myip.body, example:

ingress {
  from_port = 5432
  to_port = 5432
  protocol = "tcp"
  cidr_blocks = ["${chomp(data.http.myip.body)}/32"]
}
  • Note I used terraform chomp() method to remove any trailing space or new line which comes with body.

  • You can use your ipv6 with http://ipv6.icanhazip.com. Take care by just using http://icanhazip.com because it can retrieve ipv4 or ipv6


In Terraform 0.12 and greater you can use native jsondecode and jsonencode to get better reliability and long term reproducibility rather than relying on string manipulation. For reference, this implementation was tested on Terraform 0.12.16.

An example implementation for grabbing your public ip address using the newer Terraform methods currently available is below. Output block is added to provide visual verification that it is working as intended.

data "http" "my_public_ip" {
  url = "https://ifconfig.co/json"
  request_headers = {
    Accept = "application/json"
  }
}

locals {
  ifconfig_co_json = jsondecode(data.http.my_public_ip.body)
}

output "my_ip_addr" {
  value = local.ifconfig_co_json.ip
}

If you want to utilize this in a network ingress rule, you can do the following (icmp utilized for testability in the example, without unnecessarily opening up ports in case this is blindly copy and pasted) :

ingress {
  from_port = 0
  to_port = 0
  protocol = "-1"
  cidr_blocks = ["${local.ifconfig_co_json.ip}/32"]
}

Please note that the service referenced is rate limited to ~1 request per min, so you might end up wanting to host this yourself if your needs exceed this; or if you need more guaranteed availability. The echoip service is containerized so can be hosted nearly anywhere.

https://github.com/mpolden/echoip

Tags:

Terraform