Serverless Functions in PHP: AWS Lambda — A Practical 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

Serverless Functions in PHP: AWS Lambda — From Theory to Practice



Modern web development is increasingly moving away from traditional monolithic servers in favor of cloud computing. Serverless architecture allows developers to focus solely on code without worrying about infrastructure management. AWS Lambda is one of the most popular services for implementing this approach. However, for a long time it was believed that PHP was not suitable for serverless. This is a myth. In this article, we will look at how to run PHP functions on AWS Lambda, what tools are needed for this, and how to optimize performance.



What is AWS Lambda and how to fit PHP into it?



AWS Lambda is a compute service that runs your code in response to events and automatically manages compute resources. You pay only for the actual execution time and the number of requests. Lambda natively supports Node.js, Python, Java, Go, and .NET, but PHP is not included in this list "out of the box". However, this is not a problem: we can use Custom Runtimes or ready-made wrappers such as Bref.



Bref is an open-source project that provides a ready-made Runtime for PHP on AWS Lambda, as well as integration with frameworks (Laravel, Symfony). With it, you can write event handlers, API endpoints, or even full-fledged web applications running in serverless mode.



Main components for running PHP on Lambda:

  • Bref Runtime — a layer containing the PHP interpreter and necessary extensions.
  • Event — a trigger that invokes the function (HTTP request, file upload to S3, change in DynamoDB).
  • Handler — a PHP function or class method that receives and processes the event.


Environment setup and first example



To work, we will need Composer, Docker (for building), and an AWS account. Let's install Bref:



composer require bref/bref


Bref uses a Docker image for local testing and building. After installation, let's create a serverless.yml file — the configuration for deploying to AWS Lambda.



Example serverless.yml configuration:

service: my-php-function

provider: name: aws region: us-east-1 runtime: provided.al2

plugins: - ./vendor/bref/bref

functions: hello: handler: index.php layers: - ${bref:layer.php-81} events: - httpApi: 'GET /hello'


Now let's write a simple handler — the file index.php:



<?php

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

return function ($event) { $name = $event['queryStringParameters']['name'] ?? 'World'; return [ 'statusCode' => 200, 'body' => "Hello, $name!", ];};


This code returns a JSON response with a greeting. To run it locally, use the command:



vendor/bin/bref local hello


Deployment and triggers: from HTTP to S3



After local testing, you can deploy the function to the cloud. Make sure you have configured AWS credentials (via AWS CLI or environment variables). Run:



serverless deploy


In the console, you will see the URL of your API Gateway (if an HTTP trigger is configured). Now you can access the function via a browser or curl.



Other popular triggers for PHP functions:

  • S3 Events — processing file uploads/deletions.
  • SQS/SNS — asynchronous processing of message queues.
  • CloudWatch Events — scheduled execution (cron).
  • DynamoDB Streams — reacting to changes in

Blogs

Book Recommendations