Serverless
FaaS & BaaS
Agenda
- What is Serverless?
- Why Serverless?
- Road to Serverless
- Functions as a Service
- FaaS: Under the Hood
- Best Practices
- Exercises
What is Serverless?
"Serverful"
Serverless
vs
Server maintenance is responsibility of user
Server maintenance is responsibility of provider
Why Serverless?
Why Serverless?
Serverless is about focusing your efforts on what provides value to users. Upgrading your Linux distro does not provide value to users. Managing your RabbitMQ servers does not provide value to users. Shipping product provides value to users.
That is the serverless maxim: focus on business logic, not servers.
serverless.com
Road to Serverless
On Premise Servers
- Needs staff to maintain the system
- Servers must be bought upfront
- High level of control
Infrastructure as a Service
- Provider manages hardware
- Provides a base OS
- User maintains and updates software
- Scales vertically and horizontally
Platform as a Service
-
User just selects the runtime
- OS and virtualization managed by
provider
- Scales horizontally and vertically
Backend as a Service
- Common features provided as APIs or SDKs
- Very fast development
- Provider maintains infrastructure and software
Functions as a Service
- User just writes a function
- Based on ephemeral containers
- Scales easily
- Pay-per-use
Summary
Control
Abstraction
Functions as a Service
FaaS
Run code without thinking about servers. Pay only for the compute time you consume.
AWS Lambda
General Architecture
Triggers
Function
Result
File Upload
Database Update
New Message in a Queue
New Commit to Repo
Scheduled Events
HTTP API Call
Benefits
- Scalability
- Focus on Business Logic
- Fault-tolerance
- Security
- Pay per use
Drawbacks
- Cold Starts
- Vendor lock-in
- Lack of Control
- Testing
Use Cases
- Event-based Architectures
- Expensive and Spontaneous Computations
- Variable Demand Systems
Under the Hood
Your Code
Lambda Runtime
Sandbox
Guest OS
Hypervisor
Host OS
Hardware
Amazon Linux
Node, Python...
Lambda Function
AWS EC2 Metal
Container
AWS Lambda Init
Development Tools
- Official tool by AWS
- Only for AWS
- Allows local testing for Lambda functions
- Permits event generation for other AWS services
- Unofficial tool
- Provider-agnostic
- Allows local testing for various services
- Has a great variety of plugins
Best Practices
Keep Functions Stateless
const AWS = require('aws-sdk'),
const uuid = require('uuid'),
const documentClient = new AWS.DynamoDB.DocumentClient();
exports.handler = function(event, context, callback){
const params = {
Item: {
id: uuid.v1(),
firstName: event.firstName,
lastName: event.lastName,
},
TableName: 'people'
};
documentClient.put(params, function(err, data){
callback(err, data);
});
}
Use permanent data stores like databases to store state
Separate Business Logic
exports.handler = function(event, context, callback) {
const foo = event.foo;
const bar = event.bar;
const result = myLambdaFunction (foo, bar);
callback(null, result);
}
function myLambdaFunction (foo, bar) {
// your logic here
}
Separate your business logic from vendor-specific handler
Use the Execution Context
const mysql = require('mysql');
const connection = mysql.createConnection({
host : 'localhost',
database : 'my_db'
});
connection.on('error', function(err) { connection.connect(); });
exports.handler = function(event, context, callback){
connection.query('SELECT * FROM people', function (error, results, fields) {
if (error) {
connection.destroy();
callback(error, null);
}
callback(null, results);
});
}
Reuse connections between calls to the same function
Monitor Memory Usage
A higher memory tier may be more expensive, but the execution time may be shorter
Hello World
Install Serverless
$ npm install -g serverless
Create Project
$ mkdir serverless-hello-world
$ cd serverless-hello-world
$ sls create --template aws-nodejs
Install Serverless Offline
$ npm install serverless-offline --save-dev
Enable Serverless Offline
## serverless.yml
service: aws-nodejs
## Add these two lines
plugins:
- serverless-offline
provider:
Setup HTTP Route
## serverless.yml
functions:
hello:
handler: handler.hello
events: ## Add these four lines to setup the HTTP route
- http:
path: /
method: get
Test
$ sls offline start
Serverless: Starting Offline: dev/us-east-1.
Serverless: Routes for hello:
Serverless: GET /
Serverless: Offline listening on http://localhost:3000
1st Exercise
Note Taking API
Should be able to:
- Create note
- Get note by ID
- Get all notes
- Update note
- Delete note
Tools
Useful Material
2nd Exercise
HTML-based Visual Testing
Endpoint /store should be able to:
- Store HTML files in AWS S3 with an ID
Endpoint /diff should be able to:
- Retrieve HTML from S3 given the ID
- Render HTML files with Chrome
- Screenshot rendered files
- Diff screenshots
- Return whether screenshots match or not
Tools
Useful Material
Interesting Resources
Serverless: FaaS & BaaS
By Bernardo Belchior
Serverless: FaaS & BaaS
- 1,013