Server side sessions in Flask using AWS DynamoDB table as a data store
Project description
# Flask DynamoDB Sessions
[![](https://img.shields.io/pypi/v/flask-dynamodb-sessions.svg)](https://pypi.org/project/flask-dynamodb-sessions/) [![Updates](https://pyup.io/repos/github/ibejohn818/flask-dynamodb-sessions/shield.svg)](https://pyup.io/repos/github/ibejohn818/flask-dynamodb-sessions/)
Server-side sessions in Flask using AWS DynamoDB as the backend data store.
DynamoDB is AWS's SaaS NoSQL solution which makes it perfect for use as a session store.
Being a SaaS service we no longer have to manage servers/storage/etc and take advantage of some notable features such as:
- Auto-scaling
- Automatic Lifecycle ( Garbage collection )
- Encryption at rest
- etc...
Sessions are pickled and base64 encoded to be stored in DynamoDB as strings. As a result you may save
objects to your sessions as long as the object supports the pickle interface.
DynamoDB supports a maximum object size of 400 KB. Minus the UUID4 session id, modified date/time string and ttl timestamp
you have approximately 398 KB available for your session.
## Installation
```shell
# w/ pip
pip install flask-dynamodb-sessions
# w/ easy_install
easy_install flask-dynamodb-sessions
```
### Usage example
```python
from flask import (Flask, session)
from flask_dynamodb_sessions import Session
app = Flask(__name__)
# Set flask to use the dynamo session interface
Session(app)
@app.route('/', methods=['GET'])
def index_get():
# use sessions just as you normally would
session['user'] = {'username': 'jhardy'}
user = session.get('user')
session_id = session.sid
```
*View examples directory for more*
### Configuration Options
Below are additional `SESSION_*` configuration options specific to DynamoDB sessions.
SESSION_DYNAMODB_TABLE (string): The DynamoDB table to save to. Default: flask_sessions
SESSION_DYNAMODB_ENDPOINT (string): Override the boto3 endpoint, good for local development and using dynamodb-local. Default: None
SESSION_DYNAMODB_TTL_SECONDS (int): Number of seconds to add to the TTL column. Default: 86400 * 14 (14 Days)
The existing `SESSION_*` config parameters still apply (IE: cookie settings). SESSION_REFRESH_EACH_REQUEST
is the only setting that is negated and each request will refesh the cookie (Might be modified in a future release).
### Table Structure
The table structure is fairly simple.
```
{
id: string HASH,
modified: string DATETIME UTC
ttl: number UTC TIME + SESSION_DYANMODB_TTL_SECONDS
data: string JSON ENCODED SESSION
}
```
Create the table VIA `aws` cli.
```
aws dynamodb create-table --key-schema "AttributeName=id,KeyType=HASH" \
--attribute-definitions "AttributeName=id,AttributeType=S" \
--provisioned-throughput "ReadCapacityUnits=5,WriteCapacityUnits=5" \
--table-name flask_sessions
```
The `ttl` column is present to take advantage of DynamoDB's `Lifecycle` feature where dynamo will delete all rows with a ttl in the past.
Enable time-to-live (garbage collection)
```
aws dynamodb update-time-to-live --time-to-live-specification 'Enabled=true,AttributeName=ttl' --table-name flask_sessions
```
## TODO
- Test coverage
- More laxed cookie refresh
=======
History
=======
0.1.0 (2019-01-04)
------------------
* First release on PyPI.
[![](https://img.shields.io/pypi/v/flask-dynamodb-sessions.svg)](https://pypi.org/project/flask-dynamodb-sessions/) [![Updates](https://pyup.io/repos/github/ibejohn818/flask-dynamodb-sessions/shield.svg)](https://pyup.io/repos/github/ibejohn818/flask-dynamodb-sessions/)
Server-side sessions in Flask using AWS DynamoDB as the backend data store.
DynamoDB is AWS's SaaS NoSQL solution which makes it perfect for use as a session store.
Being a SaaS service we no longer have to manage servers/storage/etc and take advantage of some notable features such as:
- Auto-scaling
- Automatic Lifecycle ( Garbage collection )
- Encryption at rest
- etc...
Sessions are pickled and base64 encoded to be stored in DynamoDB as strings. As a result you may save
objects to your sessions as long as the object supports the pickle interface.
DynamoDB supports a maximum object size of 400 KB. Minus the UUID4 session id, modified date/time string and ttl timestamp
you have approximately 398 KB available for your session.
## Installation
```shell
# w/ pip
pip install flask-dynamodb-sessions
# w/ easy_install
easy_install flask-dynamodb-sessions
```
### Usage example
```python
from flask import (Flask, session)
from flask_dynamodb_sessions import Session
app = Flask(__name__)
# Set flask to use the dynamo session interface
Session(app)
@app.route('/', methods=['GET'])
def index_get():
# use sessions just as you normally would
session['user'] = {'username': 'jhardy'}
user = session.get('user')
session_id = session.sid
```
*View examples directory for more*
### Configuration Options
Below are additional `SESSION_*` configuration options specific to DynamoDB sessions.
SESSION_DYNAMODB_TABLE (string): The DynamoDB table to save to. Default: flask_sessions
SESSION_DYNAMODB_ENDPOINT (string): Override the boto3 endpoint, good for local development and using dynamodb-local. Default: None
SESSION_DYNAMODB_TTL_SECONDS (int): Number of seconds to add to the TTL column. Default: 86400 * 14 (14 Days)
The existing `SESSION_*` config parameters still apply (IE: cookie settings). SESSION_REFRESH_EACH_REQUEST
is the only setting that is negated and each request will refesh the cookie (Might be modified in a future release).
### Table Structure
The table structure is fairly simple.
```
{
id: string HASH,
modified: string DATETIME UTC
ttl: number UTC TIME + SESSION_DYANMODB_TTL_SECONDS
data: string JSON ENCODED SESSION
}
```
Create the table VIA `aws` cli.
```
aws dynamodb create-table --key-schema "AttributeName=id,KeyType=HASH" \
--attribute-definitions "AttributeName=id,AttributeType=S" \
--provisioned-throughput "ReadCapacityUnits=5,WriteCapacityUnits=5" \
--table-name flask_sessions
```
The `ttl` column is present to take advantage of DynamoDB's `Lifecycle` feature where dynamo will delete all rows with a ttl in the past.
Enable time-to-live (garbage collection)
```
aws dynamodb update-time-to-live --time-to-live-specification 'Enabled=true,AttributeName=ttl' --table-name flask_sessions
```
## TODO
- Test coverage
- More laxed cookie refresh
=======
History
=======
0.1.0 (2019-01-04)
------------------
* First release on PyPI.
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
Built Distribution
File details
Details for the file flask-dynamodb-sessions-0.1.4.tar.gz
.
File metadata
- Download URL: flask-dynamodb-sessions-0.1.4.tar.gz
- Upload date:
- Size: 17.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f4c0569cad255af6283249e4b9179c2b3744c7a05aced514ab7a97be78817eb8 |
|
MD5 | d8fff55ab706567e3e11e7b71ba5bcde |
|
BLAKE2b-256 | a5b02aa17a5c9af70e9ea74d8ebb0da384b001c8f13fec281d173961cdb47c6f |
File details
Details for the file flask_dynamodb_sessions-0.1.4-py2.py3-none-any.whl
.
File metadata
- Download URL: flask_dynamodb_sessions-0.1.4-py2.py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9e9640d51f2d4ed1deaf079c72673f16029db42a7b8a89bd48d8501dc404479e |
|
MD5 | 5a5af5a188e9f904a96f8f89ef35deca |
|
BLAKE2b-256 | 78c1d148841d20fac48cfd5eb788e5d6d0b4999af218de4350daa7c8a325ab36 |