Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a project wherein I need to update configuration files each time an EC2 instance is booted with the Public DNS address of the current instance. I'll be using Perl or Sed for this, so that's not really the question, but the real question is: is there a way that I can determine the instance's public DNS address? Is there an EC2 api that I can access from the instance to determine it?

share|improve this question

1 Answer 1

up vote 13 down vote accepted

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/

share|improve this answer
    
PUBLIC_HOSTNAME="$(curl http://169.254.169.254/latest/meta-data/public-hostname 2>/dev/null)" :) You, sir, are metal. –  Naftuli Tzvi Kay Nov 11 '11 at 3:26
    
glad to help :) –  Tim Kennedy Nov 11 '11 at 3:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.