UPDATE: This article has become out of date with the release of the Lambda Runtime API. I would also have a look at Bref for getting up and running with Serverless PHP.

Recently, I was lucky enough to have the chance to speak at my local PHP meetup and then later at DundeePHP. When given the chance to do a talk, I like to use it as an excuse to learn something new. Nothing focuses the mind like a deadline. A few ideas came and went but the idea that stuck was serverless, specifically FaaS (Functions as a Service). The talk has been and gone and I wanted to give the content life beyond the evening itself. I’ve written this article, summarising my thoughts and findings, to share a few of the key learnings with you and to hopefully help navigate the world of serverless PHP.

I remember when AWS Lambda was first released in 2014. I sent an email around the office as I was super excited about the possibilities it opened up. It seemed like a fresh way to write and run code. However, in the nearly 4 years since AWS Lambda was released, I never quite got around to trying it out.

The latest Digital Ocean quarterly report shows that “nearly half of developers fail to clearly understand what it [serverless] is.”. It also shows that serverless is still quite a bit behind in developer mindshare compared to the adoption and interest of containers. As with every new technology, there are some hyperbolic statements around serverless however, I am broadly excited about the benefits it provides and new possibilities it opens up compared to both traditional servers and even containers.

This article is mainly about using PHP in a serverless environment and I won’t delve into the details, pro/cons behind serverless. If you have not tried, or read much on, serverless I recommend you read this article by Mike Roberts. It does a much better job than I could hope to do of explaining serverless.

If you look at the big three cloud providers, Amazon, Google and Microsoft, they each provide their own FaaS solution. Lambda, Cloud Functions and Azure Functions respectively. If you take a look at the documentation, none provide support for PHP*. This is quite a disappointment if you are a PHP developer as the only official way you can run your code is to use another language. Obviously this is not possible in many situations, such as if you have a substantial amount of code already written in PHP, if you are in a team where the majority of team members are comfortable using PHP or simply, you quite like PHP.

I think one reason that we have yet to see PHP become available from these providers is due to PHPs unique execution model. PHP’s share nothing architecture is quite different when compared to other languages that are supported. Hopefully, we shall see at least one of the big three fully support PHP in the near future. Until then, we will need to think outside of the box in order to get our PHP code running in this new serverless world.

I will look at three different ways that we can run PHP on a serverless platform. I unfortunately did not have an endless pot of time so I chose to focus on AWS Lambda.