A Bref talk about Serverles PHP
Who Am I?
- I am Ryan
- I have been using PHP for ~ 5 years and enjoy working with serverless architectures
- codebar Birmingham Co-Organiser
- I love programming mascots 🐘

Talkception
Talkception
2 Lightning talks ⚡
(Aiming to be beginner friendly as possible)
- What is Serverless? (10 Minutes)
- What is bref php? (5 Minutes)
- Live demo ("What could go wrong™")

What is serverless?
What Is a Server?









Let's add a user
login system 🎉




Let's add a user
login system 🎉



Let's add a user
login system 🎉




Great!
Lets now add a reporting page 🎉



Great!
Lets now add a reporting page 🎉



That's really good,
Can we now add Search?



That's really good,
Can we now add Search?



Awsome... Now can
you make that all
a bit more faster🏃



Awsome... Now can
you make that all
a bit more faster🏃



Can you push that report to this external service every hour?



Can you actually
push that report you generated hourly to this external service



That is perfect
thanks ❤️
See you in a bit!



That is perfect
thanks ❤️
See you in a bit!




Actually could you
just...





Actually could you
just...





Actually could you
just...



Why Might having a server like this be a problem?
Your load can be inconsistent. Your resources cannot.

[Steam active user chart]



You can have too much


You can also have too little
Also give it a few years and you server will eventually look like this:

So what can we do about this?
Vertically Scale

Vertical scaling
- Easier to scale (Just add a bigger server)
- Lower time investment into making infrastructure stack
- Fewer considerations need to be made about the application stack's hosting environment when writing the app.
However:
- The server is a single point of failure.
- Scaling options limited as there is eventually be limitations surrounding how much hardware the server can support
- Harder to isolate where issues might arise.
What are our other options?














Time until ready
Size of server
Size of server
Resolution of scaling
Time until ready

~ 10 Requests
per second
Resolution of scaling
5 Minutes

Resolution of scaling

~ 10 Requests
per second
5 Minutes
Lets make it smaller

~ 5 Requests
per second
2 Minutes
~ 10 Requests
per second
Lets make it smaller

5 Minutes

5< Requests
per second
Make it even Smaller
1 Minute



How small can we go?
A single
request
~ 250MS
How small can we go?
A single
request
~ 250MS

Serverless
A single
request
~ 250MS

