Package: pyramid-dynamodb-sessions
pyramid-dynamodb-sessionspyramid-dynamodb-sessionsstores user sessions for Pyramid in DynamoDB.
DynamoDB sessions offer large storage (up to 400kb per session), confidentiality (the user can't see the contents of the session) and low operational overhead.MotiviationPyramid ships with cookie-based sessions by default.
This is a good option for many applications, but has several downsides:Data is unencrypted and can be be read by the user.Cookies are limited to 4kb of data.Redis-backed sessions is another popular option, but requires operating a Redis server.DynamoDB can store up to 400kb of data per session and requires no server management.
With On-Demand Capacitiy, you don't have to do any capacity planning and only pay for what you use.
You can also opt for Provisioned Capacity as a way to optimize costs.Getting StartedBefore getting started, you should be familiar with AWS IAM and know how to grant permissions to your application.To get started, first create a new DynamoDB table.
The table should have a hash key ofsidas a binary type.
Time-to-live should be enabled on theexpattribute.
You can use the following AWS CLI command to create the table.awsdynamodbcreate-table\--table-namemytable\--attribute-definitionsAttributeName=sid,AttributeType=B\--key-schemaAttributeName=sid,KeyType=HASH\--billing-mode=PAY_PER_REQUEST
awsdynamodbupdate-time-to-live\--table-namemytable\--time-to-live-specification"Enabled=true, AttributeName=exp"To enable pyramid-dynamodb-sessions in your application, first make sure your application has the following permissions for your newly created table:dynamodb:GetItemdynamodb:PutItemdynamodb:UpdateItemdynamodb:DeleteItemNext set your session factory to apyramid_dynamodb_sessions.DynamoDBSessionFactoryobject.frompyramid.configimportConfiguratorfrompyramid_dynamodb_sessionsimportDynamoDBSessionFactorydefapp(config,**settings):config=Configurator(session_factory=DynamoDBSessionFactory('mytable'),)config.make_wsgi_app()And that's it!
When your application adds data to the session, it will be persisted to DynamoDB and a unique session ID will be stored in the users cookie.ConfigurationDynamoDBSessionFactoryrequires only one argument on initialization:  the table name.
You can also pass aboto3 Table object.
This is useful if you want control over the configuration of Boto3.Additional arguments mostly deal with the session ID cookie and roughly match the arguments forPyramid's built-in cookie sessions.table—  The DynamoDB table to use.
Can be a string or a boto3 Table object.cookie_name— The name of the cookie used to store the session ID.
Defaults tosession_id.max_age—  The expiration time for the cookie in seconds.
Defaults toNone(session-only cookie).path— The path for the cookie.  Defaults to/.domain—  The domain for the cookie.
Defaults to no domain.secure— If true, sets thesecureflag for the cookie.
Defaults toNone, which will set the flag if the request is made via HTTPS.httponly—  If true, hide the cookie from Javascript by setting theHttpOnlyflag.
Defaults to true.samesite— The SameSite property for the cookie, orNoneto disable the SameSite option.
Defaults toStrict.timeout— The number of seconds of inactivity before the session times out.
Defaults to1200(20 minutes).reissue_time— The number of seconds before the session is "reissued," meaning that the activity timeout is reset.
Reissuing performs a write to DynamoDB, so it is recommended to set this to a reasonably high value, such as 1/10 of the timeout.
Defaults to120(2 minutes).