PHP functions

The simplest way to write a lambda is to write one in the form of a PHP function:


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

lambda(function ($event) {
    return 'Hello ' . ($event['name'] ?? 'world');

This form is very similar to lambdas written in other languages, for example JavaScript:

exports.myHandler = async function (event, context) {
   return "Hello " +;

The function

A function can be defined by calling Bref's lambda() function and passing it a callable. The callable can be an anonymous function or any kind of callable supported by PHP.


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

lambda(function ($event) {
    return /* response */;

The function:

  • takes an $event parameter which contains data from the event that triggered the function (list of examples here)
  • can optionally return a response: the response must be serializable to JSON

There must be only one function defined per PHP file.


The function is invoked with the $event parameter as well a $context parameter that can be optionally declared if you want to use it:


use Bref\Context\Context;

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

lambda(function ($event, Context $context) {
    return /* response */;

The Context object is inspired from the context parameter in other languages and provides information about the current lambda invocation (the request ID, the X-Ray trace ID, etc.).

serverless.yml configuration

Below is a minimal serverless.yml to deploy a function. To create it automatically run vendor/bin/bref init.

service: app
    name: aws
    runtime: provided
    - ./vendor/bref/bref
        handler: index.php
            - ${bref:layer.php-73}

The runtime to use is php. To learn more check out the runtimes documentation.


A PHP function must be invoked via the AWS Lambda API. If you instead want to write a classic HTTP application read the HTTP guide.


A PHP function can be triggered manually from the CLI using the serverless invoke command:

$ serverless invoke -f <function-name>
"Hello world"

To pass event data to the lambda use the --data option. For example:

serverless invoke -f <function-name> --data='{"name": "John" }'

Run serverless invoke --help to learn more about the invoke command.

From PHP applications

A PHP function can be triggered from another PHP application using the AWS PHP SDK:

$lambda = new \Aws\Lambda\LambdaClient([
    'version' => 'latest',
    'region' => <region>,

$result = $lambda->invoke([
    'FunctionName' => '<function-name>',
    'InvocationType' => 'RequestResponse',
    'LogType' => 'None',
    'Payload' => json_encode(/* event data */),

$result = json_decode($result->get('Payload')->getContents(), true);