未加星标

Serverless PHP on AWS Lamda

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二05 | 时间 2019 | 作者 红领巾 ] 0人收藏点击收藏

Like, Simon Wardley , I think that serverless computing is an interesting space because the billing is granular (pay only when your code executes) and you don’t need to worry about maintaining and provisioning servers or containers. So much so, that I maintain the Open Sourcephp Runtime for Apache OpenWhisk which is available commercially as IBM Cloud Functions

There are other serverless providers, and AWS Lambda is the market leader, but until recently PHP support could most charitably described as cumbersome . That all changed at the end of 2018 with Lambda’s new runtime API and support for layers .

Let’s look at the practicalities of serverless PHP on Lambda with Serverless Framework .

TL;DR

The source code for a simple Hello World is in my lambda-php-sls-hello-world Github repository. Just follow the Notes section and you should be good to go.

PHP runtime

The runtime API allows for any runtime to be used with Lambda. In some ways it looks a bit like the way OpenWhisk runtimes work in that there’s an HTTP API between the serverless platform and the runtime. One very obvious difference is that with Lambda, the runtime calls back to the platform to get its invocation data whereas OpenWhisk calls an endpoint that the runtime must implement. More details are in Michael Moussa’s article on the AWS blog , which inspired my work.

To get back on track, we need a PHP runtime for Lambda! This will comprise the PHP binary, the code to invoke our PHP serverless function and a bootstrap file as required by the platform. We put these three things into a layer. Layers are re-usable across accounts, so I’m quite surprised that AWS doesn’t provide a PHP one for us. Stackery do, but they aren’t using PHP 7.3, so we’ll build our own.

We’ll put all the files in the layer/php directory in our project.

Building the PHP binary

We need a PHP binary that will run inside Lambda’s containers. The easiest way to do this is to compile it on the same platform as Lambda, so we use EC2. Michael’s article explains how to do it and so I turned those commands into a compile_php.sh script, so that I could copy it up to the EC2 instance, run it & then copy the binary back to my computer:

$ export AWS_IP=ec2-user@{ipaddress} $ export SSH_KEY_FILE=~/.ssh/aws-key.rsa $ scp -i $SSH_KEY_FILE compile_php.sh $AWS_IP:doc/compile_php.sh $ ssh -i $SSH_KEY_FILE -t $AWS_IP "chmod a+x compile_php.sh && ./compile_php.sh 3.7.0" $ scp -i $SSH_KEY_FILE $AWS_IP:php-7-bin/bin/php layer/php/php

This makes it nicely repeatable and hopefully it will be fairly simple to update to newer versions of PHP.

Bootstrapping

As we are using the runtime API, we need a bootstrap file. This filename is required by Lambda and is responsible for invoking the function by making relevant API calls in a while loop.

Essentially, we need to sit in a loop and call the /next endpoint to find out what to invoke, invoke it and then send the response to the /response endpoint.

AWS provides an example in BASH using curl :

while true do HEADERS="$(mktemp)" # Get an event EVENT_DATA=$(curl -sS -LD "$HEADERS" -X GET "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/next") REQUEST_ID=$(grep -Fi Lambda-Runtime-Aws-Request-Id "$HEADERS" | tr -d '[:space:]' | cut -d: -f2) # Execute the handler function from the script RESPONSE=$($(echo "$_HANDLER" | cut -d. -f2) "$EVENT_DATA") # Send the response curl -X POST "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/$REQUEST_ID/response"-d "$RESPONSE" done

We want to do the same thing in PHP and while I could write it myself, Parikshit Agnihotry has already done so in PHP-Lambda-Runtime/runtime.php , so we’ll use that and copy it into layer/php/runtime.php . I made a couple of changes to my version , so that it does the json_encoding and also to add better error handling.

The layer/php/bootstrap file is very simple as all it needs to do is run the PHP binary with this file:

#!/bin/sh cd $LAMBDA_TASK_ROOT /opt/php /opt/runtime.php

That’s it. We now have three files in layer/php :

php the PHP executable runtime.php The runtime API worker bootstrap The Lambda required boostrap stub

These will become our PHP Layer in our Lambda application and when copied into the container on invocation, will be in /opt .

Set up Serverless Framework

Serverless Framework allows repeatable configuration and deployment of a serverless application. I’m a fan of this concept and want to use tools like this more. We’ll use it for our PHP Hello World.

As there’s no handy Serverless Framework tempate for PHP applications, we’ll just create a serverless.yml file in our project directory.

Firstly, the basics:

service: php-hello-world provider: name: aws runtime: provided region: eu-west-2 memorySize: 128

We name our application php-hello-world and we’re using AWS as our provider . As I’m in the UK, I set the region to London & we don’t need much memory, so 128MB is enough.

The runtime is usually the language that you want your function to be executed in. To use the runtime API which will execute our bootstrap stub, you set this to provided .

You’ll also want a .gitignore file containing:

.serverless

as we don’t want that directory in git.

Let’s add our layer to serverless.yml next, by adding:

layers: php: path: layer/php

This will create the AWS layer and give it a name of PhpLambdaLayer which we can then reference in our function.

Writ

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

代码区博客精选文章
分页:12
转载请注明
本文标题:Serverless PHP on AWS Lamda
本站链接:https://www.codesec.net/view/628287.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(50)