Skip to main content

A simple session manager for aws lambda function using dynamodb

Project description

Session Lambda

A simple way to manage sessions for AWS Lambdas

Install

pip install session-lambda

Example

Set SESSION_LAMBDA_DYNAMODB_TABLE_NAME env var:

export SESSION_LAMBDA_DYNAMODB_TABLE_NAME=<table-name>

Run the following python code:

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 [])+[str(time.time())])
    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
['1675291378.118798']
['1675291378.118798']
['1675291378.118798']
None

This time using the update=True mode:

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 [])+[str(time.time())])
    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
['1675291406.785664']
['1675291406.785664', '1675291407.565578']
['1675291406.785664', '1675291407.565578', '1675291408.384397']
None

Features

@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 let you decide weather to update the session data each call or just not. 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

session_lambda-0.3.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

session_lambda-0.3.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file session_lambda-0.3.0.tar.gz.

File metadata

  • Download URL: session_lambda-0.3.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.9.5 Darwin/21.1.0

File hashes

Hashes for session_lambda-0.3.0.tar.gz
Algorithm Hash digest
SHA256 fdc83706bc6c2ea6ae256cfef97d02d94fd7f5b3b0753bec2156773c2b43f7c4
MD5 e3bd2b252c60e30db89115cd8bf70a8c
BLAKE2b-256 11d252853cbcd0e599fb6e0ee2c5876a79590565daff585e2cfcaec20f4d9b56

See more details on using hashes here.

Provenance

File details

Details for the file session_lambda-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: session_lambda-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.9.5 Darwin/21.1.0

File hashes

Hashes for session_lambda-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e1ff01933d699a88a35461639c8200f1da087a5eec63de175474c3f4e21d4835
MD5 08ac32f6bc5c38b7d3d339c5f725ccc4
BLAKE2b-256 e82de09a0b072fe515145c9a18a8ba12e711eb009d9e39c4d4e5cd171ba8bc7e

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page