Type Annotations for aiobotocore EC2

3.4.0 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This example demonstrates how to set up an `aiobotocore` client for EC2 and apply type annotations using `types-aiobotocore-ec2`. It explicitly types the client and shows how to leverage TypeDefs for response objects, enabling better static analysis and IDE autocompletion for asynchronous AWS calls.

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())

view raw JSON →