Amazon EC2 - Retrieving Metadata
One can use tools like curl and wget to retrieve information from Amazon's metadata service to get information about an EC2 server you are sending the request from. This can be quite useful, as you may wish to write a script that may do different things based on the instance ID etc.
This will return a list of additional paths you can query to get the information you require.
If it ends in a
/ then you know that it is a directory or additonal paths, and not a single piece of information to be returned (like instance-id).
ami-id ami-launch-index ami-manifest-path block-device-mapping/ events/ hostname identity-credentials/ instance-action instance-id instance-life-cycle instance-type local-hostname local-ipv4 mac metrics/ network/ placement/ profile public-hostname public-ipv4 public-keys/ reservation-id security-groups services/
The examples above are straightforward and use IMDSv1. If you need to use IMDSv2, then you would do the following:
TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 3"` \ && curl -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/
However, bear in mind that doing so will change the response structure.
First published: 5th May 2023