Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
121 lines (87 sloc) 3.46 KB
title currentMenu introduction previous next
PHP functions
Learn how to run serverless PHP functions on AWS Lambda using Bref.
link title
What are runtimes?
link title
HTTP applications

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 (array $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 (array $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.

SAM configuration

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

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
        Type: AWS::Serverless::Function
            FunctionName: 'my-function'
            CodeUri: .
            Handler: index.php # the name of your PHP file
            Runtime: provided
                - 'arn:aws:lambda:<region>:209497400698:layer:php-73:<version>'

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 bref invoke command:

$ vendor/bin/bref invoke <function-name>
START RequestId: 3afca641-112c-11e9-bd8b-7108ee29dd18 Version: $LATEST
END RequestId: 3afca641-112c-11e9-bd8b-7108ee29dd18
REPORT RequestId: c42dce1e-112c-11e9-bed8-3f4bd1a349e2	Duration: 201.14 ms	Billed Duration: 300 ms 	Memory Size: 128 MB	Max Memory Used: 39 MB

"Hello world"

The logs will be displayed above the function's result encoded as JSON.

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

vendor/bin/bref invoke <function-name> --event='{"name": "John" }'

Run vendor/bin/bref 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);