mypy-boto3-ec2 Type Stubs

1.42.85 · active · verified Thu Apr 09

mypy-boto3-ec2 provides type annotations for the boto3 EC2 service. It ensures that your boto3 client calls for EC2 are type-checked by tools like MyPy, catching potential errors at development time. The current version is 1.42.85, closely following boto3/botocore releases, with frequent updates tied to AWS API changes.

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `mypy-boto3-ec2` to type-hint an EC2 client and process its responses with type-safe accessors. It lists all EC2 instances in the default region.

import boto3
from mypy_boto3_ec2.client import EC2Client
from mypy_boto3_ec2.type_defs import InstanceTypeDef, ReservationTypeDef

def list_ec2_instances() -> None:
    # Type-hint the boto3 client for EC2
    ec2_client: EC2Client = boto3.client('ec2')

    print('Describing EC2 instances...')
    response = ec2_client.describe_instances()

    # Use type definitions for better type checking on the response
    for reservation in response.get('Reservations', []):
        reservation_typed: ReservationTypeDef = reservation
        for instance in reservation_typed.get('Instances', []):
            instance_typed: InstanceTypeDef = instance
            instance_id = instance_typed.get('InstanceId', 'N/A')
            instance_type = instance_typed.get('InstanceType', 'N/A')
            state = instance_typed.get('State', {}).get('Name', 'N/A')
            print(f'  Instance ID: {instance_id}, Type: {instance_type}, State: {state}')

if __name__ == '__main__':
    # This quickstart assumes AWS credentials are configured (e.g., via environment variables or ~/.aws/credentials)
    list_ec2_instances()

view raw JSON →