Print external host name of EC2 instance

Solution 1:

From within the instance, you can hit the Instance Metadata Service:

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

or

curl http://instance-data.ec2.internal/latest/meta-data/public-hostname

A wide variety of data is available via this interface.

Solution 2:

/opt/aws/bin/ec2-metadata -p will print the public hostname of that instance.