Deploying PHP Docker containers to AWS Lambda

Jan Richter
4 min readDec 13, 2020

In the past few years, most full-stack developers adopted Docker as part of their day-to-day engineering process. While PHP was never an officially supported language in AWS Lambda, the new announcement from Re:Invent 2020 makes it much simpler to deploy PHP code.

This means that you can leverage the power of serverless without having to adopt new abstractions such as the Serverless framework or Lambda layers.

Bear in mind that the release is only a couple of days old and the support is limited. You can currently deploy containers to Lambda in US East (N. Virginia), US East (Ohio), US West (Oregon), Asia Pacific (Tokyo), Asia Pacific (Singapore), Europe (Ireland), Europe (Frankfurt), South America (São Paulo).

Getting started

You need to have a working AWS account (ideally with Administrator access), you will also need to have Docker, PHP, Composer and AWS CLI installed on your machine.

Pricing

Deploying cloud workloads can be tricky when it comes to costs. In this example, we will be using Lambda and ECR which shouldn’t exceed $1/month (depending on the size of your image and Lambda invocations). Nonetheless, make sure you have a budget in place.

Creating a new project

Make a new directory for your project and run Composer to initialise it.

composer init

After a few prompts, you should end up with a composer.json file.

Lambda requires a Runtime Interface Client to be able to execute your code. For this, we can leverage an existing project: Bref. Install the dependency using Composer:

composer require bref/bref

Adding our custom code

Create a new file e.g. function.php in the root directory. This will serve as an entry point to your application.

Custom PHP function for AWS Lambda

Now that our logic is added, we can focus on the Dockerfile :

Docker image for AWS Lambda

Creating a repository for Docker images

We need a place to store our Docker images. This can be done in AWS by using Elastic Container Registry (ECR).

Log in to the AWS console and navigate to ECR. Make sure you’re using one of the supported regions mentioned above.

Creating a repository in AWS ECR

Click on Create repository and follow the steps. You can use the defaults for everything.

Pushing the image

Click on the repository you just created and view the push commands for your platform:

Push commands for AWS ECR

Open a terminal in your project’s directory and follow the commands mentioned in the AWS console:

Note: This step can take quite a long time since you’ll be downloading and pushing ~2 GB of data.

Creating a Lambda function

While in the AWS Console, navigate to Lambda functions

Create a new function and select Container Image as the option:

Choose the function’s name and browse images:

Choose the repository you created earlier and select the latest image. Keep the defaults for everything else, the function will take about 1 minute to create.

Testing the function

When the function is ready, click on the alias to open the function’s details:

Select the Test tab and click on Invoke. Don’t worry about the event payload since our code doesn’t work with it.

Once the invocation is complete, you can see the result showing our Hello from Lambda :

That’s it! You have successfully executed a Lambda function running PHP code and packaged using a Docker image.

Conclusion

While running all your PHP code in Lambda might be tempting, most PHP applications are designed to work with a web server. You can read more about what suits your use case in the maturity matrix for serverless PHP by Bref.

If you’re interested in a more in-depth guide about the new Docker with AWS Lambda, I highly recommend this post by James Beswick where he covers a way to use AWS SAM to test and deploy functions locally.

--

--