impl¶
PynamoDB Context Manager for Using Specific Boto3 Sessions
This module provides a context manager that allows PynamoDB models to temporarily use different AWS credentials/sessions without modifying the model definition. This is particularly useful for:
Multi-account operations
Different AWS profiles for different environments
Temporary credential switching
Cross-account DynamoDB operations
- pynamodb_session_manager.impl.use_boto_session(table: Type[Model], bsm: BotoSesManager | None = None, restore_on_exit: bool = True)[source]¶
Context manager to temporarily switch PynamoDB model to use different AWS credentials.
This context manager allows a PynamoDB model to temporarily use different AWS credentials by leveraging the boto-session-manager’s awscli() context manager and manipulating the model’s connection and region settings.
- Parameters:
table – The PynamoDB model class that will use the new credentials.
bsm – The boto session manager instance containing the target AWS credentials/profile to use. If None, the context manager has no effect, providing a clean API where users can conditionally switch credentials without separate if/else logic. See https://pypi.org/project/boto-session-manager/
restore_on_exit – If True, restore original connection on exit if False, keep current connection for subsequent operations. See also
reset_connection().
- Yields:
None, This is a context manager that doesn’t return a value
Usage:
# Define your model class MyModel(Model): class Meta: table_name = "my_table" region = "us-east-1" id = NumberAttribute(hash_key=True) # Use different credentials temporarily target_bsm = BotoSesManager(profile_name="target_profile") with target_aws_cred(target_bsm, MyModel): # All operations here use the target_profile credentials MyModel.create_table() item = MyModel(id=1) item.save() # Back to original credentials items = MyModel.scan() # This uses default credentials
How it works:
Saves the current region setting from the model’s Meta class
Enters the boto session manager’s awscli() context (sets env vars)
Clears the model’s cached connection to force recreation
Updates the model’s region to match the session manager’s region
Creates a new Connection object with the new region
On exit, restores the original region and clears connection again
Note
- The model’s _connection attribute is set to None to force PynamoDB
to create a new connection with the current environment variables
The region is temporarily changed to ensure consistency with the session
All changes are reverted when exiting the context manager when restore_on_exit is True
- pynamodb_session_manager.impl.reset_connection(table: Type[Model])[source]¶
Reset the PynamoDB model’s connection to use the default AWS connection.
This is useful when you have changed AWS credentials using
use_boto_session()context manager, and then want to revert back to the default PynamoDB behavior.- Parameters:
table – The PynamoDB model class whose connection should be reset.
Usage:
# Define your model class MyModel(Model): ... # Use different credentials and keep the connection with use_boto_session(target_bsm, MyModel, restore_on_exit=False): ... # Reset the connection to use the default AWS credentials reset_connection(MyModel)