Type Annotations for boto3 SSM

1.42.54 · active · verified Thu Apr 09

mypy-boto3-ssm provides comprehensive type annotations for the AWS Boto3 SSM (Systems Manager) client. This library enhances static analysis, improves auto-completion in IDEs like VSCode and PyCharm, and enables early detection of potential runtime errors with type checkers like Mypy. It is currently at version 1.42.54, which aligns with a specific Boto3 version, and is generated using the `mypy-boto3-builder` (version 8.12.0). The project maintains an active release cadence, syncing with Boto3 updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to get a type-hinted AWS SSM client and retrieve a parameter. It uses explicit type annotations for the client and the response, leveraging the `mypy-boto3-ssm` stubs for static analysis and IDE support. Remember to have `boto3` installed and your AWS credentials configured.

import os
import boto3
from mypy_boto3_ssm.client import SSMClient
from mypy_boto3_ssm.type_defs import GetParameterResultTypeDef

def get_ssm_parameter(name: str) -> str | None:
    # Ensure AWS credentials are available (e.g., via environment variables, ~/.aws/credentials)
    # For local testing, you might need to mock or ensure a valid AWS setup.
    
    # Explicitly type the client for mypy compatibility and IDE autocompletion
    client: SSMClient = boto3.client("ssm")

    try:
        response: GetParameterResultTypeDef = client.get_parameter(Name=name, WithDecryption=True)
        return response.get("Parameter", {}).get("Value")
    except client.exceptions.ParameterNotFound:
        print(f"Parameter '{name}' not found.")
        return None
    except Exception as e:
        print(f"An error occurred: {e}")
        return None

# Example usage (requires an actual AWS SSM parameter and credentials)
# You might need to set AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION
# os.environ['AWS_ACCESS_KEY_ID'] = os.environ.get('AWS_ACCESS_KEY_ID', '')
# os.environ['AWS_SECRET_ACCESS_KEY'] = os.environ.get('AWS_SECRET_ACCESS_KEY', '')
# os.environ['AWS_REGION'] = os.environ.get('AWS_REGION', 'us-east-1')

# Example parameter name - replace with one existing in your AWS account
# parameter_name = "/my/test/parameter"
# parameter_value = get_ssm_parameter(parameter_name)
# if parameter_value:
#    print(f"Value of '{parameter_name}': {parameter_value}")
# else:
#    print(f"Could not retrieve value for '{parameter_name}'.")

view raw JSON →