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

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 key of 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_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

Additional Inforamtion

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.6.tar.gz (6.1 kB view hashes)

Uploaded Source

Built Distribution

session_lambda-0.3.6-py3-none-any.whl (7.3 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