An AWS instance metadata is data about the EC2 instance that we can use to configure or manage the running instance. Instance metadata is divided into categories, for example, hostname, events, and security groups. We can also use instance metadata to access user data that you specified when launching your instance.
There is a number of things about the EC2 instance that we need to remember:
- The EC2 instance operating system only knows the private IPv4 address associated with it, it never knows the Public IPv4 address associated with it.
- The internet gateway’s task is to convert the private IP address to the public IP address.
If we do ifconfig then we can clearly see only private IPv4 and IPv6 addresses are visible within the EC2 instance.
We can use the Meta-data information to know the associated information about the EC2 instances that are not visible within the EC2 instances. To know the metadata about the EC2 instance we need to query the metadata service using the metadata URL and the particular attribute that we are inquiring about by using a curl command.
The curl command URL to use for meta-data is: curl http://169.254.169.254/latest/meta-data/<attributename>
curl command for Public IPv4 will be: curl http://169.254.169.254/latest/meta-data/public-ipv4
curl command for Public Url of the EC2 instance will be: curl http://169.254.169.254/latest/meta-data/public-hostname
Example is below: To know the list of items that we can retrieve from the AWS instance metadata we can use the Instance Metadata query tool. We can download the query tool using the following command:
wget http://s3.amazonaws.com/ec2metadata/ec2-metadata
After downloading the metadata query tool we can make it executable by issuing the command:
chmod u+x ec2-metadata
Now it is ready to be used and if we run “./ec2-metadata –help” command it will show all the list of information that we can retrieve from an EC2 instance metadata by attaching it to the curl Url : http://169.254.169.254/latest/meta-data/<attributename>
Or we can use metadata query tool command: ./ec2-metadata -z