Introduction: Why Serverless and PHP?
\n\nFor a long time, it was believed that PHP and serverless computing were incompatible. However, with the advent of custom AWS Lambda runtimes and the development of tools like Bref, the situation has changed dramatically. Today, you can run PHP applications in a fully managed AWS Lambda infrastructure, paying only for the actual code execution time.
In this article, we will break down how to create, configure, and deploy a Serverless function in PHP using AWS Lambda. You will learn about key concepts, typical issues, and best practices. The material is intended for PHP developers familiar with AWS basics, but we will try to explain everything in as much detail as possible.
\n\nBy the end of this guide, you will be able to launch your first PHP endpoint in the cloud without a single dedicated server.
\n\n1. Architecture of Serverless PHP on AWS Lambda
\n\n1.1. How does AWS Lambda execute PHP?
\n\nThe native Lambda environment does not directly support PHP. To run PHP code, a special intermediary layer is used — the Custom Runtime. It is an executable file (usually in Python or Bash) that launches the PHP interpreter, passes an event to it, and returns the result.
\n\nThe most popular tool for this is Bref (Bare-metal PHP for AWS Lambda). It provides a ready-made PHP image with optimized settings for a serverless environment.
\n\nThe basic process looks like this:
\n- \n
- The client sends an HTTP request to API Gateway. \n
- API Gateway converts the request into a JSON event and passes it to Lambda. \n
- Lambda launches the Custom Runtime, which initializes PHP-FPM. \n
- The PHP script processes the event and returns a response. \n
1.2. Key components of the stack
\n- \n
- AWS Lambda — the compute unit. \n
- API Gateway — the HTTP interface for invoking the function. \n
- Bref — the PHP runtime for Lambda. \n
- Serverless Framework (optional) — a deployment tool. \n
- Amazon S3 / DynamoDB — data storage (if needed). \n
2. Step-by-step setup: from project to deployment
\n\n2.1. Installing tools
\nFor this, you will need:
\n- \n
- PHP 8.1+ with the
mbstring,curl,jsonextensions. \n - Composer — dependency manager. \n
- Node.js and npm (for Serverless Framework). \n
- An AWS account with a configured IAM user. \n
Install the Serverless Framework globally:
\nnpm install -g serverless\n\n2.2. Creating a project with Bref
\nCreate a project directory and initialize it:
\nmkdir php-lambda-demo\ncd php-lambda-demo\ncomposer require bref/bref\n\nCreate a file index.php — this will be our function:
<?php\nrequire __DIR__ . '/vendor/autoload.php';\n\nreturn function ($event) {\n $name = $event['queryStringParameters']['name'] ?? 'World';\n \n return [\n 'statusCode' => 200,\n 'headers' => ['Content-Type' => 'text/plain'],\n 'body' => "Hello, {$name}! This is Serverless PHP on AWS Lambda."\n ];\n};\n\nNote: the function must return an array with the keys statusCode, headers, and body. This is the standard response format for integration with API Gateway.
2.3. Configuring serverless.yml
\nCreate a file serverless.yml in the project root:
service: php-lambda-demo\n\nprovider:\n name: aws\n region: eu-central-1\n runtime: provided.al2\n\nplugins:\n - ./vendor/bref/bref\n\nfunction