Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon EC2 instance metadata

Tags:

amazon-ec2

I ran this command to get the instance-id from a EC2 instance, how is the request processed and how does the service know what details to send back ?

wget -q -O - http://169.254.169.254/latest/meta-data/instance-id

thanks

like image 818
Santhosh S Avatar asked Sep 21 '11 07:09

Santhosh S


People also ask

How do I find my EC2 instance metadata?

To view instance metadata, you can only use the link-local address of 169.254. 169.254 to access. Requests to the metadata via the URI are free, so there are no additional charges from AWS. Using the curl tool on Linux or the PowerShell cmdlet Invoke-WebRequest on Windows, you will first create your token.

What is instance metadata in AWS?

Instance metadata is data about your instance that you can use to configure or manage the running instance. Instance metadata is divided into categories, for example, host name, events, and security groups. You can also use instance metadata to access user data that you specified when launching your instance.

How do I find my instance metadata service version?

If you want to determine it from the EC2 instance, you can just try sending a request to http://169.254.169.254/ and see what the status code is.

What is the difference between metadata and user data?

The main difference between Data and Metadata is that data is simply the content that can provide a description, measurement, or even a report on anything relative to an enterprise's data assets. On the other hand, metadata describes the relevant information on said data, giving them more context for data users.


2 Answers

At a high level, wget is a command that initiates an HTTP web request (pretending to be a browser) and those options tell it to spit out the resulting response to stdout (what you see).

Since the EC2 dom0 host controls the network stack as seen by your instance running in a virtual machine, EC2 can handle network traffic to 169.254.169.254 any way it wants.

In this case, EC2 knows what instance is making the request (whether it's based on your internal IP address controlled by EC2, or based on the fact that the dom0 host may be processing the request before it even gets sent across the network).

So, EC2 knows what instance is making the request and EC2 knows all the information about every instance, so EC2 can return the meta-data that is requested including the instance id.

Amazon hasn't published exactly how they have implemented this feature, but they do guarantee that it will return the correct data for the requesting instance with no chance of anybody else interfering.

You can learn more about available EC2 metadata here:

http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?AESDG-chapter-instancedata.html

The magic IP address 169.254.169.254 and the corresponding meta-data URLs will not work outside of an EC2 instance, unless you happen to be running on a system which is trying to emulate EC2.

like image 119
Eric Hammond Avatar answered Oct 08 '22 13:10

Eric Hammond


You can use ec2metadata

ec2metadata --instance-id
like image 37
Rag Sagar Avatar answered Oct 08 '22 13:10

Rag Sagar