Is there a way to get the Public DNS address of an instance?

There is. From inside the instance, you can run:

curl http://169.254.169.254/latest/meta-data/public-ipv4

To get the public DNS hostname, you can change that to:

curl http://169.254.169.254/latest/meta-data/public-hostname

You can get the private IP for the instance, too:

curl http://169.254.169.254/latest/meta-data/local-ipv4

As a side note, you can double-check it against a non-AWS site on the internet, like http://ip4.me

#!/bin/bash

pubip=$( curl http://ip4.me 2>/dev/null | sed -e 's#<[^>]*>##g' | grep '^[0-9]' )

echo $pubip

That will work, generally, to check the "public IP" of any NATed system, or to find your public proxy IP, etc.

And here's a good link to read up on the types of information you can get from Amazon's API: http://www.ducea.com/2009/06/01/howto-update-dns-hostnames-automatically-for-your-amazon-ec2-instances/


I define this function inside my .bashrc to retrieve the public ip and dns:

export PUBLIC_DNS=`curl http://169.254.169.254/latest/meta-data/public-hostname 2>/dev/null`
export PUBLIC_IP=`curl http://169.254.169.254/latest/meta-data/public-ipv4 2>/dev/null`

function get-pub() {
  if [ $# -ne 1 ]; then
    echo "Invalid number of arguments"
    return 1
  else
    case $1 in
      dns)
        echo $PUBLIC_DNS
        ;;
      ip)
        echo $PUBLIC_IP
        ;;
      *)
        echo $"Usage: get-pub {dns|ip}"
        return 2
    esac;
  fi
  return 0
}

Tags:

Dns

Amazon Ec2