What is AWS Lambda?
"AWS Lambda is a compute service that lets you run code without provisioning or managing servers."
It effectively allows you to execute code based on a large list of events within the AWS ecosystem (Including network requests) without the need of worrying about the underlying infrastructure, scaling, availability, logging ect...
Serverless('nt)
- It is a Lie, Serverless does actually involve running a server :( ... Just ones fully managed by AWS.

Key Advantages of serverless
- It eliminates the need to maintain dedicated servers
- It should significantly reduce hosting costs in most cases
- It frees up engineering resources and moves the focus towards delivering business value
- It ensures the platform is highly available, automatically scales directly with demand.
- Wide range of supported events and well established ecosystem across multiple cloud providers
How much is it going to cost?
Metric | Amount | Per |
---|---|---|
Compute | $0.0000166667 | GB/s |
Requests Resources | $0.20 | Million Requests |
Data Transfer | ~ $0.108 | per GB Egress (To internet) |





Serverless is not a panacea for all backend compute related problems. It is a highly powerful tool with it's own quirks, limitations and ways of thinking.
What is the catch?
Limitation | Maximum value |
---|---|
Concurrently running lambdas | 1000 per AWS Region (can be raised on request) |
Lambda Memory | 10GB (Hard limit) (6v cpus allocated by memory) |
Ephemeral Storage | 10GB |
Other Limitations
Name | Description |
---|---|
Vendor Lock In | You are committed to the AWS Cloud while using Lambda. Moving away could be difficult |
Cold starts | Lambdas can be significantly slower on the first call made to them if they have not been invoked in a while (subsequent calls are faster) |
Vulnerable to Denial of wallet attacks | Given it is a Pay As You Go model the costs can become huge if limits are not set / costs not monitored. |
Scalable as the weakest link | Lambda adds scalability in the compute layer but not in the data layer. Non scaling data storage can still be overwhelmed (Though Serverless options exist Serverless Aurora, DynamoDB) |
Here are a few examples of where AWS Lambda Might be useful in an Existing application stack

If you load is very stable.
It is probably not all to problematic to leave a server running as is

However if you load is very unstable AWS Lambda can be a great option

Also if your load is very small / low volume it can be a great way to reduce hosting costs
Other circumstances where AWS Lambda can be useful
- Running business critical processes
- Running scheduled jobs (Especially computationally ones)
- Handling routes on the website that are disproportionately expensive to load

How do we get started?







A

Talk About


A

Talk About




A

Talk About









LOGO | What is it |
---|---|
PHP | |
Bref PHP | |
Serverless Framework | |
AWS Lambda | |
AWS Cloudformation |




LOGO | What is it |
---|---|
PHP | |
Bref PHP | |
Serverless Framework | |
AWS Lambda | |
AWS Cloudformation |






AWS Cloud Formation
"AWS CloudFormation is an infrastructure as code (IaC) service that allows you to easily model, provision, and manage AWS and third-party resources."
AWSTemplateFormatVersion: '2010-09-09'
Description: Template for Lambda Sample.
Resources:
LambdaFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName:
Fn::Sub: lambda-function-${EnvName}
Description: LambdaFunctioni of nodejs12.x.
Runtime: nodejs12.x
Code:
ZipFile:
"exports.handler = function(event, context){\n
var sample = sample;"
Handler: ${LambdaHandlerPath}
MemorySize: 128
Timeout: 10
Role:
Fn::GetAtt:
- LambdaRole
- Arn
Environment:
Variables:
ENV:
Fn::Sub: ${EnvName}
TZ: UTC




AWS Lambda
"AWS Lambda is a serverless, event-driven compute service that lets you run code for virtually any type of application or backend service without provisioning or managing servers. You can trigger Lambda from over 200 AWS services and software as a service (SaaS) applications, and only pay for what you use. "
AWSTemplateFormatVersion: '2010-09-09'
Description: Template for Lambda Sample.
Resources:
LambdaFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName:
Fn::Sub: lambda-function-${EnvName}
Description: LambdaFunctioni of nodejs12.x.
Runtime: nodejs12.x
Code:
ZipFile:
"exports.handler = function(event, context){\n
var sample = sample;"
Handler: ${LambdaHandlerPath}
MemorySize: 128
Timeout: 10
Role:
Fn::GetAtt:
- LambdaRole
- Arn
Environment:
Variables:
ENV:
Fn::Sub: ${EnvName}
TZ: UTC





Serverless Framework
"The Serverless Framework is a free and open-source web framework written using Node.js. Serverless is the first framework developed for building applications on AWS Lambda, a serverless computing platform provided by Amazon as a part of Amazon Web Services."
# Serverless.yml
service: Some Service
custom:
name: ${self:service}
stage: ${opt:stage, 'dev'}
provider:
name: aws
region: us-east-1
runtime: provided.al2
plugins:
- ./vendor/bref/bref
functions:
frontend:
handler: src/handler.php
description: 'Handles Requests'
timeout: 28
layers:
- ${bref:layer.php-80}
events:
- httpApi: '*'





Bref PHP
"Bref (which means "brief" in french) is as an open source Composer package and helps you deploy PHP applications to AWS and run them on AWS Lambda."
[...]
"Bref uses the Serverless framework to configure and deploy serverless applications. Being the most popular tool, Serverless comes with a huge community, a lot of examples online and a simple configuration format."





🐘PHP 🐘
🐘








Lets Build something!

Layer Name | Purpose |
---|---|
Web apps: php-xx-fpm | Uses a single Threaded version of PHP-FPM to process requests |
Event Driven: php-xx | Used in more conventional lambda applications and can be triggered by any event in the PHP ecosystem. |
Event Driven: php-console | Used for invoking PHP console commands on AWS Lambda |
Credits
Hosts / Sponsors
The joint works (For hosting)
Jump 24 (For sponsoring)
The Birmingham PHP Team (For organizing)
Usage Credits
Diagrams + Charts
https://diagrams.mingrammer.com/
https://plotly.com/
https://matplotlib.org/
Special Thanks
Jim Seconde
Live Demo Time 👀
Credits
Hosts / Sponsors
The joint works (For hosting)
Jump 24 (For sponsoring)
The Birmingham PHP Team (For organizing)
Usage Credits
Diagrams + Charts
https://diagrams.mingrammer.com/
https://plotly.com/
https://matplotlib.org/
Special Thanks
Jim Seconde
Credits
Hosts / Sponsors
The joint works (For hosting)
Jump 24 (For sponsoring)
The Birmingham PHP Team (For organizing)
Usage Credits
diagrams.mingrammer.com
plotly.com
matplotlib.org
slides.com
Special Thanks
Jim Seconde
Questions?
Codebar



Find me on:
- https://ryan.gd
All examples and slides can be found at:
- https://github.com/ryanolee/talks
[Image : Andy Holmes]

Copy of Fizz Pop KVMs
By Rizza
Copy of Fizz Pop KVMs
- 104