Serverless Functions in PHP: AWS Lambda from A to Z — Complete Guide

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

Introduction: Why Serverless and PHP Are Not an Oxymoron



For a long time, it was believed that PHP and serverless architecture were incompatible. PHP was associated with eternal Apache or Nginx processes, while serverless was associated with Node.js or Python. However, with the advent of tools like Bref and official support for Custom Runtimes in AWS Lambda, PHP has gained a second wind.



Serverless functions allow you to run PHP code without managing a server. You pay only for the actual execution time (down to the millisecond). This is an ideal solution for:

  • Webhook handlers (Telegram, Stripe, Slack);
  • Microservices with low load;
  • Scheduled tasks (cron) via CloudWatch Events;
  • API proxies and lightweight backends.


In this article, we will break down how to set up PHP on AWS Lambda from scratch: from creating IAM roles to deploying a working function.



1. PHP Architecture in AWS Lambda



How Custom Runtime Works

AWS Lambda natively supports only Java, Go, PowerShell, Node.js, C#, Python, and Ruby. To run PHP, we use a Custom Runtime — a special executable file (bootstrap) that communicates with the Lambda Runtime API.



The most popular way is the Bref framework. It comes with a ready-made Layer containing PHP 8.x, Composer, and a set of standard extensions.



// Project structure for Brefproject/├── index.php          // Entry point├── composer.json├── serverless.yml     // Deployment configuration└── vendor/


Three Deployment Methods

  1. Bref (recommended) — the simplest. Uses serverless.yml and ready-made layers.
  2. Manual build — you compile PHP for Amazon Linux 2 and package it into a ZIP.
  3. Docker image — you use a Lambda Container Image (supported since 2020).


We will look at the first option — it suits 90% of tasks.



2. Step-by-Step Setup: From Installation to the First Request



Step 1. Environment Preparation

Make sure you have installed:

  • PHP 8.0+ (locally)
  • Composer
  • Node.js and npm (for Serverless Framework)
  • The aws-cli utility with configured credentials


Install the Serverless Framework globally:

npm install -g serverless


Step 2. Create a Project

composer create-project bref/bref my-php-lambdacd my-php-lambda


After initialization, you will see the serverless.yml file. Let's edit it:



service: my-php-app

provider: name: aws region: eu-central-1 # Choose the nearest region runtime: provided.al2 # Amazon Linux 2

plugins: - ./vendor/bref/bref

functions: hello: handler: index.php description: 'Simple PHP function' layers: - ${bref:layer.php-80} # Layer with PHP 8.0


Step 3. Write the Function Itself

Open index.php:



<?php

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

/** * Lambda function handler * @param array $event Input data from AWS (API Gateway, SQS, etc.) * @return array Response in JSON format */function handler(array $event): array{ // Log to CloudWatch error_log('Request received: ' . json_encode($event));

$name = $event['queryStringParameters']['name'] ?? 'Guest';

return [ 'statusCode' => 200, 'headers' => [ 'Content-Type' => 'application/json', ], 'body' => json_encode([ 'message' => "Hello, {$name}!", 'timestamp' => time(), ]), ];}


Blogs

Book Recommendations