DynamoDB events
To handle DynamoDB eventsย , extend the DynamoDbHandler class:
use Bref\Context\Context;
use Bref\Event\DynamoDb\DynamoDbEvent;
use Bref\Event\DynamoDb\DynamoDbHandler;
class MyHandler extends DynamoDbHandler
{
public function handleDynamoDb(DynamoDbEvent $event, Context $context): void
{
foreach ($event->getRecords() as $record) {
$keys = $record->getKeys();
$old = $record->getOldImage();
$new = $record->getNewImage();
// do something
}
}
}getKeys(), getOldImage() and getNewImage() return items in the DynamoDB attribute format, for example ['id' => ['S' => '123']]. To get plain PHP arrays, use the Marshaler class of the AWS SDK: (new \Aws\DynamoDb\Marshaler)->unmarshalItem($record->getNewImage()).
Then, create a Lambda function that listens to the DynamoDB stream of the table with the handler you created:
Laravel
serverless.yml
functions:
# ...
worker:
handler: App\MyHandler
runtime: php-84
events:
- stream:
type: dynamodb
arn: arn:aws:dynamodb:eu-west-1:111111111111:table/my-table/stream/2026-01-01T00:00:00.000The App\MyHandler class will be instantiated by Laravelโs service container.
The stream event does not create the stream: enable DynamoDB Streamsย on the table. Learn more about using DynamoDB in serverless.yml in the Serverless Framework documentationย .
Last updated on