mypy-boto3-elb Type Stubs for AWS Elastic Load Balancing

1.42.3 · active · verified Sat Apr 11

mypy-boto3-elb provides type annotations for the `boto3` Elastic Load Balancing (ELB) service. It allows static type checkers like MyPy and Pyright to validate `boto3` calls, improving code quality and developer experience. The library version 1.42.3 matches the corresponding `boto3` version, and it's actively maintained with frequent updates reflecting changes in AWS services and `boto3` itself.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize an Elastic Load Balancing (ELB) client with type hints and use it to list classic load balancers. The `TYPE_CHECKING` block ensures that type-related imports are only active during static analysis, preventing runtime overhead. Replace dummy AWS credentials or ensure they are configured in your environment.

import boto3
from typing import TYPE_CHECKING
import os

# Ensure boto3 is configured, e.g., via environment variables or AWS CLI config
# For quickstart, using dummy values if not set in environment
os.environ.setdefault('AWS_ACCESS_KEY_ID', 'YOUR_ACCESS_KEY')
os.environ.setdefault('AWS_SECRET_ACCESS_KEY', 'YOUR_SECRET_KEY')
os.environ.setdefault('AWS_DEFAULT_REGION', 'us-east-1')

if TYPE_CHECKING:
    from mypy_boto3_elb.client import ElasticLoadBalancingClient
    from mypy_boto3_elb.type_defs import LoadBalancerDescriptionTypeDef

def list_classic_load_balancers() -> None:
    """Lists classic load balancers and their DNS names with type hints."""
    # boto3.client automatically infers the return type if mypy-boto3-elb is installed
    client: ElasticLoadBalancingClient = boto3.client("elb")
    
    try:
        response = client.describe_load_balancers()
        load_balancers: list[LoadBalancerDescriptionTypeDef] = response["LoadBalancerDescriptions"]
        
        if not load_balancers:
            print("No classic load balancers found.")
            return

        print("Classic Load Balancers:")
        for lb in load_balancers:
            print(f"  Name: {lb['LoadBalancerName']}")
            print(f"  DNS Name: {lb['DNSName']}")
            print(f"  Scheme: {lb.get('Scheme', 'internet-facing')}") # 'Scheme' might be optional
    except Exception as e:
        print(f"Error describing load balancers: {e}")

if __name__ == "__main__":
    list_classic_load_balancers()

view raw JSON →