Custom runtimes for AWS Lambda
Custom runtimes for AWS Lambda

Amazon AWS announced new features in the AWS Lambda such as the custom runtimes, which will let you bring basically any language to the platform. Lambda was launched in 2015, the product will let you write the code, then AWS will manage the necessary computing, memory and storage. You pay only for the compute time you consume – there is no charge when your code is not running.

The video below explain what AWS Lambda is all about :

So if you notice the PHP logo below, that’s the PHP Layer for AWS Lambda provided by Stackery, an advanced Tier Technology Partner status in the Amazon Web Services Partner Network (APN). They provide a Lambda Runtime Layer which runs the PHP 7.1 webserver in response to AWS API Gateway requests. However you should notice that this is an early iteration of the PHP runtime Layer which is not yet ready for production.

Custom runtimes for AWS Lambda
Custom runtimes for AWS Lambda

PHP Layer for AWS Lambda is available as open source, however if you use it as it, it’s not yet ready for production. But if you decided to use it using Stackery it should be fine for production environment. Stackery provides a command line to easily deploy your code to AWS lambda.

Otherwise you need to get AWS SAM CLI, which is a CLI tool for local development and testing of Serverless applications. Then create a yaml template with the following SAM infrastructure :

AWSTemplateFormatVersion: 2010-09-09
Description: My PHP Application
Transform: AWS::Serverless-2016-10-31
Resources:
  phpserver:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: !Sub ${AWS::StackName}-phpserver
      Description: PHP Webserver
      CodeUri: src/server
      Runtime: provided
      Handler: index.php
      MemorySize: 3008
      Timeout: 30
      Tracing: Active
      Layers:
        - !Sub arn:aws:lambda:${AWS::Region}:887080169480:layer:php71:3
      Events:
        api:
          Type: Api
          Properties:
            Path: /{proxy+}
            Method: ANY

Then you create your PHP application such as

Hello World! You've reached <?php print($_SERVER['REQUEST_URI']); ?>

The directory structure should be something like this :

.
├── template.yaml
└── src
    └── php
        └── index.php

Finally you can deploy your code using :

$ sam package \
    --template-file template.yaml \
    --output-template-file serverless-output.yaml \
    --s3-bucket <your SAM deployment bucket created above>

$ sam deploy \
    --template-file serverless-output.yaml \
    --stack-name my-first-serverless-php-service \
    --capabilities CAPABILITY_IAM

The idea of serverless applications is very similar to docker, but on the code level. Running PHP applications on AWS Lambda was even possible before this announcement, and a full guide to host Laravel on Lambda is even available but using the NodeJs runtime.

Lambda previously does not support PHP out of the box however they do let you run arbitrary executables paving the way to run PHP CGI, which is not a very good solution performance wise.

Actually the only code that we have seen running is a hello world, so we cannot talk yet about performance. It will be interesting to have some benchmark done on real world applications such as wordpress, drupal, magento, woocommerce, etc.

More information about PHP Layer for AWS Lambda here, and AWS Lambda itself is available here.

LEAVE A REPLY

Please enter your comment!
Please enter your name here