KEMBAR78
Getting Started with Serverless PHP | PPTX
@andyraines
@araines
Topics
● What is Serverless?
● AWS Lambda
● Running PHP on Lambda
● PHP performance in Lambda
● Future work
What is Serverless?
What is Serverless?
“Function as a service (FaaS) is a category of cloud computing services that
provides a platform allowing customers to develop, run, and manage application
functionalities without the complexity of building and maintaining the infrastructure
typically associated with developing and launching an app”
https://en.wikipedia.org/wiki/Function_as_a_service
Responsibilities
Application
Data
Scaling
Runtime
Operating System
Servers
Storage
Networking
Application
Data
Scaling
Runtime
Operating System
Servers
Storage
Networking
Application
Data
Scaling
Runtime
Operating System
Servers
Storage
Networking
Application
Data
Scaling
Runtime
Operating System
Servers
Storage
Networking
On Premise IaaS PaaS FaaS
AWS EC2
Google Cloud
Heroku
AWS Lambda
GCF
What defines Function as a Service (FaaS)?
● Leading-edge technology (~3 years old)
● Run code, not servers
● Extremely scalable - ultra parallel
● Pay as you go - no executions, no costs
● Extremely cheap for many applications
Serverless Framework
● Simplified, common way to set up
serverless architecture
● Works on major FaaS providers
● Configure infrastructure in YAML
● Simple command line deploy / destroy
www.serverless.com
FaaS providers
Microsoft Azure Functions
Google Cloud Functions
AWS Lambda
AWS Lambda
AWS Lambda
Stateless (sort-of)
Deployment via upload of code to S3
Triggered from other AWS services (e.g. S3 / API Gateway / Alexa)
Run up to 5 minutes (billed in 100ms increments)
Select memory allocation to select CPU power - 128MB to 1.5GB
$0.20/million <100ms 128MB invocations
First million/month free!
Example Service Architectures
S3
Image
uploaded
Invokes
Lambda
Store resized
image
Image Resize Service
Example Service Architectures
API Gateway
HTTP request Invokes
Lambda
Data response
Simple Web Service
Database
HTTP response
Lambda Programming Model
Handler - Code entry point, gets executed when invoked. Event data is passed to
this function as the first parameter.
Context - An object which allows interaction with Lambda itself at runtime (e.g.
find out how much execution time is left before termination). Pass to the handler
as the second parameter.
Logging - Language native logging is handled and sent back to AWS Cloudwatch
Logs
Stateless - Code should assume everything is limited to the lifetime of the
invocation
AWS Lambda Language Support
AWS Lambda Language Support
PHP is Awesome!
Running PHP on Lambda
Concept
Utilise Node.js as a shim for PHP
Handler - should be a function which takes event data and a Context object
Context - an Object which conforms to the native Java methods allowing for run-
time interaction with Lambda
Logging - use a PSR compliant logger to log to Cloudwatch Logs
Stateless - assume nothing can survive the end of a request
Architecture
./php handler.php <eventData> <contextData>
(inc environment)
stdout: result
stderr: logs
fd3: command
invokes
Concept
handler.php
config/services.yml src/HelloHandler.php
src/Context.php
src/Context.php
serverless.yml
Function name (can have multiple)
Javascript shim function name
Symfony service name to use
Describe what invokes this function
Architecture
invokes
stdout: result
stderr: logs
fd3: command
API Gateway Lambda
Demo Time!
PHP Performance on Lambda
Future Work
PHP-FPM
PHP
Handler ExecutionBoot
PHP
Handler ExecutionBoot
PHP
Handler ExecutionBoot
Request 1 Request 2 Request 3
Lambda Lifespan
PHP-FPM
Handler ExecutionBoot Handler Execution Handler Execution
Symfony & Project Improvements
● Use S3 to deploy static assets for full-stack deployments
● Produce a Flex recipe to get everything going
● General code tidying etc
● Better exception handling
● Out-of-the-box integrations with other AWS services (e.g. SES for email)
Resources
github.com/araines/serverless-php
serverless.com
read.acloud.guru
@andyraines

Getting Started with Serverless PHP

Editor's Notes

  • #2 Introduce self How many people used AWS Lambda / FaaS / Serverless?
  • #5 Serverless is the commonly used name for Function as a Service But what does this mean?
  • #6 In blue are your responsibilities when using different setups
  • #7 First FaaS around 3 years ago (hook.io), lots more providers since We may be now where we were 5 years ago with containers Upload code, run code - don’t worry about OS, servers etc Scale with events - practically unlimited scalability AWS Lambda - billed per 100ms, first million requests every month are free, 20 US cents per million thereafter - assuming <100ms per request
  • #8 Probably most popular project for managing serverless services There are other projects too!
  • #9 Microsoft Azure Functions Google Cloud Functions AWS Lambda IBM OpenWhisk Others….
  • #17 Work daily with PHP and Symfony Very familiar with the language, and more importantly the tooling and processes Save many hours learning something new and becoming proficient at it Excellent community
  • #20 Request hits API Gateway API Gateway invokes Lambda with request data Lambda starts node and executes Node.js shim Shim invokes PHP passing request data as command line arguments PHP executes and returns the result via stdout, logs via stderr and uses fd3 for context commands
  • #22 Logger logs to stderr
  • #26 Request hits API Gateway API Gateway invokes Lambda with request data Lambda starts node and executes Node.js shim Shim invokes PHP passing request data as command line arguments PHP executes and returns the result via stdout, logs via stderr and uses fd3 for context commands
  • #28 Can pick different sizes of Lambda - CPU scales with memory Node pretty consistently 20ms PHP ~40ms boot time Believe this is primarily because we are starting a PHP binary every time
  • #30 Lambda functions should be considered stateless when developing However, they actually live for quite some time (5-30 minutes) Boot FPM once, reuse for subsequent requests
  • #32 GitHub - has a link to my blog post with way more detail Serverless Framework