Session Lambda
A simple way to manage sessions for AWS Lambdas
This package simplfy the way one can use DynamoDB as a key/value presistance layer for AWS Lambda - using a single python decorator.
Install
pip install session-lambda
Prerequisites
DynamoDB Table
- A table in DynamoDB with a primary key named
keyof type string - [optional] Enable TTL in your DynamoDB table with attribute named
ttl
Usage
Set SESSION_LAMBDA_DYNAMODB_TABLE_NAME env var:
export SESSION_LAMBDA_DYNAMODB_TABLE_NAME=<table-name>
Run the following python code:
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":{}}
# 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:
None
1
1
1
None
This time using the update=True mode:
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":{}}
# 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:
None
1
2
3
None
Features
@session(id_key_name='session-id', update=False, ttl=0)
def lambda_handler(event, context):
...
id_key_nameis the expected key name in theevent[headers]. It is default tosession-id. It is case-sensitive.updateflag control weatherset_sessions_dataupdates the data. It is default toFalse.ttlis 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 tottl.
Future Features
- Support Schema validation for session data
Additional Inforamtion
-
Check this
session-lambda-examplerepository for more usage examples: https://github.com/roy-pstr/session-lambda-example -
Check this medium post for general guidance: https://medium.com/@roy-pstr/a-simple-way-to-manage-sessions-with-aws-lambda-dynamodb-in-python-c7aae1aa7258
Metadata
Release files for session-lambda 0.3.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| session_lambda-0.3.6.tar.gz | 6.1 kB | Without provenance | Details for session_lambda-0.3.6.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| session_lambda-0.3.6-py3-none-any.whl | Python 3 | none | any | Details for session_lambda-0.3.6-py3-none-any.whl |
Total release size: 13.4 kB
Release files / session_lambda-0.3.6.tar.gz
| Download URL | session_lambda-0.3.6.tar.gz |
|---|---|
| Size | 6.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
50c8b5e0b4b454c8bce214ebd7099434ce818f047044e4e80e9a25747c5488b0
|
|
BLAKE2b-256 checksum How to use checksums |
0a5dd464c92d5710c248207ffacf8985e5662dc73204b84ecedbfea142bc06e7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.3.2 CPython/3.9.5 Darwin/21.1.0
|
Release files / session_lambda-0.3.6-py3-none-any.whl
| Download URL | session_lambda-0.3.6-py3-none-any.whl |
|---|---|
| Size | 7.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6ec28ae2eacfd10cb37949943d1805a0ddba8528024b813b60e94013df49afb9
|
|
BLAKE2b-256 checksum How to use checksums |
3e5cbaf9cc5009248d95e8e183bfb58b67d9fcc40e8329021caedaf33d3d9b58
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.3.2 CPython/3.9.5 Darwin/21.1.0
|
Security for session-lambda 0.3.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.