Using AWS Lambda Layers for PHP

Until recently AWS Lambda did not support PHP, but with the advent of the runtime API and layers, now we able to implement an AWS Lambda runtime in any programming language including PHP. Each layer can contain libraries, a custom runtime, or other dependencies. You can create layers, or use layers published by AWS and other AWS customers.

There is a great practical example of creating a Custom Runtime for PHP if you want to build a PHP runtime layer by yourself. In this article, we'll use partner supported PHP layer for Lambda from stackery for a quick understanding of its use.

1. Create a Lambda function from scratch.

aws-lambda-php-layer-01

2. Click "Layers" and then "Add a layer"

aws-lambda-php-layer-02

3. Choose "Provide a layer version ARN". Here we need to specify an ARN.

You can choose the desired version here. Currently, the latest available Lambda Layer Version ARNs for PHP 7.3 and 7.1 are:

arn:aws:lambda:<region>:887080169480:layer:php73:3

arn:aws:lambda:<region>:887080169480:layer:php71:10

aws-lambda-php-layer-03

4. Create index.php file and change handler to index.php.

The content of our function is:

<?php
echo "Hello from the AWS Lambda!";
phpinfo();

aws-lambda-php-layer-04

5. Add API Gateway trigger with the HTTP API template.

aws-lambda-php-layer-05

6. Now we have the API endpoint which will trigger our function.

aws-lambda-php-layer-07

7. Click on the API endpoint link and you'll see the result of our function

aws-lambda-php-layer-08

What's next?

I recommend reading the article "AWS Lambda Custom Runtime for PHP: A Practical Example" to deep dive in creating custom runtime for PHP and also watching video from re:Invent 2018 "AWS Lambda Layers, the Runtime API, and Nested Applications"

Комментарии