# Session Lambda
<p align="center">
<img src="assets/session_management_with.jpg" data-canonical-src="assets/session_management_with.jpg" width=400/>
</p>
<p align="center">
<em>A simple way to manage sessions for AWS Lambdas</em>
</p>
## Install
```
pip install session-lambda
```
## Prerequisites
### DynamoDB Table
- A table in DynamoDB with a primary key named `key` with type string
- [optional] Enable TTL in your DynamoDB table with attribute named `ttl`
## Usage
Set `SESSION_LAMBDA_DYNAMODB_TABLE_NAME` env var:
```bash
export SESSION_LAMBDA_DYNAMODB_TABLE_NAME=<table-name>
```
Run the following python code:
```python
import time
from session_lambda import session, set_session_data, get_session_data
@session
def lambda_handler(event, context):
print(get_session_data())
set_session_data((get_session_data() or 0)+1)
return {"headers":{}}
```
```python
# first client_a call
response = lambda_handler({'headers':{}}, {})
# get session id from response (created by the server)
session_id = response.get('headers').get('session-id')
# use session id in subsequent calls
lambda_handler({'headers':{'session-id':session_id}}, {})
lambda_handler({'headers':{'session-id':session_id}}, {})
lambda_handler({'headers':{'session-id':session_id}}, {})
# first client_b call
lambda_handler({'headers':{}}, {})
```
You should get the following prints:
```python
None
1
1
1
None
```
This time using the `update=True` mode:
```python
import time
from session_lambda import session, set_session_data, get_session_data
@session(update=True)
def lambda_handler(event, context):
print(get_session_data())
set_session_data((get_session_data() or 0)+1)
return {"headers":{}}
```
```python
# first client_a call
response = lambda_handler({'headers':{}}, {})
# get session id from response (created by the server)
session_id = response.get('headers').get('session-id')
# use session id in subsequent calls
lambda_handler({'headers':{'session-id':session_id}}, {})
lambda_handler({'headers':{'session-id':session_id}}, {})
lambda_handler({'headers':{'session-id':session_id}}, {})
# first client_b call
lambda_handler({'headers':{}}, {})
```
Now you should see:
```python
None
1
2
3
None
```
## Features
```python
@session(id_key_name='session-id', update=False, ttl=0)
def lambda_handler(event, context):
...
```
- `id_key_name` is the expected key name in the `event[headers]`. It is default to `session-id`. It is case-sensitive.
- `update` flag control weather `set_sessions_data` updates the data. It is default to `False`.
- `ttl` is seconds interval for the session to live (since the last update time). By default it is disabled. Any value larger then 0 will enable this feature. Make sure to set the TTL key name in your dynamodb to `ttl`.
## Future Features
- Support Schema validation for session data
Raw data
{
"_id": null,
"home_page": "https://github.com/roy-pstr/session-lambda",
"name": "session-lambda",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7,<4.0",
"maintainer_email": "",
"keywords": "aws lambda,session,dynamodb",
"author": "Roy Pasternak",
"author_email": "roy.pasternakk@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/3c/63/11b1f5715ba68f784bf9b263c40ce8094aa4a85d72dbd825c4dd4f47b782/session_lambda-0.3.4.tar.gz",
"platform": null,
"description": "# Session Lambda\n\n<p align=\"center\">\n<img src=\"assets/session_management_with.jpg\" data-canonical-src=\"assets/session_management_with.jpg\" width=400/>\n</p>\n\n<p align=\"center\">\n <em>A simple way to manage sessions for AWS Lambdas</em>\n</p>\n\n## Install\n```\npip install session-lambda\n```\n\n## Prerequisites\n### DynamoDB Table\n- A table in DynamoDB with a primary key named `key` with type string \n- [optional] Enable TTL in your DynamoDB table with attribute named `ttl`\n\n## Usage\nSet `SESSION_LAMBDA_DYNAMODB_TABLE_NAME` env var:\n```bash\nexport SESSION_LAMBDA_DYNAMODB_TABLE_NAME=<table-name>\n```\nRun the following python code:\n```python\nimport time\nfrom session_lambda import session, set_session_data, get_session_data\n\n@session\ndef lambda_handler(event, context):\n print(get_session_data())\n set_session_data((get_session_data() or 0)+1)\n return {\"headers\":{}}\n```\n```python\n# first client_a call \nresponse = lambda_handler({'headers':{}}, {}) \n# get session id from response (created by the server)\nsession_id = response.get('headers').get('session-id')\n# use session id in subsequent calls\nlambda_handler({'headers':{'session-id':session_id}}, {})\nlambda_handler({'headers':{'session-id':session_id}}, {})\nlambda_handler({'headers':{'session-id':session_id}}, {})\n\n# first client_b call \nlambda_handler({'headers':{}}, {})\n```\nYou should get the following prints:\n```python\nNone\n1\n1\n1\nNone\n```\nThis time using the `update=True` mode:\n```python\nimport time\nfrom session_lambda import session, set_session_data, get_session_data\n\n@session(update=True)\ndef lambda_handler(event, context):\n print(get_session_data())\n set_session_data((get_session_data() or 0)+1)\n return {\"headers\":{}}\n```\n```python\n# first client_a call \nresponse = lambda_handler({'headers':{}}, {}) \n# get session id from response (created by the server)\nsession_id = response.get('headers').get('session-id')\n# use session id in subsequent calls\nlambda_handler({'headers':{'session-id':session_id}}, {})\nlambda_handler({'headers':{'session-id':session_id}}, {})\nlambda_handler({'headers':{'session-id':session_id}}, {})\n\n# first client_b call \nlambda_handler({'headers':{}}, {})\n```\nNow you should see:\n```python\nNone\n1\n2\n3\nNone\n```\n\n## Features\n```python\n@session(id_key_name='session-id', update=False, ttl=0)\ndef lambda_handler(event, context):\n ...\n```\n- `id_key_name` is the expected key name in the `event[headers]`. It is default to `session-id`. It is case-sensitive.\n- `update` flag control weather `set_sessions_data` updates the data. It is default to `False`.\n- `ttl` is seconds interval for the session to live (since the last update time). By default it is disabled. Any value larger then 0 will enable this feature. Make sure to set the TTL key name in your dynamodb to `ttl`.\n\n## Future Features\n- Support Schema validation for session data\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A simple session manager for aws lambda function using dynamodb",
"version": "0.3.4",
"split_keywords": [
"aws lambda",
"session",
"dynamodb"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "42dbaae3bad5aedadc2a38e374bc9947f4a47cf72f70c26effa29f9f0fbf9b05",
"md5": "a00e2e7703b1e01f1ddb601033d13fc0",
"sha256": "6c2fa065dd019fbb1514e89f1086a23c462b9a73de52dfcf3b2742ceb0afb948"
},
"downloads": -1,
"filename": "session_lambda-0.3.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "a00e2e7703b1e01f1ddb601033d13fc0",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7,<4.0",
"size": 6801,
"upload_time": "2023-02-03T23:58:01",
"upload_time_iso_8601": "2023-02-03T23:58:01.793794Z",
"url": "https://files.pythonhosted.org/packages/42/db/aae3bad5aedadc2a38e374bc9947f4a47cf72f70c26effa29f9f0fbf9b05/session_lambda-0.3.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "3c6311b1f5715ba68f784bf9b263c40ce8094aa4a85d72dbd825c4dd4f47b782",
"md5": "48f9e24d194a1945139f6d804837151a",
"sha256": "4f4d9184fad3edf170a44eea0c25e5685f0d484b3a3a1a03fa56f92ddcf91019"
},
"downloads": -1,
"filename": "session_lambda-0.3.4.tar.gz",
"has_sig": false,
"md5_digest": "48f9e24d194a1945139f6d804837151a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7,<4.0",
"size": 5511,
"upload_time": "2023-02-03T23:58:03",
"upload_time_iso_8601": "2023-02-03T23:58:03.699460Z",
"url": "https://files.pythonhosted.org/packages/3c/63/11b1f5715ba68f784bf9b263c40ce8094aa4a85d72dbd825c4dd4f47b782/session_lambda-0.3.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-02-03 23:58:03",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "roy-pstr",
"github_project": "session-lambda",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "session-lambda"
}