PHP on Lambda

with Custom Runtimes

Ian Littman / @iansltx

NomadPHP US February 2020

Follow along at https://ian.im/lambno0220

What we'll cover

  • How Lambda works with Custom Runtimes (including PHP)
  • How to build an email reply bot with Lambda, SES, S3, and PHP
  • How to build a Lambda-powered API via API Gateway
  • ...including live demos using the AWS console 🤞

What We won't cover

  • Laravel Vapor
  • A deep dive into Bref
  • Building Lambda applications at the command line

WHY LAMBDA?

Shared-nothing-ish?

Wait, I thought Lambda Didn't Do PHP...

  • Custom runtime support has been available since 2018
  • Build the file system needed to run your function code
  • Store the file system as one or more (up to 5) Layers
    • Extracted to /opt (function code is in /var/task)
    • Versioned
    • Can include library dependencies (e.g. /vendor)
    • Zipped + submitted to AWS API (like functions)
  • Worker/polling model, NOT HTTP request based

Custom runtime lifecycle

  • Bootstrap (cold start)
  • Task execution (warm start)
  • Lambda decides when to kill the instance
    • Inactivity timeout
    • Task deadline (configurable, up to 15 minutes)
  • Initialize runtime
  • Prep function for execution
  • Pass env vars including file/method for handler function
  • If there's an error init-ing, make an API call

Task execution (warm start) Phase

  • Call APIs to process work
  • Next invocation (GET)
    • Request ID and more in headers
    • Task data in response body, JSON-encoded
  • Invocation response (POST)
    • Request ID in URL
    • Response in body
    • Consuming services may require specific format
  • Invocation error (POST)

Virtual Private Caveats

  • No internet connectivity by default
  • NAT Gateway is $$$ (4.5¢/hr + 4.5¢/GB)
  • Workaround: split functionality between functions:
    one that needs 'net connectivity, one that needs
    resources inside the VPC
  • Cold start times are no longer an issue as of late 2019
  1. SES rule pushes email to S3
  2. S3 triggers Lambda
  3. Lambda function executes...
    1. Grabs email from S3
    2. Parses using MIME parser
    3. RaNdOMlY CaPiTaLiZeS LEtTerS
    4. Sends email reply via SES
  1. HTTP request hits API Gateway
  2. Lambda function executes...
    1. Grabs query string parameter if there is one
    2. RaNdOMlY CaPiTaLiZeS LEtTerS
    3. Sends a JSON response
  3. API Gateway turns the Lambda payload into an HTTP response

...but you should probably use Bref in prod

Remember returntrue.win?

...or Vapor for Laravel-ites

Further Reading

Thanks! Questions?

PHP on Lambda with Custom Runtimes - NomadPHP US February 2020

By Ian Littman

PHP on Lambda with Custom Runtimes - NomadPHP US February 2020

At re:invent 2018, Amazon released custom runtime support and Layers for their Lambda function-as-a-service runtime. With this release, the fact that the company still doesn't have an official PHP Lambda runtime becomes more or less irrelevant, since we as developers can create our own without undue overhead. This talk will show you how Lambda custom runtimes work, how to build one that speaks PHP, and how to use pre-built custom runtime layers for either one-off job processing or behind a load balancer or API Gateway to serve web traffic with extreme infrastructure elasticity.

  • 1,155