This project demonstrates how to run PHP applications on AWS Lambda. It serves as a proof of concept that PHP can indeed be used with AWS Lambda, contrary to common misconceptions that PHP is not scalable or that AWS doesn't support PHP for serverless functions.
This example application provides a basic framework for building PHP-based Lambda functions on AWS. It uses the Bref framework, which enables PHP applications to run on AWS Lambda by providing custom runtime layers. The project includes all the necessary configuration files and scripts to deploy, test, and manage a PHP Lambda function.
php-lambda/
├── aws/ # AWS configuration files
│ ├── iam-policy/ # IAM policy templates
│ ├── lambda-trust-policy.json # Trust policy for Lambda execution role
│ ├── parameters-test.json # CloudFormation parameters for test environment
│ └── template.yaml # CloudFormation template for Lambda function
├── src/ # Source code directory (for your business logic)
├── vendor/ # Composer dependencies
├── composer.json # PHP dependencies configuration
├── index.php # Lambda function handler
├── Makefile # Automation scripts
└── README.md # This file
The project relies on the following PHP packages:
- bref/bref: Framework for running PHP applications on AWS Lambda
- monolog/monolog: Logging library for PHP
- bref/monolog-bridge: Integration between Monolog and Bref for better logging in AWS Lambda
The project includes a comprehensive Makefile with commands to manage the entire lifecycle of the Lambda function:
make build: Builds the project by updating dependencies and creating a ZIP file for deploymentmake upload: Uploads the ZIP file to the S3 bucketmake code-update: Builds the project and updates the Lambda function code without changing the configurationmake clean: Removes the ZIP file and vendor directory
make bucket-create: Creates the S3 bucket if it doesn't existmake bucket-check: Checks if the S3 bucket existsmake bucket-delete: Deletes the S3 bucket and all its contents
make stack-deploy: Deploys the CloudFormation stackmake stack-update: Updates the CloudFormation stackmake stack-delete: Deletes the CloudFormation stackmake stack-describe: Describes the CloudFormation stackmake stack-describe-events: Shows the CloudFormation stack events
make role-create: Creates the IAM role for Lambda executionmake role-attach-basic-policy: Attaches the basic execution policy to the IAM rolemake role-detach-all-policies: Detaches all policies from the IAM rolemake role-delete: Detaches all policies and deletes the IAM role
make invoke: Invokes the Lambda function with a test payloadmake logs-tail: Tails the CloudWatch logs for the Lambda function
Follow these steps to deploy and test the PHP Lambda function:
- AWS CLI installed and configured with appropriate credentials
- PHP 8.x installed
- Composer installed
-
Create the IAM role:
make role-create make role-attach-basic-policy -
Create the S3 bucket:
make bucket-create -
Build and upload the Lambda function:
make build make upload -
Deploy the CloudFormation stack:
make stack-deploy -
Test the Lambda function:
make invoke -
View the logs:
make logs-tail
If you modify the code in index.php or add your own business logic:
- Make your changes to the code
- Run
make code-updateto build and update the Lambda function
For more substantial changes that require updating the CloudFormation stack:
- Make your changes to the code and/or AWS configuration files
- Run
make buildandmake uploadto prepare the new code - Run
make stack-updateto update the CloudFormation stack
To remove all resources created by this project:
-
Delete the CloudFormation stack:
make stack-delete -
Delete the IAM role:
make role-delete -
Delete the S3 bucket:
make bucket-delete -
Clean up local files:
make clean
To adapt this project for your own use:
- Modify
index.phpto implement your business logic - Update
composer.jsonto add any additional dependencies - Adjust the AWS configuration files in the
aws/directory as needed - Important: Make sure to update the
AccountIdparameter in theaws/template.yamlfile with your own AWS account ID - Update the Makefile variables if you want to change names or regions
This project demonstrates that PHP can be effectively used with AWS Lambda for serverless applications. By leveraging the Bref framework and proper AWS configuration, PHP applications can be deployed as scalable, serverless functions on AWS.
Feel free to use this project as a starting point for your own PHP-based Lambda functions.