Understanding EC2 Instance Metadata Service V2 (IMDSv2) and Its Application

Jason Anderson
2 min readMar 20, 2024

--

The rise of cloud computing has unlocked a myriad of possibilities, with Amazon EC2 standing out as one of the most popular compute services. Provided by Amazon Web Services (AWS), EC2 allows users to run and manage server instances in the cloud. To enhance security and flexibility, AWS introduced the second version of the EC2 Instance Metadata Service (IMDSv2). This article delves into IMDSv2, focusing on how to obtain an EC2 instance’s local IP address after enabling IMDSv2.

What is the EC2 Instance Metadata Service?

The EC2 Instance Metadata Service is an internal service available to EC2 instances, enabling them to query information about themselves, such as instance ID, AMI ID, network configurations, and more. This information is invaluable for scenarios like automation configurations, script executions, and self-configuration during application startup.

Introducing IMDSv2

IMDSv2 is an updated version of the instance metadata service that strengthens security. Compared to the first version (IMDSv1), IMDSv2 introduces a session-oriented model that requires the creation of a session (or token acquisition) before making any metadata information requests. This enhancement significantly improves the security of interactions with the metadata service.

How to Use IMDSv2 to Obtain an EC2 Instance’s Local IP Address?

Obtaining an EC2 instance’s local IP address is a common requirement that can be achieved in two steps in an IMDSv2-enabled environment:

1. Acquiring a Session Token

First, use the following command to create a token. This token will be used in subsequent requests as authentication to prove the request originates from a legitimate EC2 instance:

TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" \
-H "X-aws-ec2-metadata-token-ttl-seconds: 21600")

Here, X-aws-ec2-metadata-token-ttl-seconds represents the token's lifespan (in seconds), set to 21600 seconds (6 hours) in this example.

2. Using the Token to Obtain the Local IP Address

With the token acquired, you can now use it to request the instance’s local IP address:

curl -H "X-aws-ec2-metadata-token: \
$TOKEN" http://169.254.169.254/latest/meta-data/local-ipv4

This will return the local IP address of the EC2 instance.

Simplifying the Command

For those who prefer an all-in-one approach, the steps above can be condensed into a single command:

curl -H "X-aws-ec2-metadata-token: \
$(curl -X PUT "http://169.254.169.254/latest/api/token" \
-H "X-aws-ec2-metadata-token-ttl-seconds: 21600")" \
http://169.254.169.254/latest/meta-data/local-ipv4

This command first acquires a token and then immediately uses that token to obtain the local IP address, streamlining the process.

Conclusion

IMDSv2 significantly enhances the security of interactions with the EC2 instance metadata service by introducing a token mechanism. Understanding how to obtain an EC2 instance’s local IP address in an IMDSv2-enabled environment is crucial for developers and system administrators. The methods and techniques presented in this article can help you manage your EC2 instances efficiently and securely. As cloud computing technology continues to evolve, mastering these core skills is indispensable for every cloud computing professional.

--

--