mypy-boto3-ec2-instance-connect
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
- breaking Starting with `mypy-boto3-builder` version 8.12.0 (which generates this stub package), Python 3.8 is no longer supported. The minimum required Python version is now 3.9.
- breaking In `mypy-boto3-builder` 8.9.0, there were breaking changes to TypeDef naming conventions. Type definitions for packed method arguments now use shorter names (e.g., `CreateDistributionRequestRequestTypeDef` became `CreateDistributionRequestTypeDef`), and conflicting TypeDef `Extra` postfixes were moved to the end.
- gotcha This package provides *only* type stubs. It does not include the `boto3` library itself. You must install `boto3` separately for your code to run.
- gotcha The primary utility of `mypy-boto3-ec2-instance-connect` is for static type checking. Imports from this package (e.g., `from mypy_boto3_ec2_instance_connect.client import EC2InstanceConnectClient`) should ideally be placed within an `if TYPE_CHECKING:` block to prevent runtime dependencies and potential import errors if the stub package is not present at runtime.
Install
-
pip install mypy-boto3-ec2-instance-connect -
pip install boto3 mypy
Imports
- EC2InstanceConnectClient
from typing import TYPE_CHECKING if TYPE_CHECKING: from mypy_boto3_ec2_instance_connect.client import EC2InstanceConnectClient - SendSSHPublicKeyRequestRequestTypeDef
from typing import TYPE_CHECKING if TYPE_CHECKING: from mypy_boto3_ec2_instance_connect.type_defs import SendSSHPublicKeyRequestRequestTypeDef - SendSSHPublicKeyResponseTypeDef
from typing import TYPE_CHECKING if TYPE_CHECKING: from mypy_boto3_ec2_instance_connect.type_defs import SendSSHPublicKeyResponseTypeDef
Quickstart
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.")