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.
pip install boto-session-managerVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize the BotoSesManager, retrieve a cached boto3 client for a service (e.g., S3), and perform a basic operation. It also includes an example of assuming an IAM role, which is a key feature of the library, assuming a valid role ARN is provided via an environment variable or hardcoded for testing.
Be aware of potential instability. For critical production workflows requiring highly stable assumed roles, monitor upstream `botocore` changes or consider implementing custom refresh logic if issues arise.
Run `pip install "boto3-stubs[all]"` in your development environment. This is a development-time dependency and usually not required in production.
Ensure that `BotoSesManager` instances are created once and reused across threads/processes, or that clients are obtained from a single, shared `BotoSesManager` instance. Avoid creating new `BotoSesManager` instances or directly calling `boto3.client()` within hot paths of multi-threaded code. `boto-session-manager`'s caching mechanism for clients inherently helps mitigate this if used properly.
Ensure that operations that rely on a specific AWS CLI credential context are performed within the `with bsm.awscli():` block. Avoid relying on the modified CLI credentials outside of this context manager if other CLI operations might be affected.