Going serverless with PHP

Kpicaza
PHP FAD
Published in
4 min readJun 8, 2020

I am sure that you have listened to talk about serverless architecture and lambda functions, in this post I will try to explain what it is, its pros and cons, the current support for PHP and some reasons or cases to use it.

What is Serverless architecture?

Serverless architecture is a technology that allows managing applications without the need for maintaining its server, giving us the option to scale application on demand out of the box.

Serverless, doesn’t mean that we don’t need a server anymore, it means that the Cloud provider will have the responsibility to manage the server.

Like every technology, it has a few trade-offs to take in mind before to apply it, here goes a small list of its benefits and weaknesses:

Pros:

  • Costs: The primary advantage of the serverless architecture compared to classic server infrastructure, is that with the Serverless, you only pay for the resources you use in execution time. There are no hardware costs, and you don’t pay while the application is not in use.
  • Scalability: The scalability is a core feature of the serverless technology providers. It scales up and down depending on the service demand and requesting location.
  • Focus on business domain and features: Without having the responsibility os maintaining server and without having to care about scalability, we can set the focus on our business domain and features that improves our time to market as developers.

Cons:

  • Limitations: The cloud provider has its limitations, in some features like application size, execution time, disk writing, and so on. It also can change its prices, terms of service, or in the worst case, close serverless service.
  • Cold Starts: The serverless applications have a concept named “Cold Start” these means that when the application is not running in a portion of time, the machine that executes our code can fall in something like repose, giving us a delay on starting the app the first time. You can find different benchmarks measuring the bootstrap time between different programming languages with a simple Google search.

Current status of serverless in PHP

Unfortunately for us as PHP developers, the mayor serverless service providers don’t support our language natively, but fortunately, the PHP community already has created at least two different options to run PHP applications as serverless lambda functions.

If you are a Laravel user, you are lucky because they have launched the Vapor platform that allows you to run Laravel applications as Lambda functions. On the other hand, when you prefer some other framework, or you need to implement more specific use cases, you are lucky too, thanks to Mattieu Napoli’s(@matthieunapoli) excellent work done, developing the Bref framework.

When to use serverless with PHP?

This question has not an absolute answer. You have to decide when the serverless architecture fits your requirements or not. I can enumerate some use cases that surely can suppose an improvement in cost and performance:

  • Queued jobs: You can save machine resources by running queued jobs as lambda functions. Further, if the queue runs in parallel, you take a high-performance boost because you can run as many jobs as workers in parallel you demand, without needing big machines.
  • Landing pages: When you are involved in a massive marketing campaign that required high availability of some sections of a website in various moments of a day. Running these landing pages as lambda functions, you may save a lot of extra work and money because you won’t have to need to over-scale your infrastructure, nor then undersize it after the campaign finishes.

Using popular frameworks as lambda functions:

Bref is a framework running on top of the Serverless framework. It facilitates the developer a toolset to convert a classic PHP app into a lambda function hosted in AWS.

It uses the `serverless.yml` file to configure:

The PHP version, the PHP extensions, function handlers, API endpoints, the connections with other resources, private and public networks that we can access to, the deployment artifact configuration, AWS availability regions, and all other stuff needed to configure a lambda function deployment and execution, of a PHP application.

As I said before, Laravel users can choose between using Vapor or using Bref. For the others, we have to use Bref.

Using Symfony and Larabel as a serverless application is already well documented in the Bref docs.

To use Mezzio or Antidot framework, you can follow the Readme of this Github repository.

Vapor is a full-featured cloud platform built expressly for Laravel framework, I don’t know many aspects of it(I’m not daily Laravel user), but I can tell you that it has comprehensive documentation.

Conclusion:

To conclude this post, we can say that serverless architecture is another tool that we have to know and learn about, It can help us with some approaches, and we have to know when they are.

On the other hand, we can tell that the PHP community has made the labor that the industry didn’t want to do, and it did great. We have ready to run PHP on lambda functions out of the box with the principal PHP frameworks, and we can run many others with a little workaround.

--

--