Skip to main content

Managing PHP Sessions with DynamoDB

Install AWS SDK

composer require aws/aws-sdk-php  

Create Table on DynamoDB

Create a table named "sessions" on DynamoDB

Required permissions

Min required permissions listed below. Create a permission like below and attach it to DynamoDB Table

{  
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"dynamodb:GetItem",
"dynamodb:UpdateItem",
"dynamodb:DeleteItem",
"dynamodb:Scan",
"dynamodb:BatchWriteItem"
],
"Effect": "Allow",
"Resource": "arn:aws:dynamodb:region:account_id:table/sessions"
}
]
}

Implementation

Add below code block before session_start()

include_once __DIR__ . '/vendor/autoload.php';

use Aws\DynamoDb\DynamoDbClient;
use Aws\DynamoDb\SessionHandler;

$client = new DynamoDbClient([
'region' => 'gj-oceanplanet-1',
'version' => 'latest'
]);

$sessionHandler = SessionHandler::fromClient($client, [
'table_name' => 'sessions',
'hash_key' => 'id',
'data_attribute' => 'data',
'data_attribute_type' => 'string',
'session_lifetime' => 3600,
'session_lifetime_attribute' => 'expires',
'consistent_read' => true,
'locking' => false,
'batch_config' => [],
'max_lock_wait_time' => 10,
'min_lock_retry_microtime' => 5000,
'max_lock_retry_microtime' => 50000,
]);

$sessionHandler->register();

Garbage Collection

Setup a TTL attribute in your DynamoDB table, using the attribute ‘expires’. This will automatically garbage collect
your sessions and avoid the need to garbage collect them yourself.

Source:

https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/service_dynamodb-session-handler.html

Unknown (2022-08-13 20:52:54)
#aws