Build your own PHP runtime layer for AWS Lambda.

We have had custom layers for a long time in Lambda, I have used them for a few mini app I have personally as well as professionally. Then along comes this official AWS Post on the (L)ambda(A)piGateway(M)ySQL(P)hp stack.

I host a few Wordpress sites one of which is DevCOP on LightSail. LightSail has served me incredibly well however it would be an interesting challenge to move the site to Lambda and Serverless Aurora.

But first things first how can we create an deploy a PHP layer and some sample PHP code to run in lambda without using one of the pre-built packages and why would you want to do this. I suspect the main reason you might want to do this yourself is to well….do it yourself. It helps to understand what is happening and how the pieces come together.

In this example we will:

  • Create a Cloud9 instance
  • Install on that instance the development tools and compilers to build openSSL and PHP
  • Compile/Build OpenSSL and PHP
  • Install via composer vendor libraries require for the layer to execute PHP
  • download a bootstrap file for lambda
  • Zip up the PHP runtime
  • Zip up the PHP vendor layer
  • Deploy both layers using the AWS CLI
  • Create a Lambda function that leverages these new layers
  • Create a simple php program to add 2 numbers together
  • Deploy API gateway to invoke the lambda function.
  • Deploy a simple front-end application to consume the API to demonstrate PHP in Lambda as an invokable endpoint.

# Requirements:

- An AWS account

This example can be completed 100% in the AWS Console, via click ops and cli commands.

Steps:

Create a Cloud9 instance:

Navigate to: https://ap-southeast-2.console.aws.amazon.com/cloud9/home?region=ap-southeast-2

Click “Create Environment”

Name the environment:

We are going to use this instance to compile source code, it will compile just fine with the smaller instance however it will take a little longer, as I will terminate this environment after this experiment I will use the biggest/fastest as it will only be active for less than 1 hour. Ensure “Amazon Linux” is selected in platform.

Confirm:

The steps we are going to run are taken from the AWS Announcement blog. It is handy to copy/paste them to a file on the Cloud9 instance to avoided jumping between tabs.

The full script is below. Open the REDME.md file in Cloud9, delete all the content and paste this in.

Update for above use this for the bootstrap source: https://github.com/aws-samples/php-examples-for-aws-lambda/blob/master/0.1-SimplePhpFunction/bootstrap

Once these steps are completed we now have the required lambda layers in our AWS account to run PHP. To confirm what have been created navigate to:

https://ap-southeast-1.console.aws.amazon.com/lambda/home?region=ap-southeast-1#/layers Take note of the region in the URL and ensure this is the region you are operating in.

We will see 2 new layers:

Let’s now create a sample PHP lambda function. From the “Layers” interface click “Functions”, then “Create Function”. Give your function a name. For “Runtime” select “Provide you own bootstrap”. If you have a role for Lambda select that otherwise select “Create a new role with basic Lambda permissions”. Then click “Create Function”

Once the function is created we need to add the 2 layers we created.

Add the layers in order, the PHP run time first then the vendor layer as below: Please not you will have to use the ARN of your layers not those in the image:

Once both layers are added scroll down until you see “Basic Settings” click edit.

We will change the handler to simply say “index”:

We are now ready for some php code! We will use the in page editor, remove the existing 3 files ( right click and delete ) create a folder “src”, in the source folder create a new file “index.php” This is the file called when we change the handler to “index”.

Write some php code or just copy and paste this sample.

Click “Save” then click “Test”.

We will have to create a test event, edit to provide a key “user” with the value “{yourname}:

Scroll up and click “Test”

In part 2 we will explore the bootstrap file and how to invoke the Lambda function with API Gateway.

For the full Video walkthrough click and watch here: