mypy-boto3-ec2-instance-connect

1.42.3 · active · verified Sat Apr 11

mypy-boto3-ec2-instance-connect provides type annotations for the `boto3` EC2InstanceConnect service client. It ensures that your interactions with the AWS SDK for Python are type-safe, allowing tools like MyPy to catch potential errors at development time. This package is part of the `mypy-boto3-builder` ecosystem, currently at version 1.42.3, and is frequently updated to align with `boto3` releases and Python typing improvements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a `boto3` EC2InstanceConnect client and leverage `mypy-boto3-ec2-instance-connect` for type-hinting. The client creation relies on `boto3`'s standard credential chain (e.g., environment variables, `~/.aws/credentials`). Mypy will validate the types of the client object and any request dictionaries at static analysis time, enhancing code reliability.

import os
from typing import TYPE_CHECKING
import boto3

# Conditional import for type checking only
if TYPE_CHECKING:
    from mypy_boto3_ec2_instance_connect.client import EC2InstanceConnectClient
    from mypy_boto3_ec2_instance_connect.type_defs import (
        SendSSHPublicKeyRequestRequestTypeDef,
        SendSSHPublicKeyResponseTypeDef
    )

def get_ec2_instance_connect_client() -> "EC2InstanceConnectClient":
    """
    Returns a type-hinted EC2InstanceConnect client.
    This function demonstrates how to obtain a client that mypy can type-check.
    boto3 client creation will implicitly use AWS credentials from environment variables
    (e.g., AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) or configuration files.
    """
    # Explicitly retrieve AWS region to ensure it's set, for example.
    aws_region = os.environ.get("AWS_REGION", "us-east-1")
    
    # The returned client will be typed as EC2InstanceConnectClient by mypy
    # but at runtime it's a botocore.client.EC2InstanceConnect
    client: "EC2InstanceConnectClient" = boto3.client("ec2-instance-connect", region_name=aws_region)
    return client

if __name__ == "__main__":
    print("Attempting to get an EC2InstanceConnect client...")
    try:
        typed_client = get_ec2_instance_connect_client()
        print(f"Successfully obtained client (runtime type: {type(typed_client)}).")
        print("Note: If AWS credentials are not configured, subsequent API calls will fail.")

        # Example of a type-checked dictionary for an API call
        # mypy will validate the structure and types of this dictionary
        sample_request: "SendSSHPublicKeyRequestRequestTypeDef" = {
            "InstanceId": "i-1234567890abcdef0", # Placeholder for an actual EC2 instance ID
            "InstanceOSUser": "ec2-user",         # Placeholder for the OS user
            "SSHPublicKey": "ssh-rsa AAAAB3NzaC...", # Placeholder for a valid public key
            "AvailabilityZone": "us-east-1a"    # Placeholder for the instance's AZ
        }
        print(f"\nExample type-checked request parameters:\n{sample_request}")

        # To make an actual API call, uncomment the following.
        # This will require valid AWS credentials and permissions.
        # response: SendSSHPublicKeyResponseTypeDef = typed_client.send_ssh_public_key(**sample_request)
        # print(f"\nAPI Response: {response}")

    except Exception as e:
        print(f"Error: Could not initialize client or access AWS: {e}")
        print("Please ensure boto3 is installed and AWS credentials (e.g., via environment variables like AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION or ~/.aws/credentials) are configured correctly.")

view raw JSON →