RR as AWS Lambda

RoadRunner can run PHP as an AWS Lambda function.

PHP Worker

The PHP worker does not require any specific configuration to run inside a Lambda function. We can use the default snippet with an internal counter to demonstrate how workers are reused:

handler.php
<?php
/**
 * @var Goridge\RelayInterface $relay
 */
use Spiral\Goridge;
use Spiral\RoadRunner;

ini_set('display_errors', 'stderr');
require __DIR__ . "/vendor/autoload.php";

$worker = RoadRunner\Worker::create();
$psr7 = new RoadRunner\Http\PSR7Worker(
    $worker,
    new \Nyholm\Psr7\Factory\Psr17Factory(),
    new \Nyholm\Psr7\Factory\Psr17Factory(),
    new \Nyholm\Psr7\Factory\Psr17Factory()
);

while ($req = $psr7->waitRequest()) {
    try {
        $resp = new \Nyholm\Psr7\Response();
        $resp->getBody()->write("hello world");

        $psr7->respond($resp);
    } catch (\Throwable $e) {
        $psr7->getWorker()->error((string)$e);
    }
}

Name this file handler.php and put it in the root of your project. Make sure to run:

Application

We can create a simple application to demonstrate how it works:

  1. You need three files: main.go with the Endure container:

  1. plugin.go with the plugin implementation:

  1. A config file, which can be embedded into the binary using the embed package:

Here you can take full advantage of RoadRunner: you can include any plugin here and configure it with the embedded config (within reasonable limits).

To build and package your Lambda function, run:

You can now upload and invoke your handler using a simple string event.

Repository with the full example

Notes

There are multiple notes to acknowledge:

  • Start with one worker per Lambda function to control your memory usage.

  • Make sure to include the environment variables listed in the code to properly resolve the location of the PHP binary and its dependencies.

Last updated

Was this helpful?