In order to use PHP on AWS Lambda you need to bundle the PHP binary, including the required libraries, into the package. This serverless example project includes the necessary binary to run a simple PHP script.
See my blog post here.
Install this serverless project. It will create a new serverless-php folder.
serverless install --url https://github.com/ZeroSharp/serverless-php
Check the serverless.yml
file and modify region and stage if necessary.
sls deploy
sls invoke local --function hello
sls invoke --function hello
If you need different compiler flags or dependencies you will need to recompile PHP.
To do this, we have to compile the PHP 7.2.2 with statically linked libraries:
sh buildphp.sh
The default is to use the PHP 7.2.2 branch to compile the PHP binary.
To switch the PHP version you can set the branch PHP_VERSION_GIT_BRANCH parameter in buildphp.sh
line 8.
Thanks to:
- Danny Linden who worked out most of the hard stuff here.
- Robert Anderson who provided the original GitHub repo.
- Stolz for the binaries update.