{"id":3809,"library":"snapshot-restore-py","title":"AWS Lambda SnapStart Python Runtime Hooks","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.","status":"active","version":"1.0.0","language":"en","source_language":"en","source_url":"https://github.com/aws/snapshot-restore-py","tags":["aws","lambda","snapstart","serverless","python","cold-start"],"install":[{"cmd":"pip install snapshot-restore-py","lang":"bash","label":"Install for local development/testing"}],"dependencies":[{"reason":"Requires Python 3.9 or newer, with optimal SnapStart support for Python 3.12+ managed runtimes.","package":"python","optional":false}],"imports":[{"symbol":"register_before_snapshot","correct":"from snapshot_restore import register_before_snapshot"},{"symbol":"register_after_restore","correct":"from snapshot_restore import register_after_restore"}],"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."},"warnings":[{"fix":"Ensure `from snapshot_restore import ...` and the decorated functions are placed globally in your module.","message":"Runtime hooks (@register_before_snapshot, @register_after_restore) must be imported and defined at the top level of your Python code, outside of the `lambda_handler` function. Importing or defining them within the handler will cause them to be ignored and not executed by SnapStart.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Exclude `snapshot-restore-py` from your Lambda deployment package (e.g., using `.dockerignore` or `exclude` in `serverless.yml`). Install it only for local development.","message":"The `snapshot-restore-py` library is pre-installed in AWS Lambda's Python 3.9+ managed runtimes. You should *not* include this library in your deployment package when deploying to Lambda, as it can lead to conflicts or increase your package size unnecessarily. For local development and testing, however, you *do* need to install it (`pip install snapshot-restore-py`).","severity":"gotcha","affected_versions":"All versions"},{"fix":"Identify all volatile global state. In your `@register_after_restore` hook, add code to re-establish connections, re-fetch credentials, or re-generate random seeds.","message":"Global state (e.g., timestamps, external service tokens, credentials, or open network/database connections) captured in the snapshot will be stale or invalid upon restoration. You *must* implement logic within `register_after_restore` hooks to re-initialize or refresh any such time-sensitive or ephemeral data.","severity":"breaking","affected_versions":"All versions"},{"fix":"Optimize your `after_restore_hook` functions to perform only essential, quick operations. Consider asynchronous patterns or lazy initialization for longer tasks if absolutely necessary.","message":"The total execution time for all `@register_after_restore` hooks combined is limited to 10 seconds. If your after-restore logic exceeds this timeout, the Lambda function will fail to restore from the snapshot with a `SnapStartTimeoutException`.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}