What is the easiest way to get a EC2 public dns inside a running instance?

Solution 1:

You can fetch data about the running instance from a little http API using curl like this:

#/bin/bash
public_name=$(curl -s http://169.254.169.254/latest/meta-data/public-hostname)
echo $public_name

Other values you can fetch include:

  • ami-id
  • hostname
  • instance-id
  • local-ipv4
  • local-hostname
  • public-hostname
  • public-ipv4

There are more but those are some of the most useful. Things like the SSH keys you specify at instance launch can also be retrieved from there.