Skip to main content

Context manager that solves PynamoDB's static credential limitation by enabling dynamic AWS session switching.

Project description

Documentation Status https://github.com/MacHu-GWU/pynamodb_session_manager-project/actions/workflows/main.yml/badge.svg https://codecov.io/gh/MacHu-GWU/pynamodb_session_manager-project/branch/main/graph/badge.svg https://img.shields.io/pypi/v/pynamodb-session-manager.svg https://img.shields.io/pypi/l/pynamodb-session-manager.svg https://img.shields.io/pypi/pyversions/pynamodb-session-manager.svg https://img.shields.io/badge/✍️_Release_History!--None.svg?style=social&logo=github https://img.shields.io/badge/⭐_Star_me_on_GitHub!--None.svg?style=social&logo=github
https://img.shields.io/badge/Link-API-blue.svg https://img.shields.io/badge/Link-Install-blue.svg https://img.shields.io/badge/Link-GitHub-blue.svg https://img.shields.io/badge/Link-Submit_Issue-blue.svg https://img.shields.io/badge/Link-Request_Feature-blue.svg https://img.shields.io/badge/Link-Download-blue.svg

Welcome to pynamodb_session_manager Documentation

https://pynamodb-session-manager.readthedocs.io/en/latest/_static/pynamodb_session_manager-logo.png

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(target_bsm, User):
    # 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(target_bsm, User, 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(staging_bsm, User):
    User.create_table(wait=True)

# Copy data from staging to production
with use_boto_session(staging_bsm, User):
    staging_users = list(User.scan())

with use_boto_session(prod_bsm, User):
    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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pynamodb_session_manager-0.1.1.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pynamodb_session_manager-0.1.1-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file pynamodb_session_manager-0.1.1.tar.gz.

File metadata

  • Download URL: pynamodb_session_manager-0.1.1.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.8

File hashes

Hashes for pynamodb_session_manager-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4330ac15e37f032dbc216dfe8a048227f18952e654d739a7fa4b273bff79b2f5
MD5 e9043208a6440eb56014aee5a40e9b45
BLAKE2b-256 18e5e7e4b518eceb328d2b17330367bf7aef81f5365923552ece8e85338b6004

See more details on using hashes here.

File details

Details for the file pynamodb_session_manager-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pynamodb_session_manager-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3df7ce92ee7219c3c8f5a72dacc9194575ab86862b3f99d56200bed648befa54
MD5 16bd28458b8737d78cd31e891c83e51f
BLAKE2b-256 e3f96456324718bfd11e0ba6e02a363fa6f6d4f1cc2052d1a639081b290dceaf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page