Skip to content

szabacsik/aws-php-lambda-cloudformation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP Lambda Example

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.

Project Overview

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.

Project Structure

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

Dependencies

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

Makefile Commands

The project includes a comprehensive Makefile with commands to manage the entire lifecycle of the Lambda function:

Build and Deployment

  • make build: Builds the project by updating dependencies and creating a ZIP file for deployment
  • make upload: Uploads the ZIP file to the S3 bucket
  • make code-update: Builds the project and updates the Lambda function code without changing the configuration
  • make clean: Removes the ZIP file and vendor directory

S3 Bucket Management

  • make bucket-create: Creates the S3 bucket if it doesn't exist
  • make bucket-check: Checks if the S3 bucket exists
  • make bucket-delete: Deletes the S3 bucket and all its contents

CloudFormation Stack Management

  • make stack-deploy: Deploys the CloudFormation stack
  • make stack-update: Updates the CloudFormation stack
  • make stack-delete: Deletes the CloudFormation stack
  • make stack-describe: Describes the CloudFormation stack
  • make stack-describe-events: Shows the CloudFormation stack events

IAM Role Management

  • make role-create: Creates the IAM role for Lambda execution
  • make role-attach-basic-policy: Attaches the basic execution policy to the IAM role
  • make role-detach-all-policies: Detaches all policies from the IAM role
  • make role-delete: Detaches all policies and deletes the IAM role

Testing and Monitoring

  • make invoke: Invokes the Lambda function with a test payload
  • make logs-tail: Tails the CloudWatch logs for the Lambda function

Getting Started

Follow these steps to deploy and test the PHP Lambda function:

Prerequisites

  1. AWS CLI installed and configured with appropriate credentials
  2. PHP 8.x installed
  3. Composer installed

Deployment Steps

  1. Create the IAM role:

    make role-create
    make role-attach-basic-policy
    
  2. Create the S3 bucket:

    make bucket-create
    
  3. Build and upload the Lambda function:

    make build
    make upload
    
  4. Deploy the CloudFormation stack:

    make stack-deploy
    
  5. Test the Lambda function:

    make invoke
    
  6. View the logs:

    make logs-tail
    

Updating Your Lambda Function

If you modify the code in index.php or add your own business logic:

  1. Make your changes to the code
  2. Run make code-update to build and update the Lambda function

For more substantial changes that require updating the CloudFormation stack:

  1. Make your changes to the code and/or AWS configuration files
  2. Run make build and make upload to prepare the new code
  3. Run make stack-update to update the CloudFormation stack

Cleaning Up

To remove all resources created by this project:

  1. Delete the CloudFormation stack:

    make stack-delete
    
  2. Delete the IAM role:

    make role-delete
    
  3. Delete the S3 bucket:

    make bucket-delete
    
  4. Clean up local files:

    make clean
    

Customizing the Project

To adapt this project for your own use:

  1. Modify index.php to implement your business logic
  2. Update composer.json to add any additional dependencies
  3. Adjust the AWS configuration files in the aws/ directory as needed
  4. Important: Make sure to update the AccountId parameter in the aws/template.yaml file with your own AWS account ID
  5. Update the Makefile variables if you want to change names or regions

Conclusion

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.

About

PHP based Lambda function on AWS

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published