Hello
Leo from TURBINE KREUZBERG
Serverless Framework
- CLI tool
- configure your (AWS) app in a .yml file
permissions, resources, functions, routes, events - deploy your app to FaaS provider
- invoke functions local
- ...
You need
- nodejs v6.5.0+
- Serverless CLI v1.9.0+
npm install -g serverless - FaaS provider account
- set up provider credentials
key concepts
- functions
- events
- resources
- plugins
serverless.yml
service: my-app
provider:
name: aws
runtime: nodejs6.10
stage: dev
region: eu-central-1
functions:
homePage:
handler: src/homePage.handler
events:
- http: 'GET /'
deploy my app
$ sls deploy
Serverless: Packaging service...
Serverless: Excluding development dependencies...
(...)
endpoints:
GET - https://abcdefghij.execute-api.eu-central-1.amazonaws.com/dev
functions:
homePage: my-app-dev-homePage
src/homePage.js
'use strict';
module.exports.handler = (event, context, callback) => {
const response = {
statusCode: 200,
body: JSON.stringify({
message: 'Welcome to my website',
input: event,
}),
};
callback(null, response);
};
change, log, deploy, run
$ sls log -f homePage -t
$ sls deploy -f homePage
$ sls invoke -f homePage
permissions
service: my-app
provider:
...
environment:
DYNAMODB_LOG_TABLE: ${self:service}-request-log-${self:provider.stage}
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:PutItem
Resource: "arn:aws:dynamodb:${self:provider.region}:*:table/${self:provider.environment.DYNAMODB_LOG_TABLE}"
...
Add Resource Table
resources:
Resources:
RequestLogDynamoDbTable:
Type: 'AWS::DynamoDB::Table'
Properties:
AttributeDefinitions:
-
AttributeName: id
AttributeType: S
KeySchema:
-
AttributeName: id
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TableName: ${self:provider.environment.DYNAMODB_LOG_TABLE}
src/homePage.js
const uuid = require('uuid');
const aws = require('aws-sdk');
const dynamoDb = new aws.DynamoDB.DocumentClient();
module.exports.handler = (event, context, callback) => {
...
const params = {
TableName: process.env.DYNAMODB_LOG_TABLE,
Item: {
id: uuid.v1(),
function: 'homePage.handler',
requestTime: new Date().getTime()
},
};
dynamoDb.put(params, (error) => {
if(error) {
// we should to some error handling ...
console.log(error);
}
callback(null, response);
}
};
Events
- assign multiple events to single function
- S3, DynamoDb, API gateway, schedule, SNS, AlexaSkill, IoT
- CloudWatch Events and Logs
Resources
- write raw template syntax
logging
sls logs -f myFunction -t
- creates a LogGroup for every function
- tags in serverless.yml for functions
deployment
- set global environment variables and override them in functions
- automated versioning
- deploy to different stages or regions
- deploy whole app
- deploy single functions
failure
- DQL support for every functions
kickstart
$ sls create --template aws-nodejs --path my-app # create
$ sls deploy -v # deploy app
$ sls deploy function -f my-function # deploy single function
$ sls invoke -f my-function -l # run single function
$ sls logs -f hello -t # like tail -f your.log
$ sls remove # remove your service :(
Speaker View
There's a speaker view. It includes a timer, preview of the upcoming slide as well as your speaker notes.
Press the S key to try it out.
deck
By Stefan Adolf
deck
- 350