EC2 Linux Server Metadata
Updated: Aug 6
Tutorial blog to access instance metadata from a running instance
Learning Objective:
Learn to access instance metadata from a running instance using:
Instance Metadata Service Version 1 (IMDSv1)
Instance Metadata Service Version 2 (IMDSv2)
Access instance metadata from a running instance using IMDSv1
Step 1: Provision a Linux instance using Amazon Linux 2 AMI.

You may refer to our tutorial blog for step-by-step instructions to create & connect to the EC2 instance.
Step 2: Connect to the instance using EC2 instance connect.
Once you are connected to the instance your command prompt should display similar to the image below.

Step 3: To retrieve the metadata run the following command.
This command will list all the available metadata for the instance e.g. hostname, instance-type, local-ipv4, etc.

Step 4: To retrieve the value of instance id run following command

Similarly you can retrieve rest of the metadata for the instance.
Step 5: Close the SSH session and terminate the instance.

Access instance metadata from a running instance using IMDSv2
Step 1: Provision a Linux instance using Amazon Linux 2023 AMI.
You may refer to our tutorial blog for step-by-step instructions to create & connect to the EC2 instance.

Step 2: Connect to the instance using EC2 instance connect.
Once you are connected to the instance your command prompt should display similar to the image below.

Step 3: To retrieve metadata run the following command.
TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` && curl -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/

Step 4: To retrievethe value of instance id run following command
TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` && curl -H "X-aws-ec2-metadata-token: $TOKEN" –v http://169.254.169.254/latest/meta-data/instance-id/

Similarly you can retrieve rest of the metadata for the instance.
Step 5: Close the SSH session and terminate the instance.

Was this document helpful? How can we make this document better. Please provide your insights. You can download PDF version for reference.