Type Annotations for aiobotocore EC2
This library provides comprehensive type annotations for the aiobotocore EC2 service (version 3.4.0), generated by `mypy-boto3-builder`. It enables static type checking with tools like MyPy and Pyright, offering improved developer experience for asynchronous AWS interactions. The project maintains an active release cadence, often aligning with `aiobotocore` and `botocore` updates.
Warnings
- breaking Support for Python 3.8 has been removed in `mypy-boto3-builder` version 8.12.0 and subsequent generated packages. Users on Python 3.8 must either upgrade Python or use an older version of `types-aiobotocore-ec2`.
- breaking Generated TypeDef naming conventions may have changed in `mypy-boto3-builder` version 8.9.0. For instance, `CreateDistributionRequestRequestTypeDef` might become `CreateDistributionRequestTypeDef`. Code relying on specific TypeDef names may break.
- gotcha Installing `types-aiobotocore-ec2` only provides type annotations. For actual runtime execution, you must also install the `aiobotocore` library itself (`pip install aiobotocore`).
- gotcha When using standalone service packages like `types-aiobotocore-ec2` or `types-aiobotocore-lite`, explicit type annotations (e.g., `client: EC2Client`) are often required for full type checking and IDE support. The main `types-aiobotocore` package with overloads can sometimes infer types implicitly.
- gotcha PyCharm users might experience slow performance or high CPU usage due to the large number of `Literal` overloads in these stub packages (related to PyCharm issue PY-40997).
Install
-
pip install types-aiobotocore-ec2 -
pip install 'types-aiobotocore[ec2]'
Imports
- EC2Client
from types_aiobotocore_ec2.client import EC2Client
- DescribeInstancesResultTypeDef
from types_aiobotocore_ec2.type_defs import DescribeInstancesResultTypeDef
- EC2ServiceResource
from types_aiobotocore_ec2.service_resource import EC2ServiceResource
Quickstart
import asyncio
from aiobotocore.session import get_session
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from types_aiobotocore_ec2.client import EC2Client
from types_aiobotocore_ec2.type_defs import DescribeInstancesResultTypeDef
async def describe_ec2_instances():
session = get_session()
async with session.create_client("ec2") as client:
# Explicit type annotation for the client
client: "EC2Client" = client
response = await client.describe_instances()
print(f"Found {len(response['Reservations'])} reservations.")
for reservation in response['Reservations']:
for instance in reservation['Instances']:
print(f" Instance ID: {instance['InstanceId']}, State: {instance['State']['Name']}")
# Example of using a TypeDef for a dictionary structure
example_type_def: "DescribeInstancesResultTypeDef" = response
print(f"Example TypeDef Reservations key: {example_type_def['Reservations'][0]['ReservationId']}")
if __name__ == "__main__":
asyncio.run(describe_ec2_instances())