Install & Compatibility
Where this runs
tested against v1.0.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.012s · 17.8MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.5s · import 0.006s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
register_before_snapshot
✓ from snapshot_restore_py import register_before_snapshot
✗ from snapshot_restore import register_before_snapshot
register_after_restore
✓ from snapshot_restore_py import register_after_restore
get_before_snapshot
✓ from snapshot_restore_py import get_before_snapshot
This quickstart demonstrates how to use `register_before_snapshot` and `register_after_restore` decorators for an AWS Lambda function. The `load_config` and `connect_to_db` functions simulate resource initialization that benefits from SnapStart. The `before_snapshot_hook` cleans up resources (like database connections) that cannot be snapshotted, while the `after_restore_hook` re-establishes them or refreshes time-sensitive data after a function is restored from a snapshot. The example includes local testing instructions to simulate the hook execution flow.
import os
from snapshot_restore import register_before_snapshot, register_after_restore
# Global state that will be snapshotted
config = None
db_connection = None
def load_config():
"""Simulate loading configuration from environment or Parameter Store."""
global config
if config is None:
print("Loading config...")
# In a real scenario, fetch config from a secure source
config = os.environ.get('MY_APP_CONFIG', 'default_config_value')
print(f"Config loaded: {config}")
return config
def connect_to_db():
"""Simulate connecting to a database."""
global db_connection
if db_connection is None:
print("Connecting to database...")
# In a real scenario, use actual DB connection, e.g., psycopg2.connect
db_connection = f"DB_Connection_to_{os.environ.get('DB_HOST', 'localhost')}"
print(f"Database connected: {db_connection}")
return db_connection
@register_before_snapshot
def before_snapshot_hook():
"""Hook to run before Lambda takes a snapshot.
Use this to clean up resources that cannot be snapshotted (e.g., open network connections).
"""
global db_connection
print("Executing before_snapshot_hook: Closing DB connection.")
if db_connection:
# Proper closing of connection in a real app
db_connection = None # Simulate closing
@register_after_restore
def after_restore_hook():
"""Hook to run after Lambda restores from a snapshot.
Use this to re-establish connections or refresh volatile data.
"""
global config, db_connection
print("Executing after_restore_hook: Re-establishing resources.")
# Refresh config if it contains time-sensitive data
load_config()
# Re-establish DB connection
connect_to_db()
def lambda_handler(event, context):
"""Main Lambda handler function."""
load_config()
db = connect_to_db()
print(f"Handler executed with config: {config} and db: {db}")
return {
'statusCode': 200,
'body': 'Function executed successfully!'
}
# Example of local testing (not part of actual Lambda deployment process)
if __name__ == "__main__":
os.environ['MY_APP_CONFIG'] = 'local_test_config'
os.environ['DB_HOST'] = 'test_db_instance'
print("--- First 'invocation' (initialization) ---")
lambda_handler({}, {})
print("\n--- Simulating Snapshot and Restore (local only, no actual snapshot) ---")
before_snapshot_hook()
after_restore_hook()
print("\n--- Second 'invocation' (after restore) ---")
lambda_handler({}, {})
Upgrade
Version history
1.0.0latest on PyPI · released Nov 13, 2024
Audit
Dependencies
pythonrequiredRequires Python 3.9 or newer, with optimal SnapStart support for Python 3.12+ managed runtimes.