{"library":"snapshot-restore-py","title":"AWS Lambda SnapStart Python Runtime Hooks","type":"library","description":"The `snapshot-restore-py` library provides runtime hooks for AWS Lambda SnapStart, enabling Python functions to execute code before a snapshot is taken and after a function is restored from a snapshot. This significantly reduces cold start times for Python 3.9+ Lambda functions by allowing the caching and reuse of an initialized execution environment. It is currently at version 1.0.0 and is actively maintained by AWS, with releases primarily driven by Lambda runtime updates or feature enhancements.","language":"python","status":"active","last_verified":"Thu May 21","install":{"commands":["pip install snapshot-restore-py"],"cli":null},"imports":["from snapshot_restore import register_before_snapshot","from snapshot_restore import register_after_restore"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":null,"github":"https://github.com/aws/snapshot-restore-py","docs":null,"changelog":null,"pypi":"https://pypi.org/project/snapshot-restore-py/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"import os\nfrom snapshot_restore import register_before_snapshot, register_after_restore\n\n# Global state that will be snapshotted\nconfig = None\ndb_connection = None\n\ndef load_config():\n    \"\"\"Simulate loading configuration from environment or Parameter Store.\"\"\"\n    global config\n    if config is None:\n        print(\"Loading config...\")\n        # In a real scenario, fetch config from a secure source\n        config = os.environ.get('MY_APP_CONFIG', 'default_config_value')\n        print(f\"Config loaded: {config}\")\n    return config\n\ndef connect_to_db():\n    \"\"\"Simulate connecting to a database.\"\"\"\n    global db_connection\n    if db_connection is None:\n        print(\"Connecting to database...\")\n        # In a real scenario, use actual DB connection, e.g., psycopg2.connect\n        db_connection = f\"DB_Connection_to_{os.environ.get('DB_HOST', 'localhost')}\"\n        print(f\"Database connected: {db_connection}\")\n    return db_connection\n\n@register_before_snapshot\ndef before_snapshot_hook():\n    \"\"\"Hook to run before Lambda takes a snapshot.\n    Use this to clean up resources that cannot be snapshotted (e.g., open network connections).\n    \"\"\"\n    global db_connection\n    print(\"Executing before_snapshot_hook: Closing DB connection.\")\n    if db_connection:\n        # Proper closing of connection in a real app\n        db_connection = None # Simulate closing\n\n@register_after_restore\ndef after_restore_hook():\n    \"\"\"Hook to run after Lambda restores from a snapshot.\n    Use this to re-establish connections or refresh volatile data.\n    \"\"\"\n    global config, db_connection\n    print(\"Executing after_restore_hook: Re-establishing resources.\")\n    # Refresh config if it contains time-sensitive data\n    load_config()\n    # Re-establish DB connection\n    connect_to_db()\n\ndef lambda_handler(event, context):\n    \"\"\"Main Lambda handler function.\"\"\"\n    load_config()\n    db = connect_to_db()\n    print(f\"Handler executed with config: {config} and db: {db}\")\n    return {\n        'statusCode': 200,\n        'body': 'Function executed successfully!'\n    }\n\n# Example of local testing (not part of actual Lambda deployment process)\nif __name__ == \"__main__\":\n    os.environ['MY_APP_CONFIG'] = 'local_test_config'\n    os.environ['DB_HOST'] = 'test_db_instance'\n    \n    print(\"--- First 'invocation' (initialization) ---\")\n    lambda_handler({}, {})\n    \n    print(\"\\n--- Simulating Snapshot and Restore (local only, no actual snapshot) ---\")\n    before_snapshot_hook()\n    after_restore_hook()\n    \n    print(\"\\n--- Second 'invocation' (after restore) ---\")\n    lambda_handler({}, {})\n","lang":"python","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":{"tag":null,"tag_description":null,"last_tested":"2026-05-21","installed_version":"1.0.0","pypi_latest":"1.0.0","is_stale":false,"summary":{"python_range":"3.10–3.9","success_rate":50,"avg_install_s":1.6,"avg_import_s":null,"wheel_type":"wheel"},"results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"snapshot-restore-py","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"17.8M"},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"snapshot-restore-py","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"snapshot-restore-py","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":1.4,"import_time_s":null,"mem_mb":null,"disk_size":"18M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"snapshot-restore-py","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"snapshot-restore-py","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"19.6M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"snapshot-restore-py","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"snapshot-restore-py","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":1.6,"import_time_s":null,"mem_mb":null,"disk_size":"20M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"snapshot-restore-py","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"snapshot-restore-py","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"11.5M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"snapshot-restore-py","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"snapshot-restore-py","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":1.6,"import_time_s":null,"mem_mb":null,"disk_size":"12M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"snapshot-restore-py","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"snapshot-restore-py","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"11.2M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"snapshot-restore-py","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"snapshot-restore-py","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":1.4,"import_time_s":null,"mem_mb":null,"disk_size":"12M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"snapshot-restore-py","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"snapshot-restore-py","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"17.3M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"snapshot-restore-py","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"snapshot-restore-py","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":1.7,"import_time_s":null,"mem_mb":null,"disk_size":"18M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"snapshot-restore-py","exit_code":1,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null}]}}