Leo from TURBINE KREUZBERG
npm install -g serverless
service: my-app
provider:
name: aws
runtime: nodejs6.10
stage: dev
region: eu-central-1
functions:
homePage:
handler: src/homePage.handler
events:
- http: 'GET /'
$ 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
'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);
};
$ sls log -f homePage -t
$ sls deploy -f homePage
$ sls invoke -f homePage
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}"
...
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}
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);
}
};
sls logs -f myFunction -t
$ 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 :(
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.