SilverStripe DynamoDB integration.
This module enables storing SilverStripe sessions in DynamoDB.
- SilverStripe 4.0+
- PHP 5.6+
Add these custom repositories to your composer.json
composer require silverstripe/dynamodb
If you wish to store sessions in DynamoDB, set the required environment variables in
// the name of the DynamoDB table to store sessions in AWS_DYNAMODB_SESSION_TABLE=mysession // the region that the DynamoDB table will live in (in this example here it uses Sydney) AWS_REGION_NAME=ap-southeast-2
Once these are in place, this module will configure DynamoDB and register that as the session handler.
Before you can actually use this, you need to create a table in which to store the sessions. This can be done through the AWS Console for Amazon DynamoDB, or using the SDK. When creating the table, you should set the primary key to
id of type
Sometimes you'll want to test that DynamoDB sessions work on your local development environment. You can make that
happen by defining
AWS_SECRET_KEY. Please don't define these constants in the environment file
in EC2 instances, as credentials are automatically handled by the IAM role inside of AWS.
// the AWS access key and secret. This is optional if you've configured an instance with an IAM role // http://docs.aws.amazon.com/aws-sdk-php/guide/latest/credentials.html#caching-iam-role-credentials AWS_ACCESS_KEY=my-access-key AWS_SECRET_KEY=my-secret
Inactive sessions are garbage collected by
GarbageCollectSessionCronTask if silverstripe-crontask
is setup on your instance. The time when a session should be collected after inactivity can be changed by setting
For example, in your application's config YAML file, this sets a 20 minute session timeout:
Session: timeout: 1200
You can also set the DynamoDB garbage collection time independently of
Session::$timeout, but it's recommended you
make it at least the value of
Session::$timeout or greater.
For example, in your
_ss_environment.php file, set garbage collection after 1 hour of inactivity in sessions:
See https://docs.aws.amazon.com/aws-sdk-php/v3/guide/service/dynamodb-session-handler.html for more information.
You can simulate DynamoDB locally for easier development through DynamoDB Local.
Set environment constants. Note that actual access keys and regions are ignored, they just need to be defined.
AWS_DYNAMODB_SESSION_TABLE=mysession AWS_ACCESS_KEY=my-access-key AWS_SECRET_KEY=my-secret AWS_DYNAMODB_ENDPOINT=http://localhost:8000 AWS_REGION_NAME=us-east-1
Download DynamoDB Local
and start it - it'll be available under
Now use the AWS CLI Tools to interact with your local DynamoDB.
aws dynamodb create-table --table-name mysession --attribute-definitions AttributeName=id,AttributeType=S --key-schema AttributeName=id,KeyType=HASH --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 --endpoint-url http://localhost:8000
aws dynamodb list-tables --endpoint-url http://localhost:8000
List all sessions:
aws dynamodb scan --table-name mysession --endpoint-url http://localhost:8000
Delete all sessions (use create table to reset afterwards):
aws dynamodb delete-table --table-name mysession --endpoint-url http://localhost:8000
Do you want to contribute? Great, please see the CONTRIBUTING.md guide.
This module is released under the BSD 3-Clause License, see license.md.
See the separate CHANGELOG.md
When having discussions about this module in issues or pull request please adhere to the SilverStripe Community Code of Conduct.