Deploy a PHP Application on AWS Lambda Function Serverless
Wall Script
Wall Script
Wednesday, June 21, 2023

Deploy a PHP Application on AWS Lambda Function Serverless

In this post, I will explain the steps to deploy a simple PHP email MX record validation application on AWS lambda function using Bref layers. AWS Lambda natively supports Java, Go, PowerShell, Node. js, C#, Python, and Ruby code, not PHP. You can deploy the lite weight PHP function to improve the application performance and it will reduce cost compared with the EC2 instance. AWS is offering the first one million requests free and you need an AWS account with a payment setup. The only disadvantage is that port 25(mail/SMTP) will not support it.

Deploy a PHP Application on AWS Lambda Function Serverless

GitHub Source

  • AWS Account
  • Composer
  • PHP
  • VS Code IDE

Create a Directory
Create a directory with your project name.
$mkdir phpLambdaFunction
$cd phpLambdaFunction

Make sure to install composer software on your local machine. The following composer code contains bref/bref.
"name": "srinivas/php-lambda-function",
"description": "PHP lambda function",
"require": {
"bref/bref": "^2.0"
"authors": [
"name": "Srinivas Tamada",
"email": "[email protected]"

Install Plugins
Use the following command to download the PHP bref dependencies.
composer install

You will find the following results on the terminal
No composer.lock file present. Updating dependencies to the latest instead of installing from the lock file. See for more information.

Loading composer repositories with package information

Updating dependencies

Lock file operations: 10 installs, 0 updates, 0 removals

  - Locking bref/bref (2.0.8)

  - Locking crwlr/query-string (v1.0.3)

  - Locking hollodotme/fast-cgi-client (v3.1.7)

  - Locking nyholm/psr7 (1.8.0)

  - Locking psr/container (2.0.2)

  - Locking psr/http-factory (1.0.2)

  - Locking psr/http-message (1.1)

  - Locking psr/http-server-handler (1.0.2)

  - Locking riverline/multipart-parser (2.1.1)

  - Locking symfony/process (v6.3.0)

Writing lock file

Installing dependencies from lock file (including require-dev)

Nothing to install, update or remove

Generating autoload files

3 packages you are using are looking for funding.

Use the `composer fund` command to find out more!

Here the following function validates input email MX domain records.
function validateEmail($email)
// Step 1: Check the email address format
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
return false;
// Step 2: Extract the domain from the email address
$domain = substr(strrchr($email, "@"), 1);
// Step 3: Check the MX records of the domain
$mxRecords = [];
if (getmxrr($domain, $mxRecords) === false) {
return false;
return true;

validateEmail('[email protected]'); // TRUE
validateEmail('[email protected]'); // FALSE
validateEmail('srinivas.tamada'); // FALSE

Create a main index file and include the validateEmail and autoload.php will load the bref dependencies. Here the following return function reads the post-request data and responds with a valid status code.
require __DIR__ . '/vendor/autoload.php';

require 'validateEmail.php';

return function ($event) {
// Receive post body data from the request object
$postData = json_decode($event['body'], true);
// Default values for negative status
$response = [
'status' => false,
$statusCode = 500;
// Getting the email value from the post data
// Calling validateEmail method to validate mx email
if ($postData && isset($postData['email']) && validateEmail($postData['email'])) {
$response['status'] = true;
$statusCode = 200;

return [
'statusCode' => $statusCode,
'headers' => [
'Content-Type' => 'application/json',
'body' => json_encode($response),

Project File Structure
Here compose install the vendor dependency folder and created a lock file.
PHP Lambda Function file structure

Step 1: Create a Lambda Function
You have the launch the AWS Lambda and you find an option to create a function. You have to give the function name and choose the following runtime.
PHP Create a Lambda Function
Note: Click on the image to zoom it.

Step 2: Advance Settings
Recommended setting for Lambda function. But the following options support AWS_IAM authentication.
PHP Lambda Function Advance Settings

For testing you can choose NONE to enable the function url and hit create function button. We can turn off the latter.
PHP Lambda Function Advance Settings

Step 3: Function Overview
Here you will the function url with ARN details for AWS_IAM. Now click on the layers.
PHP Lambda Function Function Overview

Step 4: Layers
Layers will add additional support to the AWS lambda function.
PHP Lambda Function Layers

Step 5: Add a Layer
Here you have to define the ARN(Amazon Resource Name) URL. Choose to specify an ARN option.
PHP Lambda Function Add a layer

Step 6: Select Bref Runtimes
Open a new browser tab and launch Here you will find the PHP layer versions. Select the better PHP version, based on your function needs.
PHP Lambda Function bref runtime

Step 7: Update Layer ARN
Paste the layer url and hit add button.
PHP Lambda Function update layer ARN

Step 8: Layer Update
Now you will the layer got added.
PHP Lambda Function layer update

Step 9: Code Source
Scroll down and find the default code source.
PHP Lambda Function Code Source

Step 10: Upload your PHP code
Select upload from and choose from .zip file.
PHP Lambda Function zip file

Step 11: Create a PHP Project Zip file
Go to your PHP project directory and use the following command to zip the source code.
zip -r9q .

You will find the ZIP file here.
PHP Lambda Function

Step 12: Upload the
Upload file and click save button.
PHP Lambda Function upload zipe

Step 13: PHP Code Source
Now you will find the PHP email validation source code.
PHP Lambda Function php source

Step 14: Runtime Setting
Scroll down and find the runtime settings. You have to update the handler default hello.handler to index.php
PHP Lambda Function runtime setting

Step 15: Update Runtime Handler with index.php
Update with index.php and choose runtime Amazon Linux 2.
PHP Lambda Function Update Runtime Handler with index.php

Step 16: Function URL
You will find the function url.
PHP Lambda Function

Step 17: Postman or Thunder Client
You need a restful api client software to execute endpoints. If you are using VS Code, install the Tunder Client extension.
PHP Lambda Function

Step 19: Test the PHP Lambda Function Positive
PHP restful endpoint is ready. Use the function url and post request the email in JSON format. Here you will notice the response status TRUE with 200
PHP Lambda Function

Step 20: Test the PHP Lambda Function Negative
Here you will notice the response status False with 500 for an invalid email domain.
PHP Lambda Function

Function ARN
AWS is recommending to use of function ARN with AWS_IAM. I will explain in my upcoming article.
PHP Lambda Function

Update and deploy
Use the code source editor and update the application code for any modification. Once ready just click on deploy for changes.
PHP Lambda Function

Disable Function URL
I would recommend disabling the function URL. It will charge per request once the free trial expires. Go to configuration and select Function Url edit the options.
PHP Lambda Function

Change it back to AWS_IAM.
PHP Lambda Function

Function URL Forbidden
Function URL should respond back with forbidden access.
PHP Lambda Function Function URL Forbidden

Delete Lambda Function
If you are not using it, better to delete the function. It will not add any additional cost.
PHP Lambda Function Delete Lambda Function

Monitor Function Logs
AWS Lambda is offering a dashboard to monitor function logs.
PHP Lambda Function Monitor Function Logs

web notification


mailxengine Youtueb channel
Make in India