ec2-metadata
An easy interface to query the EC2 metadata API, with caching.
Decision gist · record as of 2026-08-14
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.AI-flagged interpretation of the facts on this page — verify before relying
Before you install
- 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.
- Low friction install with a single runtime dependency (urllib3).
License · maintenance · safety
MIT (permissive) — MIT license permits free use, modification, and distribution with minimal restrictions—suitable for both open-source and commercial projects.
last release 2026-02-11 (184 days) · last repo commit 2026-08-13 · 115 stars
0 known vulnerabilities (OSV.dev, 2026-08-14) · 2,355,170 downloads/mo, #3,111 on PyPI
Alternatives
Verify before relying
pip install ec2-metadata
from ec2_metadata import ec2_metadata
print(ec2_metadata.region)
print(ec2_metadata.instance_id)- 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.
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 on it.
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
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.
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.
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)
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 packageurllib3 |
| 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 |
| Classifiers | 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 |
Evidence: ec2_metadata-3.0.0-py3-none-any.whl
Tags
Let your AI agent find packages like this
Example. Real query, live index.
You found this page by searching. An agent finds it by wishing: SkillFed indexes 14,416 PyPI packages by what they can do, searchable in plain language.
wish › “ec2 metadata api client”
- ec2-metadataQueries the EC2 Instance Metadata Service v2 to retrieve instance…
- types-boto3-ec2Provides type annotations and IDE autocomplete support for boto3 EC2…
- types-aiobotocore-ec2Provides type stubs and annotations for aiobotocore EC2 3.9.0,…
Give your agent the search over MCP, or paste the wish link into any chat.
More Monitoring packages
Wraps any iterable to display a real-time progress bar in the terminal or Jupyter notebook, showing iteration count, elapsed time, and estimated time remaining.
Provides generated Python code for OpenTelemetry semantic conventions, enabling standardized attribute naming and constant definitions for instrumentation and telemetry collection.
Install it if you are using OpenTelemetry and want to follow semantic conventions correctly.
Provides the reference implementation of the OpenTelemetry API for collecting and exporting traces, metrics, and logs from Python applications.
Provides the abstract API and interfaces for OpenTelemetry instrumentation in Python, defining how to emit traces, metrics, and logs without tying code to a specific SDK implementation.
Exports OpenTelemetry observability data to an OpenTelemetry Collector using Protobuf-encoded messages over HTTP.
Install it if you are using OpenTelemetry in Python and need to send data to a Collector over HTTP.
Provides automatic instrumentation commands and programmatic APIs to inject distributed tracing into Python applications without code changes, detecting and instrumenting packages used by your program.
Install it if you need distributed tracing without code changes and have compatible instrumented packages in your environment.
See also aws-cdk.aws-ec2 · boto_session_manager · sqlalchemy-rdsiam · sagemaker-studio · IP2Location · requests-aws-sign · aws-requests-auth · gcs-oauth2-boto-plugin · aws-cdk.aws-iam · sample-helper-aws-appconfig