I was inspired by two events to jump back into serverless framework.
Firstly, I attended the second London serverless meetup yesterday evening which was excellent and showed just how much enthusiasm there is for serverless architectures. Check out their new logo on the left. It was significant that each of the three speakers announced that they are actively hiring serverless developers.
Secondly, Stolz has contributed improvements to my sample project for integrating PHP into the serverless framework. It’s the purpose of this blog post to cover the changes.
The trick to getting AWS lambda to support PHP is to bundle in a PHP binary so that nodejs can call it with
child_process.spawn(). In my first implementation, I used an Ubuntu docker base image to compile and produce the php binary. Unfortunately, this is not identical to the container that AWS Lambda uses and so sometimes the logs would contain errors such as:
1 2 3 4 5
In my experience, these errors were often not fatal, but the correct approach is to build the php binary from a base image which is closer to the one lambda uses. So instead of my docker file starting with
FROM ubuntu, it now starts with
FROM amazonlinux. Also, with this image, I can use
yum to install other dependencies like
libpng-devel. So the new docker build script for producing the php binary looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
If you run this with
$ sh dockerfile.buildphp
It will use docker to overwrite the php binary which will get shipped when you deploy with
sls deploy. And this time, there are no more libcurl errors. All the code is on Github.