PHP ON LAMBDA

Ian Littman / @iansltx

AustinPHP January 2019

https://ian.im/lambatx19

WHY LAMBDA?

  • No servers to manage
  • Quick automatic scaling
  • High levels of concurrency
  • Very granular billing
  • Fair-sized free tier
  • Stateless per-request, shared-nothing-ish
  • Integrates with other AWS functions
    • Application Load Balancer (billed for capacity + time)
    • API Gateway (billed per request)

Shared-nothing-ish?

  • "Cold starts"
  • AWS keeps Lambda containers around for a bit
  • 500 MB in /tmp is available per-instance
  • Instances != invocations
  • Cache on disk if you want/need it

Wait, I thought Lambda Didn't Do PHP

  • Custom runtime support became available late last year
  • 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
    • Versioned
    • Can include library dependencies (e.g. /vendor)
    • Zipped + submitted to AWS API (like functions)
  • Worker/polling model, NOT HTTP request based

Lambda Custom Runtime Lifecycle

Wait, this isn't shared-nothing?!?

That's a good thing for billable exec time

The Easy Way

php-lambda-layer

  • Bootstrap written in PHP
  • Forks, then runs PHP built-in web server in fork
  • Only works for ALB/API Gateway
  • Rebuilds API Gateway request context, then curl-calls the forked web server to execute
  • Calls the PHP file specified in the handler
  • Reformats the result and pushes back to AWS
  • Feels a bit more shared-nothing-y
  • Enable extensions via function config ini overrides

Why PHP 7.1?

OpenSSL version conflict

Can we combine the two?

Not quite. Compiled binary doesn't have pcntl.

// TODO see if building a binary with --enable-pcntl works

Thanks! Questions?

PHP On Lambda - AustinPHP January 2019

By Ian Littman

PHP On Lambda - AustinPHP January 2019

  • 1,359