Custom Runtimes with PHP

Let's use layers to include a PHP runtime, provided by https://bref.sh/ (find the layers at here).

As of this video, the latest Layer (appropriate for our use case) is arn:aws:lambda:us-east-2:209497400698:layer:php-81:17.

1mkdir foo-func-php
2cd foo-func-php
3 
4# Create project, add in dependency `brev/brev`
5# Equivalent to running "composer require brev/brev"
6composer init

We then can code up the PHP Lambda function. Just like with NodeJS, it's pretty simple!

1<?php
2 
3require __DIR__ . '/vendor/autoload.php';
4 
5use Bref\Context\Context;
6 
7return function ($event, Context $context) {
8 echo json_encode(["info" => "this is going to cloudwatch"]);
9 
10 return json_encode(["message" => sprintf("Hello, %s!", $event['name'] ?? "unknown")]);
11};

Then we can package it up and create our function with the IAM Role and Layer pre-defined.

1# Zip it up
2zip -r9q index.zip .
3 
4# Create the function with a PHP runtime layer
5# Note that handler is the file name for PHP runtimes
6aws --profile cloudcasts lambda create-function \
7 --function-name foo_func_php \
8 --role arn:aws:iam::601364042938:role/service-role/foo_func_go-role-h6pfawno \
9 --handler index.php \
10 --runtime provided.al2 \
11 --memory-size 512 \
12 --layers "arn:aws:lambda:us-east-2:209497400698:layer:php-81:17" \
13 --zip-file fileb://index.zip
14 
15# Invoke it!
16aws --profile cloudcasts lambda invoke \
17 --function-name foo_func_php \
18 --invocation-type RequestResponse \
19 --cli-binary-format raw-in-base64-out \
20 --payload '{"name": "Chris"}' \
21 output.json
22 
23cat output.json

This works as expected!

Don't miss out

Sign up to learn when new content is released! Courses are in production now.