Creating a Lambda Layer for running PHP on Lambda
- A lambda layer is a bundle that can be uploaded and used with multiple lambda functions.
- This method of putting PHP in to a layer is similar to the old trick of creating a single binary enterpreter which gets called from a shim in another language.
- Claire Liguori of AWS has created a great tool called img2lamba which will convert a Docker Image to a Lambda Layer.
- The method below is derived from the example included with the img2lambda project
Determine version of Amazon Linux currently in use with Lambda
- We need to build our version of PHP on the same version of Amazon Linux used with Lambda.
- Create a simple Lambda function to cat the contents of
/etc/*release*, which will contain the Amazon Linux version.
Lambda is currently using Amazon Linux AMI 2018.03
Build PHP and the lambda-php image.
- The Dockerfile will use AmazonLinux to build PHP.
- The Dockerfile will take the lambci/lambda:provided image and install PHP from the build image.
- The final image will be our lambda-php image which willbe turned in to a layer.
Test Lambda Functions
- Pass a JSON string with the variable name set and the script will parrot the name back as:
- Same as hello.php, but with
goodbye, NAME instead of
Build the docker image
Test the custome lambda runtime by running the hello function:
Convert Docker image to Lambda layer and upload to AWS
Use SAM to package lambda functions in
function/src and deploy to AWS
Two functions will now exist:
Create a Test Event with a data input of: