This is a very handy trick to get your own public IP within Terrafrom. Very useful if your public IP changes a lot and you want to use it with a security group.

provider "aws" {
  region = "eu-west-1"

data "external" "myipaddr" {
  program = ["bash", "-c", "curl -s ''"]

output "my_public_ip" {
  value = "${data.external.myipaddr.result.ip}"

The simply run terrafrom init to download your provider and then terrafrom apply to run.

The output is below, I have changed the IP for security reasons.

data.external.myipaddr: Refreshing state...

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.


my_public_ip =