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:

from session_lambda import session, set_session_data, get_session_data

@session
def lambda_handler(event, context):
    
    session_data = get_session_data()

    # core_logic(event, context, session_data)
        
    set_session_data(data="hello world")
    
    return {"session_data_before": session_data, "session_data_after": get_session_data()}
    
        
print(lambda_handler({'headers':{"session-id": "0"}}, {}))
print(lambda_handler({'headers':{"session-id": "0"}}, {}))
print(lambda_handler({'headers':{"session-id": "1"}}, {}))

You should get the following prints:

{'session_data_before': None, 'session_data_after': 'hello world'}
{'session_data_before': 'hello world', 'session_data_after': 'hello world'}
{'session_data_before': None, 'session_data_after': 'hello world'}

Features

@session(id_key_name='session-id', return_session_id_in_header=True, update=False)
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.
  • return_session_id_in_header flag lets you control is the session-id is added to the response's headers (if headers exists in response). It is default to True.
  • ttl is seconds interval for the session to live. 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.2.0.tar.gz (4.5 kB view hashes)

Uploaded Source

Built Distribution

session_lambda-0.2.0-py3-none-any.whl (6.0 kB view hashes)

Uploaded Python 3

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