ec2-metadata is a Python library that provides an easy, cached interface to query the EC2 Instance Metadata Service (IMDS). It supports IMDSv2 exclusively and is designed for use within EC2 instances to retrieve details like instance ID, AMI ID, region, and more. The current version is 3.0.0, and it is actively maintained.
pip install ec2-metadataVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to retrieve basic EC2 instance metadata attributes using the singleton `ec2_metadata` object. This code should be run from within an EC2 instance.
Ensure EC2 instances are configured to support IMDSv2 (default behavior) or explicitly enable it. Consult AWS documentation on 'Transition to using Instance Metadata Service Version 2'.
Reconfigure your EC2 instance's metadata options to allow more hops, e.g., `aws ec2 modify-instance-metadata-options --instance-id <instance-id> --http-put-response-hop-limit 3`.
Use AWS Secrets Manager or Parameter Store for sensitive data and retrieve them securely within your application.
Migrate from `boto.utils.get_instance_metadata` to `ec2-metadata` or `boto3`'s equivalent if available for specific needs.
Run `pip install ec2-metadata` to install the library.
For containerized environments, reconfigure your EC2 instance's metadata options to increase the `HttpPutResponseHopLimit` to a value like 3: `aws ec2 modify-instance-metadata-options --instance-id <your-instance-id> --http-put-response-hop-limit 3`. Additionally, ensure no firewalls or proxies are blocking access to `http://169.254.169.254` within the instance.
Verify that the code is running on an EC2 instance. Ensure that IMDS is enabled for the instance. Check local firewall rules (e.g., `iptables`, `firewalld`) and proxy configurations on the instance to ensure they allow outbound HTTP access to `http://169.254.169.254`.
No dependency data recorded yet.