Aws Php

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 Source PHP Runtime for Apache OpenWhisk which is available commercially as IBM Cloud Functions

AWS SDK for PHP Get started quickly using AWS with the AWS SDK for PHP. The SDK is a modern, open-source PHP library that makes it easy to integrate your PHP application with AWS services like Amazon S3, Amazon Glacier, and Amazon DynamoDB. AWS SDK for PHP: The AWS SDK for PHP is an open source library designed to help developers build applications that are based on the PHP server-side scripting language within Amazon Web Services (AWS). Such AWS software development kits (SDKs) supply tools for a developer working with a specific programming language. Following the trend of serverless, all that hype (or not?) I was looking through the AWS services offered and stumbled upon AWS Fargate, a service that lets you run containerized applications on either Amazon Elastic Container Service (ECS) or Amazon Elastic Kubernetes Services (EKS). For the tooling (development and deployment) of our PHP application I'd like to stick to probably the widely. Acquire the knowledge you need to easily navigate the AWS Cloud Launch Your First Application Select a learning path for step-by-step tutorials to get you up and running in less than an hour.

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 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:

2
4
6
$exportSSH_KEY_FILE=~/.ssh/aws-key.rsa
$scp-i$SSH_KEY_FILEcompile_php.sh$AWS_IP:doc/compile_php.sh
$ssh-i$SSH_KEY_FILE-t$AWS_IP'chmoda+xcompile_php.sh&&./compile_php.sh7.3.0'
$scp-i$SSH_KEY_FILE$AWS_IP:php-7-bin/bin/phplayer/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:

Aws Php
2
4
6
8
10
12
do
# Get an event
EVENT_DATA=$(curl-sS-LD'$HEADERS'-XGET'http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/next')
REQUEST_ID=$(grep-FiLambda-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-XPOST'http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/$REQUEST_ID/response'-d'$RESPONSE'


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.

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

2
4
functionhello($eventData):array
return['msg'=>'hello from PHP '.PHP_VERSION];


The function takes the information about the event and returns an associative array.

To tell Serverless Framework to deploy it, we add it to serverless.yml:

Invoke our function

Php

Finally, we can invoke our function using:


And we’re done!

To sum up

With the new layers and runtime API, it’s now possible to easily run PHP serverless functions on Lambda. This is great news and worth playing if you’re a PHP developer stuck tied to AWS.

I should also note that you should look into Bref which makes PHP on Lambda much easier!

This blog post is an excerpt of our book Rapid Docker on AWS.

The biggest game-changer for Docker on AWS was the announcement of AWS Fargate. Operating Docker containers could not be easier. With AWS Fargate, you launch Docker containers in the cloud without any need of managing virtual machines.

All you need is a Docker image of your application. You will learn how to dockerize your PHP web application for the use with AWS Fargate in the following.

What is a Docker image?

S3 Aws Php

A Docker image is similar to a virtual machine image, such as an Amazon Machine Image (AMI) that is used to launch an EC2 instance. The Docker image contains an operating system, the runtime environment, 3rd party libraries, and your application. The following figure illustrates how you can fetch an image and start a container on any platform.

Two containers: NGINX and PHP-FPM

But how do you create a Docker image for your web application? By creating a script that builds the image step by step: a so-called Dockerfile.

Php Aws Api

Our example is a PHP application written in PHP without using any frameworks. The project’s folder structure:

Aws Php Api

  • conf the configuration directory (contains .ini files)
  • css the stylesheet directory (contains .css files)
  • img the images directory (contains .gif files)
  • lib the libraries directory (contains .php files)
  • index.php the main file

Add a docker directory containing the Docker configuration (e.g., the Dockerfile).