The serverless-php git repo contains a pre-compiled PHP executable which is uploaded to AWS Lambda as part of the FaaS deploy. However, and deliberately to keep it lightweight, many PHP extensions are not included in this PHP image. I thought I may get through my development without having to rebuild this image, but alas not! I decided to use the Twig template engine and that has a dependency on the hash() function. I realised something was afoot when my execution failed.
Fatal error: Uncaught Error: Call to undefined function hash() in /var/task/vendor/twig/twig/lib/Twig/Environment.php:272"
$ php -m | grep hash hash $
Thankfully the serverless-php repo contains two files to aid the recompilation of the PHP image - buildphp.sh which is a shell script to run the process, and dockerfile.buildphp which is the configuration for a docker container in which the actual recompilation will occur.
RUN ./configure \ --enable-static=yes \ --enable-shared=no \ --disable-all \ --enable-json \ --enable-libxml \ --enable-mbstring \ --enable-phar \ --enable-soap \ --enable-xml \ --enable-hash \ --with-curl \ --with-gd \ --with-zlib \ --with-openssl \ --without-pear \ --enable-ctype
$ ls -las php 49144 -rwxr-xr-x@ 1 nigel staff 25161497 2 Nov 14:03 php $ chmod +x buildphp.sh $ ./buildphp.sh Build complete. Don't forget to run 'make test'. ---> 7b602edcddb5 Removing intermediate container 6c3cd6936a6d Successfully built 7b602edcddb5 Successfully tagged php-build:latest 2bdff18d52adad839b6cbdba9ee55e3263d5399326175827398f757cc9c74c44 $
$ ls -las php 50736 -rwxr-xr-x 1 nigel staff 25976432 4 Nov 16:34 php
$ sls deploy
If we now run our function from the API Gateway or point a web browser on our assigned URL we get a 200 success! Yey!