Chris Mendez in For Developers, AWS

AWS: Metadata on EC2

As a web developer on EC2, there are times when I have to put on my DevOps hat and either write some Bash scripts or maybe use Ansible or Chef. It's not very often but common use cases include automating the process of starting a new EC2 instance, getting the DNS or IP address and adding it to either S3 or Route53. In these situations, the first thing I need to do is figure out where in the command line I will get that metadata. Here's how.

Step 1 - Log in

Log into your EC2 instance via ssh. In this example, I am using my Public DNS address but you can also use IPv4 Public IP.

ssh -i /path/to/your/key.pem ec2-user@enter-your-ec2-public-dns.compute-1.amazonaws.com  

Step 2 - Switch User

You will need to switch to Super User.

sudo su  

Step 3 - GET Request

Once you are a Super User, run the curl command to request the top-level metadata on your instance.

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

You will get this response:

EC2 Metadata Response

Step 4 - Going Deeper

Now that you're able to get the high-level metadata information, you can an make more specialized requests such as:

Get public IP address.

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

Get

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