lambda-sigterm


Namelambda-sigterm JSON
Version 0.1.2 PyPI version JSON
download
home_page
SummaryCreates a noop Lambda Extension which allows Lambda to receive SIGTERM events
upload_time2024-02-12 23:28:16
maintainer
docs_urlNone
author
requires_python>=3.7
licenseMIT License Copyright (c) 2024 Loren Howard Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Lambda SIGTERM - Allows graceful shutdown of Lambdas using Python

The [Lambda Extensions API](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-extensions-api.html) allows lambdas to register extensions. Once an extension is registered, the Lambda runtime will send a SIGTERM to the function code so the extension can collect metrics, etc.

What if you want the benefits of getting SIGTERM signals without the overhead of third-party extensions which collect metrics? Use `lambda-sigterm`!

This library creates a noop extension. All the extension does is register itself with the Lambda Extensions API, then blocks on a background thread. This makes it so the Lambda handler will receive SIGTERM signals. The library code uses `urllib3` (included with the Lambda Python runtime) and Python stdlib libraries, so there are no external dependencies in the Lambda environment.

## Installation
```bash
$ pip install lambda_sigterm
```

## How To Use
To use, import the `lambda_sigterm` module and call the `register()` function:

```python
import signal
import lambda_sigterm

lambda_sigterm.register()

def handle_sigterm(signum, frame):
    print("sigterm captured")

signal.signal(signal.SIGTERM, handle_sigterm)
```

## Full Example

```python
import signal
import lambda_sigterm

lambda_sigterm.register()

def handle_sigterm(signum, frame):
    print("sigterm captured")

signal.signal(signal.SIGTERM, handle_sigterm)

def lambda_handler(event, context):
    print("your application code has started")
```

## `register` Arguments
| keyword_arg | default | description | example |
|---|---|---|---|
| `logger` | `None` | a `logging` `Logger` to use, creates a new logger if none (falsy) is supplied | `logging.getLogger('myapp')` |
| `log_level` | `logging.ERROR` | log-level for the logger if none is supplied | `logging.INFO` |
| `log_level_debug_msg` | `logging.DEBUG` | log-level for debug logs |  |
| `log_level_info_msg` | `logging.INFO` | log-level for info logs |  |
| `log_level_error_msg` | `logging.ERROR` | log-level for error logs |  |
| `lambda_runtime_api_host` | `""` | this is the extensions API host to register the extension with, if unset (default) then this is read from the environment variable `AWS_LAMBDA_RUNTIME_API` |  |
            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "lambda-sigterm",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "",
    "author": "",
    "author_email": "Loren Howard <loren@plutonium.io>",
    "download_url": "https://files.pythonhosted.org/packages/a4/1d/33f4eb3fc51008693c9ae0a61abc7e215183dcbb10178fe6289e8890e6d7/lambda_sigterm-0.1.2.tar.gz",
    "platform": null,
    "description": "# Lambda SIGTERM - Allows graceful shutdown of Lambdas using Python\n\nThe [Lambda Extensions API](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-extensions-api.html) allows lambdas to register extensions. Once an extension is registered, the Lambda runtime will send a SIGTERM to the function code so the extension can collect metrics, etc.\n\nWhat if you want the benefits of getting SIGTERM signals without the overhead of third-party extensions which collect metrics? Use `lambda-sigterm`!\n\nThis library creates a noop extension. All the extension does is register itself with the Lambda Extensions API, then blocks on a background thread. This makes it so the Lambda handler will receive SIGTERM signals. The library code uses `urllib3` (included with the Lambda Python runtime) and Python stdlib libraries, so there are no external dependencies in the Lambda environment.\n\n## Installation\n```bash\n$ pip install lambda_sigterm\n```\n\n## How To Use\nTo use, import the `lambda_sigterm` module and call the `register()` function:\n\n```python\nimport signal\nimport lambda_sigterm\n\nlambda_sigterm.register()\n\ndef handle_sigterm(signum, frame):\n    print(\"sigterm captured\")\n\nsignal.signal(signal.SIGTERM, handle_sigterm)\n```\n\n## Full Example\n\n```python\nimport signal\nimport lambda_sigterm\n\nlambda_sigterm.register()\n\ndef handle_sigterm(signum, frame):\n    print(\"sigterm captured\")\n\nsignal.signal(signal.SIGTERM, handle_sigterm)\n\ndef lambda_handler(event, context):\n    print(\"your application code has started\")\n```\n\n## `register` Arguments\n| keyword_arg | default | description | example |\n|---|---|---|---|\n| `logger` | `None` | a `logging` `Logger` to use, creates a new logger if none (falsy) is supplied | `logging.getLogger('myapp')` |\n| `log_level` | `logging.ERROR` | log-level for the logger if none is supplied | `logging.INFO` |\n| `log_level_debug_msg` | `logging.DEBUG` | log-level for debug logs |  |\n| `log_level_info_msg` | `logging.INFO` | log-level for info logs |  |\n| `log_level_error_msg` | `logging.ERROR` | log-level for error logs |  |\n| `lambda_runtime_api_host` | `\"\"` | this is the extensions API host to register the extension with, if unset (default) then this is read from the environment variable `AWS_LAMBDA_RUNTIME_API` |  |",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2024 Loren Howard  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "Creates a noop Lambda Extension which allows Lambda to receive SIGTERM events",
    "version": "0.1.2",
    "project_urls": {
        "Bug Tracker": "https://github.com/lorenmh/lambda-sigterm/issues",
        "Homepage": "https://github.com/lorenmh/lambda-sigterm"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "af5367bedd7a95e32de85e2f87301e057ce4b05d145111eb68202f3a87fad877",
                "md5": "95395ad57d75e5ba4cdffc96553f4dd8",
                "sha256": "f4d2c43524feb4b619cc5ef8a4cb74ef1073db107cbc088cec31e4609f8dfdc2"
            },
            "downloads": -1,
            "filename": "lambda_sigterm-0.1.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "95395ad57d75e5ba4cdffc96553f4dd8",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 4475,
            "upload_time": "2024-02-12T23:28:13",
            "upload_time_iso_8601": "2024-02-12T23:28:13.648084Z",
            "url": "https://files.pythonhosted.org/packages/af/53/67bedd7a95e32de85e2f87301e057ce4b05d145111eb68202f3a87fad877/lambda_sigterm-0.1.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a41d33f4eb3fc51008693c9ae0a61abc7e215183dcbb10178fe6289e8890e6d7",
                "md5": "c1207b3fc593a1d5c44656376a177edd",
                "sha256": "1484ff4b86ec4286bde7a95f1c267517d87f91f121d6e162b82c5d70dac4ad8e"
            },
            "downloads": -1,
            "filename": "lambda_sigterm-0.1.2.tar.gz",
            "has_sig": false,
            "md5_digest": "c1207b3fc593a1d5c44656376a177edd",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 3294,
            "upload_time": "2024-02-12T23:28:16",
            "upload_time_iso_8601": "2024-02-12T23:28:16.306509Z",
            "url": "https://files.pythonhosted.org/packages/a4/1d/33f4eb3fc51008693c9ae0a61abc7e215183dcbb10178fe6289e8890e6d7/lambda_sigterm-0.1.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-12 23:28:16",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "lorenmh",
    "github_project": "lambda-sigterm",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "lambda-sigterm"
}
        
Elapsed time: 3.29063s