skillfed

ec2-metadata

An easy interface to query the EC2 metadata API, with caching.

ec2-metadata v3.0.0 2.4M downloads/30d#3,111 on PyPI115
Permissive license MIT Active released

What it is and what it does

ec2-metadata is a Python library that provides a clean interface to the AWS EC2 Instance Metadata Service v2 (IMDSv2), which is more secure against SSRF attacks than the older v1 service. It wraps urllib3 to handle HTTP requests to the metadata endpoint and caches most attributes on first access, since EC2 instance metadata is largely immutable without stopping the instance.

The library exposes a singleton instance (ec2_metadata) with attributes like region, instance_id, availability_zone, iam_security_credentials, and account_id. Most attributes are cached using functools.cached_property and can be cleared individually or all at once. It handles IMDSv2's token-based authentication automatically and is designed as a lightweight alternative to boto3 for applications that only need metadata, not the full AWS SDK.

Use it for:

  • Retrieve the current EC2 instance's region or availability zone at runtime for region-aware application logic.
  • Fetch temporary IAM security credentials from instance metadata for AWS API calls without managing long-lived keys.
  • Determine instance identity and configuration (instance type, AMI ID, account ID) for logging, monitoring, or auto-discovery.
  • Build Docker containers on EC2 that need to query instance metadata without adding boto3 as a dependency.
  • Implement health checks or lifecycle hooks that read uncached metadata like instance_action or autoscaling target state.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Queries the EC2 Instance Metadata Service v2 to retrieve instance configuration data (region, instance ID, IAM credentials, availability zone, and similar) with built-in caching.

Yes. This is a stable, actively maintained library with no known vulnerabilities, low install friction, and a permissive MIT license. It solves a real gap left by boto3's lack of a metadata utility. Install it if you're running on EC2 and need to query instance metadata without pulling in the full AWS SDK.

Install

ec2-metadata on PyPI

pip

pip install ec2-metadata

uv

uv add ec2-metadata

poetry

poetry add ec2-metadata

Installing ec2-metadata

Before you install

Low friction install with a single runtime dependency (urllib3). Actively maintained as of 2026-02-11, with recent commits and no known vulnerabilities. Supports Python 3.10 through 3.14.

License in practice

MIT license permits free use, modification, and distribution with minimal restrictions—suitable for both open-source and commercial projects.

Quickstart

pip install ec2-metadata

from ec2_metadata import ec2_metadata
print(ec2_metadata.region)
print(ec2_metadata.instance_id)

Must be running on an EC2 instance with access to the metadata service. IMDSv2 has a default hop limit of 1, which can cause timeouts in Docker containers; reconfigure with aws ec2 modify-instance-metadata-options --http-put-response-hop-limit 3 if needed.

Verify before relying

  • Whether the singleton instance pattern (ec2_metadata) is thread-safe for concurrent access in multi-threaded applications.
  • Performance characteristics of the metadata API calls under high-frequency access patterns.
  • Behavior when running outside EC2 or when the metadata service is unavailable.

Package facts

License MIT (permissive)
Python support supports the current Python release (>=3.10)
Install friction low — pure-Python wheel
Runtime dependencies 1 — urllib3
Maintenance actively maintained — 184 days since the last release
Last repo commit
First released
Downloads 2,355,170/month — #3,111 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: ec2_metadata-3.0.0-py3-none-any.whl

Keywords: AWS, EC2, metadata

Development Status :: 5 - Production/StableIntended Audience :: DevelopersNatural Language :: EnglishOperating System :: OS IndependentProgramming Language :: Python :: 3 :: OnlyProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Typing :: Typed

Tags

ec2 metadata api clientaws instance metadata queryec2 instance info retrievalaws metadata service v2ec2 region and instance idaws iam credentials from metadataec2 instance configuration
aws-ec2metadata-serviceinstance-discovery

More Monitoring packages

tqdm

Wraps any iterable to display a real-time…

copyleft · top 100 on PyPI

opentelemetry-semantic-conventions

Provides generated Python code for…

permissive · top 100 on PyPI

opentelemetry-sdk

Provides the reference implementation of the…

permissive · top 100 on PyPI

opentelemetry-api

Provides the abstract API and interfaces for…

permissive · top 100 on PyPI

opentelemetry-exporter-otlp-proto-http

Exports OpenTelemetry observability data to an…

permissive · top 1,000 on PyPI

opentelemetry-instrumentation

Provides automatic instrumentation commands and…

permissive · top 1,000 on PyPI

aws-cdk.aws-ec2

Provides AWS CDK constructs for defining EC2…

permissive · top 15,000 on PyPI

boto_session_manager

Wraps boto3 sessions with typed client…

permissive · top 5,000 on PyPI

sqlalchemy-rdsiam

Provides SQLAlchemy dialects for connecting to…

unclear · top 15,000 on PyPI

sagemaker-studio

A Python SDK for accessing Amazon SageMaker…

permissive · top 5,000 on PyPI

IP2Location

Looks up geolocation data (country, region,…

permissive · top 15,000 on PyPI

requests-aws-sign

Adds AWS V4 request signing to the requests…

permissive · top 5,000 on PyPI

aws-requests-auth

Adds AWS Signature Version 4 authentication to…

permissive · top 1,000 on PyPI

gcs-oauth2-boto-plugin

Provides OAuth 2.0 authentication for Google…

permissive · top 15,000 on PyPI

aws-cdk.aws-iam

Provides Python constructs for defining AWS IAM…

permissive · top 15,000 on PyPI

sample-helper-aws-appconfig

Fetches and caches configuration from AWS…

permissive · top 15,000 on PyPI