Type Annotations for Boto3 EC2

1.42.85 · active · verified Sat Apr 11

types-boto3-ec2 provides comprehensive type annotations for the Amazon EC2 service client in boto3, enabling static type checking with tools like mypy, pyright, and enhanced IDE support. It is part of the mypy-boto3-builder ecosystem, which generates separate stub packages for each AWS service. The current version is 1.42.85, with frequent updates synchronized with boto3 releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how types-boto3-ec2 provides type hints for your boto3 EC2 client, enabling static analysis and IDE autocompletion. The 'if TYPE_CHECKING:' block is optional but shows how to explicitly reference the stub types without creating a runtime dependency. The type checker will automatically infer types for `boto3.client('ec2')`.

import boto3
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from types_boto3_ec2 import EC2Client

# Boto3 client will be automatically typed by mypy-boto3-ec2 if installed
ec2_client: EC2Client = boto3.client('ec2')

# Example usage with type-hinted client
response = ec2_client.describe_instances(
    Filters=[
        {
            'Name': 'instance-state-name',
            'Values': ['running']
        },
    ]
)

for reservation in response['Reservations']:
    for instance in reservation['Instances']:
        print(f"Instance ID: {instance['InstanceId']}, Type: {instance['InstanceType']}")

view raw JSON →