Serverless PHP: AWS Lambda and Alternatives for Backend Without a Server

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

PHP Without a Server: Myth or Reality?

\n\n

When you hear the phrase "Serverless functions," the first association is usually Node.js or Python. PHP seems like a monolith that lives on an eternal Apache or Nginx. But times are changing. Today, PHP in a serverless architecture is not an exotic concept but a working tool for thousands of projects. You can write microservices, process webhooks, resize images, and even run full-fledged APIs without thinking about servers, scaling, or paying for idle resources.

\n\n

In this article, we'll break down how PHP on AWS Lambda works, what alternatives exist (Bref, OpenFaaS, Vercel), and most importantly, write real code that you can run immediately. If you're a PHP developer looking to step into modern cloud-native development, you've come to the right place.

\n\n

What is Serverless and Why Does a PHP Developer Need It?

\n\n

Serverless (or FaaS — Functions as a Service) is a model where you upload your code, and the provider decides when and how to run it. You only pay for the actual execution time. No virtual machines, no "Apache" consuming memory 24/7. For PHP, which was long considered a "language for hosting with FTP," this is a breath of fresh air.

\n\n

Benefits for PHP projects:

\n
    \n
  • Cost savings — no need to maintain an entire server for a single script.
  • \n
  • Auto-scaling — Lambda automatically creates copies of the function under increased load.
  • \n
  • Ease of deployment — upload the code, and it works.
  • \n
  • Microservices approach — you can break a monolith into small functions.
  • \n
\n\n

The Problem: PHP Doesn't Support Serverless Out of the Box

\n\n

Classic PHP is not a daemon. It starts, executes a script, and dies. For Lambda, this is a problem: the function must be "cold" and start in milliseconds. Regular PHP starts slowly (loading the framework, autoloading, configs). The solution is Bref.

\n\n

Bref — The Main Tool for Serverless PHP

\n\n

Bref is an open-source SDK and runtime that turns PHP into a native Lambda function. It compiles PHP into a static binary (via bref/bref), providing instant cold starts. Bref supports Laravel, Symfony, Slim, and any custom code.

\n\n

How Does Bref Work?

\n

You write a regular PHP class, implement the Bref\\Context\\Context interface, and Bref handles HTTP processing. For an API, one function is enough.

\n\n

Example: Serverless API in PHP for Generating QR Codes

\n

Suppose we need to receive a POST request with text and return a QR code in PNG format. Let's write the function:

\n\n
<?php\n// handler.php\nrequire __DIR__ . '/vendor/autoload.php';\n\nuse Bref\\Context\\Context;\nuse Bref\\Event\\Http\\HttpHandler;\nuse chillerlan\\QRCode\\QRCode;\nuse chillerlan\\QRCode\\QROptions;\n\nclass QrGenerator extends HttpHandler\n{\n    public function handleRequest($request, Context $context): array\n    {\n        $body = json_decode($request->getBody()->getContents(), true);\n        $text = $body['text'] ?? 'Hello, Serverless!';\n        \n        $options = new QROptions([\n            'outputType' => QRCode::OUTPUT_IMAGE_PNG,\n            'imageBase64' => false,\n        ]);\n        \n        $qrcode = new QRCode($options);\n        $imageData = $qrcode->render($text);\n        \n        return [\n            'headers' => ['Content-Type' => 'image/png'],\n            'statusCode' => 200,\n            'body' => base64_encode($imageData),\n            'isBase64Encoded' => true,\n        ];\n    }\n}\n\nreturn new QrGenerator();
\n\n

Deploying to AWS Lambda via Bref

\n

Install the Bref CLI and run:

\n
composer require bref/bref\n./vendor/bin/bref init\n./vendor/bin/bref deploy
\n

That's it. In a minute, you'll have an HTTPS endpoint,

Blogs

Book Recommendations