Developing software in the Real World

Using img2lambda to publish your Serverless PHP layer

This interesting tweet by Clare Liguori came to my attention last week:

Img2lambda tweet2

This new img2lambda tool will take the layers of a Docker container and convert them to AWS layers for use in Lambda.

I poked around with Clare’s example and updated my lambda-php project in order to understand how it works. I also rewrote my runtime’s bootstrap to make it clearer.

The clever thing from my point of view is that you can build your PHP runtime layer locally using Docker and then publish the layers to AWS for use in your Lambda functions. This means you can now use the layer in different projects and have them all reference the same PHP runtime.

The magic of Docker

This is all done with the magic of Docker. Specifically, we create a Dockerfile that creates to containers:

  • A container to build the PHP binary
  • A container containing the layers that img2lambda will upload to AWS

Simplified, it looks like this:

The first part download and compiles the PHP binary and puts it into /runtime/bin/php and also copies in the bootstrap files required to make the layer act as a Lambda runtime.

The second part simply copies all the files in /runtime in the first container into a new container. This new container containers a single layer which is our PHP runtime.

To build it we do:

The tag name can be anything.

Create the the layer in AWS

To create the AWS layer we use img2lambda:

This tool will find the layer in the runtime container and upload it to eu-west2 and then store the identifier in output/layers.json which looks like this.

Using in Serverless Framework

You can take the ARN number and use it for your actions in serverless.yml like this:

This is the way that we can re-use our PHP runtime in all our actions across multiple projects.

Serverless also allows you to import data from JSON files into your serverless.yml file, so you can also do:

This has the advantage that when you re-build and upload PHP runtime layer, your project picks it up, which could also be convenient.

Testing locally

One of the benefits of building the runtime in Docker is that we can run it locally for testing our Lambda function. As our runtime container inherits from lambci/lambda:provided, we get an environment that looks very much like Lambda itself.

To test locally, we use another container and inherits from our runtime one and copies in the PHP files for our function. The Dockerfile looks something like this:

We create our container:

And then we can test it:

To get the output:

Very convenient.

Summary

I really like the separation of publishing the PHP runtime layer from my project and the ability to reuse this layer in multiple projects is magic.

Ideally, I would like to be able to update the layer with a new patch release of PHP and use the ARN so that all my functions don’t need to be re-deployed. Maybe I’ll look at how that can be done at some point.

Have a play with my lambda-php project to see if this approach works for you. Also check out Bref for another way to run PHP on Lambda.

2 thoughts on “Using img2lambda to publish your Serverless PHP layer

Thoughts? Leave a reply

Your email address will not be published. Required fields are marked *