snapshot-restore-py


Namesnapshot-restore-py JSON
Version 1.0.0 PyPI version JSON
download
home_pagehttps://github.com/aws/snapshot-restore-py
SummaryRuntime Hooks for AWS Lambda SnapStart - Python
upload_time2024-11-13 14:40:22
maintainerNone
docs_urlNone
authorAmazon Web Services
requires_python>=3.9
licenseApache License 2.0
keywords serverless aws lambda python snapstart runtime hooks
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Snapshot Restore for Python

### Description

This is the Snapshot Restore for Python library which can be used for registering [runtime hooks](https://docs.aws.amazon.com/lambda/latest/dg/snapstart-runtime-hooks-python.html) in Snapstart enabled Python Lambda functions

This library provides two decorators that you can use to define your runtime hooks:

* `@register_before_snapshot` - For code you want to run before a snapshot is taken
* `@register_after_restore` - For code you want to run after a snapshot is restored

Alternatively, you can use the following methods to register callables for runtime hooks:

* `register_before_snapshot(func, *args, **kwargs)`
* `register_after_restore(func, *args, **kwargs)`

## Code sample

### Register hooks using decoratores
```
from snapshot_restore_py import register_before_snapshot, register_after_restore

def lambda_handler(event, context):
    # your logic here
    ...

@register_before_snapshot
def fn_before_snapshot():
    # your logic here
    ...

@register_after_restore
def fn_after_restore():
    # your logic here
    ...
```

### Register hooks using functions
```
from snapshot_restore_py import register_before_snapshot, register_after_restore

def lambda_handler(event, context):
    // Lambda handler code

def fn_before_snapshot():
    # your logic here
    ...

def fn_after_restore():
    # your logic here
    ...

register_before_snapshot(fn_before_snapshot)
register_after_restore(fn_after_restore)

```

Please refer to the [examples provided](examples) for more details.

## Important points to note

### Execution order

  - BeforeSnapshot hooks will run in the **reverse** order** as they were registered
  - AfterRestore hooks will run in the **same order** as they were registered.

### Best Practices

  - While decorators are convenient, using functions for hook registration can offer more explicit control over the order of execution and improve code readability.

### Registering functions with arguments

  - To register a function with specific arguments, just use the `register_...(func, *args, **kwargs)` method.

### Importing Modules

  - If you register a hook in a file that isn’t imported in your main handler, it will be ignored.
  - If you import hooks within the `lambda_handler`, they won’t be executed. Instead, make sure all essential imports are at the **top level** of your code. This way, your hooks will be recognized and executed as intended!

### Duplicate registrations

  - If you register a function more than once (like decorating it and registering it again), it’ll run multiple times.

### Local environment

- **Pre-installed in Lambda Runtime**: When deploying to AWS Lambda, you do not need to include this library in your deployment package, as it is already pre-installed in the Lambda runtime environment.

- **Local Testing Consideration**: For local testing, ensure that you include the library in your development dependencies to avoid any import issues during testing.



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/aws/snapshot-restore-py",
    "name": "snapshot-restore-py",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "serverless aws lambda python snapstart runtime hooks",
    "author": "Amazon Web Services",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/27/19/2d7584749a7f6d1b4b3a129995b1bc31e70ed9c5ecc323f1ee748b767268/snapshot-restore-py-1.0.0.tar.gz",
    "platform": null,
    "description": "# Snapshot Restore for Python\n\n### Description\n\nThis is the Snapshot Restore for Python library which can be used for registering [runtime hooks](https://docs.aws.amazon.com/lambda/latest/dg/snapstart-runtime-hooks-python.html) in Snapstart enabled Python Lambda functions\n\nThis library provides two decorators that you can use to define your runtime hooks:\n\n* `@register_before_snapshot` - For code you want to run before a snapshot is taken\n* `@register_after_restore` - For code you want to run after a snapshot is restored\n\nAlternatively, you can use the following methods to register callables for runtime hooks:\n\n* `register_before_snapshot(func, *args, **kwargs)`\n* `register_after_restore(func, *args, **kwargs)`\n\n## Code sample\n\n### Register hooks using decoratores\n```\nfrom snapshot_restore_py import register_before_snapshot, register_after_restore\n\ndef lambda_handler(event, context):\n    # your logic here\n    ...\n\n@register_before_snapshot\ndef fn_before_snapshot():\n    # your logic here\n    ...\n\n@register_after_restore\ndef fn_after_restore():\n    # your logic here\n    ...\n```\n\n### Register hooks using functions\n```\nfrom snapshot_restore_py import register_before_snapshot, register_after_restore\n\ndef lambda_handler(event, context):\n    // Lambda handler code\n\ndef fn_before_snapshot():\n    # your logic here\n    ...\n\ndef fn_after_restore():\n    # your logic here\n    ...\n\nregister_before_snapshot(fn_before_snapshot)\nregister_after_restore(fn_after_restore)\n\n```\n\nPlease refer to the [examples provided](examples) for more details.\n\n## Important points to note\n\n### Execution order\n\n  - BeforeSnapshot hooks will run in the **reverse** order** as they were registered\n  - AfterRestore hooks will run in the **same order** as they were registered.\n\n### Best Practices\n\n  - While decorators are convenient, using functions for hook registration can offer more explicit control over the order of execution and improve code readability.\n\n### Registering functions with arguments\n\n  - To register a function with specific arguments, just use the `register_...(func, *args, **kwargs)` method.\n\n### Importing Modules\n\n  - If you register a hook in a file that isn\u2019t imported in your main handler, it will be ignored.\n  - If you import hooks within the `lambda_handler`, they won\u2019t be executed. Instead, make sure all essential imports are at the **top level** of your code. This way, your hooks will be recognized and executed as intended!\n\n### Duplicate registrations\n\n  - If you register a function more than once (like decorating it and registering it again), it\u2019ll run multiple times.\n\n### Local environment\n\n- **Pre-installed in Lambda Runtime**: When deploying to AWS Lambda, you do not need to include this library in your deployment package, as it is already pre-installed in the Lambda runtime environment.\n\n- **Local Testing Consideration**: For local testing, ensure that you include the library in your development dependencies to avoid any import issues during testing.\n\n\n",
    "bugtrack_url": null,
    "license": "Apache License 2.0",
    "summary": "Runtime Hooks for AWS Lambda SnapStart - Python",
    "version": "1.0.0",
    "project_urls": {
        "Homepage": "https://github.com/aws/snapshot-restore-py"
    },
    "split_keywords": [
        "serverless",
        "aws",
        "lambda",
        "python",
        "snapstart",
        "runtime",
        "hooks"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "10b8f9da2457e9dfb5872042202d69b329940527672e20cbfdb26610c09e1d8e",
                "md5": "65a4167b68085c83e138ea78822d064c",
                "sha256": "38f99e696793790f54658e71c68c7a8a40cea877c81232b5052383b1301aceba"
            },
            "downloads": -1,
            "filename": "snapshot_restore_py-1.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "65a4167b68085c83e138ea78822d064c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 3782,
            "upload_time": "2024-11-13T14:40:21",
            "upload_time_iso_8601": "2024-11-13T14:40:21.330562Z",
            "url": "https://files.pythonhosted.org/packages/10/b8/f9da2457e9dfb5872042202d69b329940527672e20cbfdb26610c09e1d8e/snapshot_restore_py-1.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "27192d7584749a7f6d1b4b3a129995b1bc31e70ed9c5ecc323f1ee748b767268",
                "md5": "9c451c427df0c42741bd50a127022b33",
                "sha256": "4d27f82fb6f09968f422501e9c3c2dea48a46cd19dc798eb7d6cbc57523c8004"
            },
            "downloads": -1,
            "filename": "snapshot-restore-py-1.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "9c451c427df0c42741bd50a127022b33",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 3379,
            "upload_time": "2024-11-13T14:40:22",
            "upload_time_iso_8601": "2024-11-13T14:40:22.477532Z",
            "url": "https://files.pythonhosted.org/packages/27/19/2d7584749a7f6d1b4b3a129995b1bc31e70ed9c5ecc323f1ee748b767268/snapshot-restore-py-1.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-13 14:40:22",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "aws",
    "github_project": "snapshot-restore-py",
    "github_not_found": true,
    "lcname": "snapshot-restore-py"
}
        
Elapsed time: 0.42838s