Welcome to pynamodb_session_manager Documentation
pynamodb_session_manager enables PynamoDB models to dynamically switch AWS credentials at runtime without modifying model definitions.
Problem Background
PynamoDB 6.0.0+ allows setting table-level connections by explicitly providing credentials, but this approach is not flexible or elegant for dynamic credential switching. Each PynamoDB model stores AWS session information in a _connection attribute. When you first use an ORM class to send a DynamoDB request, PynamoDB checks the model’s Meta class for AWS credentials. If none are found, it uses the default AWS profile and creates a connection stored in _connection, and then use it for all subsequent requests. This means that once a model’s connection is established, it cannot be changed without modifying the model’s definition or using a different model class.
As the author of boto-session-manager, an advanced boto3 session manager that can temporarily change the “Default AWS Profile” using context managers, I created this library to solve PynamoDB’s dynamic credential switching limitation.
How It Works
The use_boto_session context manager temporarily stores the current ORM class configuration, resets the _connection using the provided boto session manager, and conditionally reverts it back (depending on restore_on_exit parameter).
Quick Start
from pynamodb.models import Model
from pynamodb.attributes import UnicodeAttribute
from boto_session_manager import BotoSesManager
from pynamodb_session_manager import use_boto_session
# Define your PynamoDB model
class User(Model):
class Meta:
table_name = "users"
region = "us-east-1"
id = UnicodeAttribute(hash_key=True)
# Create session manager for different AWS account/profile
target_bsm = BotoSesManager(profile_name="target_profile")
# Use different credentials temporarily
with use_boto_session(User, target_bsm):
# All operations here use target_profile credentials
User.create_table(wait=True)
user = User(id="123")
user.save()
# Back to default credentials
# This will fail if table doesn't exist in default account
try:
user = User.get("123") # Uses default credentials
except Exception:
print("Table not found in default account")
Advanced Usage
from pynamodb_session_manager import reset_connection
# Keep connection after context exits
with use_boto_session(User, target_bsm, restore_on_exit=False):
user = User(id="456")
user.save()
# Connection still uses target_profile
user = User.get("456") # Still uses target_profile
# Manually reset to default credentials
reset_connection(User)
# Now uses default credentials again
Multiple Account Operations
default_bsm = BotoSesManager() # Default profile
staging_bsm = BotoSesManager(profile_name="staging")
prod_bsm = BotoSesManager(profile_name="production")
# Create table in staging
with use_boto_session(User, staging_bsm):
User.create_table(wait=True)
# Copy data from staging to production
with use_boto_session(User, staging_bsm):
staging_users = list(User.scan())
with use_boto_session(User, prod_bsm):
User.create_table(wait=True)
for user in staging_users:
user.save()
For comprehensive examples and advanced usage patterns, see the complete test suite.
Install
pynamodb_session_manager is released on PyPI, so all you need is to:
$ pip install pynamodb-session-manager
To upgrade to latest version:
$ pip install --upgrade pynamodb-session-manager
Metadata
Release files for pynamodb-session-manager 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pynamodb_session_manager-0.1.2.tar.gz | 10.7 kB | Without provenance | Details for pynamodb_session_manager-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pynamodb_session_manager-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for pynamodb_session_manager-0.1.2-py3-none-any.whl |
Total release size: 21.3 kB
Release files / pynamodb_session_manager-0.1.2.tar.gz
| Download URL | pynamodb_session_manager-0.1.2.tar.gz |
|---|---|
| Size | 10.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3435f1a7eadcd6479087a02108b758eea74e8d9d5b38310cd6b0831502e2e169
|
|
BLAKE2b-256 checksum How to use checksums |
7bddcb46abb3788efed32395bd111431c011987d22ca20760d7bb8e96e5b39cc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.8
|
Release files / pynamodb_session_manager-0.1.2-py3-none-any.whl
| Download URL | pynamodb_session_manager-0.1.2-py3-none-any.whl |
|---|---|
| Size | 10.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
864b296d79216538c14652a04281ede99120dd8bb9506f55810abb6727acc009
|
|
BLAKE2b-256 checksum How to use checksums |
59326029da7d50e497efb0ab58989a30d7a53ed9f60287572c4af3d4be94a79d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.8
|
Security for pynamodb-session-manager 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.