Deploying PHP Web Applications on AWS Lambda Using Bref

Tirush Venamadala
KloudMate

--

Deploying PHP Applications on Serverless using AWS Lambda

Serverless made it possible for developers to focus on their application code instead of managing and operating infrastructures. The most basic Serverless architectures can be built using as little as a couple of static web technologies; And AWS Lambda, being the leading serverless hosting solution, offers native support for various such technologies, both inside and outside of Amazon Web Services. Dynamic frameworks such as PHP provide world-class support for building small and large web products, however, there’s no native support for PHP in AWS Lambda. That isn’t to say that PHP developers can’t join forces with serverless on AWS. PHP can be used with AWS Lambda using an open-source composer package called Bref.

Bref provides PHP runtimes for AWS Lambda and PHP frameworks integration to help PHP developers deploy their applications on AWS and run them using AWS Lambda.

Here’s a demonstration of setting up PHP websites on AWS Lambda and API Gateway

AGENDA: Developing or packaging a sample PHP application locally, or on a remote server

$ sudo apt-get update

STEP 1: Next, install software-properties-common package. It allows you to easily manage the independent software sources.

$ sudo apt -y install software-properties-common

## which will give you all your versions of PHP:

$ sudo add-apt-repository ppa:ondrej/php$ sudo apt-get update$ sudo apt-get install -y php7.4-cli php7.4-json php7.4-common php7.4-mysql php7.4-zip php7.4-gd php7.4-mbstring php7.4-curl php7.4-xml php7.4-bcmath
  • Note that we have installed multiple packages of PHP to serve different purposes:
  • php7.4-cli: This is the command interpreter, useful for testing PHP scripts from a shell or performing general shell scripting tasks
  • php7.4-json: To work with JSON data
  • php7.4-common: For documentation purposes
  • php7.4-mysql: To work with MySQL database
  • php7.4-zip: To work with compressed files
  • php7.4-gd: To work with images
  • php7.4-mbstring: To manage non-ASCII strings
  • php7.4-curl: Lets you make HTTP requests in PHP
  • php7.4-xml: To work with XML data
  • php7.4-bcmath: To work with precision floats

STEP 2: List all the modules installed

$ php -m$ php --version

STEP 3: Install the composer locally

$ php -r “copy(‘https://getcomposer.org/installer', ‘composer-setup.php’);”$ php -r “if (hash_file(‘sha384’, ‘composer-setup.php’) === ‘756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3’) { echo ‘Installer verified’; } else { echo ‘Installer corrupt’; unlink(‘composer-setup.php’); } echo PHP_EOL;”$ php composer-setup.php$ php -r “unlink(‘composer-setup.php’);”$ php composer.phar — version

Step 4: We use php composer.phar in order to run the composer

$ sudo mv composer.phar /usr/local/bin/composer

$ composer — version

Step 5: Generate a sample project using the composer

$ composer create-project laravel/laravel example-app (Creates the app project)$ cd example-app (Change the working directory)$ php artisan serve (Check the web application running on EC2)

Step 6: Install Bref using the composer

$ composer require bref/laravel-bridge:dev-master or composer require bref/bref$ php artisan vendor:publish — tag=serverless-config (Generated serverless.yml file can be modified accordingly)

Step 7: Grant permissions to create resources on AWS such as Lambda functions, and API Gateway using serverless.yml

Step 8: Run the following command in the root directory where serverless.yml is present.

$ serverless deploy or sls deploy
  • Copy the output URL of the API Gateway endpoint and paste it on browser

Conclusion

Even though PHP is one of the most widely used languages, native runtimes on AWS Lambda only supports Nodejs, Python, Go, Java, etc…

Bref package has opened up the gates to serverless development for PHP developers. With these couple of extra steps, they can also leverage the automatic scalability and flexibility that comes from going serverless services. As for the observability & monitoring of these serverless applications, there are purpose-built platforms such as KloudMate that offer end-to-end visibility into the applications hosted on AWS.

A big thanks to my co-writer, Akanksha Rana for helping with structuring and drafting this one!

--

--