PHP ON LAMBDA
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
-
bootstrap runs once on instance start
- Initializes runtime
- Preps function for execution
- Passed env vars including file/method for fn handler
- If there's an error init-ing, make an API call
- One bootstrapped, instance calls APIs to process work
- Next invocation (GET); invocation ID in headers
- Invocation response (POST), includes invocation ID
- Invocation error
- Lambda decides when to kill the instance
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
- PHP 7.3 (compiled, including OpenSSL)
- Shared execution environment from CLI init
- Handled by method calls rather than HTTP
- Much lower overhead
- Rob Allen riffed on this bit...
- ...and set up a version to use Serverless Framework
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,323