AWS’s announcement of Lambda Layers means big things for those of us using serverless in production. The creation of set components that can be included with any number of Lambdas means you no longer have to zip up your application code and all its dependencies each time you deploy a serverless stack. This allows you to include dependencies that are much more bespoke to your particular serverless environment.
In order to enable Stackery customers with Layers at launch, we took a look at Lambda Layers use cases. I also decided to go a bit further and publish a layer that enables you to write a Lambda in PHP. Keep in mind that this is an early iteration of the PHP runtime Layer, which is not yet ready for production. Feel free to use this Layer to learn about the new Lambda Layers feature and begin experimenting with PHP functions and send us any feedback; we expect this will evolve as the activity around proof of concepts expands.
PHP is a pure computing language and you can use to emulate the event processing syntax of a general-purpose Lambda. But really, PHP is used to create websites, so Chase’s implementation maintains that model: your Lambda accepts API gateway events and processes them through a PHP web server.
Configure your function as follows:
Runtime
to provided
aws lambda list-layer-versions --layer-name arn:aws:lambda:<your 3. region>:887080169480:layer:php71
arn:aws:lambda:<your region>:887080169480:layer:php71:<latest version>
If you are using AWS SAM it's even easier! Update your function:
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
...
Runtime: provided
Layers:
- !Sub arn:aws:Lambda:${AWS::Region}:887080169480:layer:php71
Now let’s write some Lambda code!
<?php
header('Foo: bar');
print('Request Headers:');
print_r(getallheaders());
print('Query String Params:');
print_r($_GET);
?>
Hi!
The response you get from this code isn’t very well formatted, but it does contain the header information passed by the API gateway:
If you try anything other than the path with a set API endpoint response, you’ll get an error response that the sharp-eyed will recognize as being from the PHP web server, which as mentioned above is processing all requests
Layers can be shared between AWS accounts, which is why the instructions above for adding a layer works: you don’t have to create a new layer for each Lambda. Some key points to remember:
Stackery can improve every part of the serverless deployment process, and Lambda Layers are no exception. Stackery makes it easy to configure components like your API gateway.
Stackery also has integrated features in the Stackery Operations Console, which lets you add layers to your Lambda:
Lambda Layers offers the potential for more complex serverless applications making use of a deep library of components, both internal to your team and shared. Try adding some data variables or a few npm modules as a layer today!