Boto Session Manager
boto_session_manager is a lightweight Python library designed to simplify the management of AWS boto3 sessions within application code. It enhances the native boto3 SDK by providing features such as boto3 Client auto-completion, cached boto3 Clients, in-application IAM role assumption, and temporary credential management for the AWS CLI. It aims to provide a more user-friendly development experience with boto3. It is currently at version 1.8.1 and actively maintained.
Warnings
- gotcha The auto-refreshable assumed role feature (introduced in v1.5.1) utilizes non-public APIs from `botocore`. These internal APIs are not officially supported and may lead to unstable behavior or breaking changes in future `botocore` updates.
- gotcha For 'Client method auto complete' and 'Arguments type hint' features to work correctly in your IDE, you need to explicitly install `boto3-stubs` with the `[all]` extra (e.g., `pip install "boto3-stubs[all]"`). Without this, your IDE may not provide the expected autocompletion or type checking for boto3 clients obtained through `boto-session-manager`.
- gotcha While `boto-session-manager` helps manage `boto3` sessions, the underlying `boto3.client()` method (when called directly without a session, or if multiple `BotoSesManager` instances are created incorrectly in a multi-threaded application) is not strictly thread-safe during client *creation* (though the client object itself is thread-safe). This can lead to `KeyError: 'credential_provider'` in highly concurrent scenarios.
- gotcha The `BotoSesManager.awscli()` context manager temporarily modifies the default AWS CLI credentials to match the session defined by `BotoSesManager`. While designed to revert automatically, users should be mindful of this temporary state change, especially in environments where concurrent AWS CLI operations are expected.
Install
-
pip install boto-session-manager -
pip install "boto3-stubs[all]"
Imports
- BotoSesManager
from boto_session_manager import BotoSesManager
- AwsServiceEnum
from boto_session_manager import AwsServiceEnum
Quickstart
from boto_session_manager import BotoSesManager
# Initialize the session manager (uses default AWS credentials chain)
bsm = BotoSesManager()
# Get an S3 client. Clients are cached for reuse.
s3_client = bsm.get_client("s3")
# Alternatively, use attribute access for common services for auto-complete
# s3_client = bsm.s3_client
# Example: List S3 buckets
try:
response = s3_client.list_buckets()
print("S3 Buckets:")
for bucket in response.get("Buckets", []):
print(f"- {bucket['Name']}")
except Exception as e:
print(f"Error listing buckets: {e}")
# Example: Assuming an IAM role
# This assumes 'MY_ASSUME_ROLE_ARN' environment variable is set
# or replaced with a valid ARN.
assume_role_arn = os.environ.get('MY_ASSUME_ROLE_ARN', 'arn:aws:iam::123456789012:role/MyTestRole')
if '123456789012' not in assume_role_arn: # Simple check to avoid running with dummy ARN
try:
print(f"\nAttempting to assume role: {assume_role_arn}")
assumed_bsm = bsm.assume_role(
RoleArn=assume_role_arn,
RoleSessionName="my-assumed-session"
)
ec2_client_assumed = assumed_bsm.get_client("ec2")
print(f"Successfully obtained EC2 client with assumed role: {ec2_client_assumed}")
except Exception as e:
print(f"Error assuming role: {e}")