WELCOME TO THE AWS SERVERLESS HACKATHON - …hackathon-blog.sandbox.sc5.io/walkthrough.pdf ·  ·...

30
WELCOME TO THE AWS SERVERLESS HACKATHON Mikael Puittinen, CTO 1 25.4.2016

Transcript of WELCOME TO THE AWS SERVERLESS HACKATHON - …hackathon-blog.sandbox.sc5.io/walkthrough.pdf ·  ·...

WELCOME TO THE AWS SERVERLESS HACKATHON

Mikael Puittinen, CTO

125.4.2016

A SHORT INTRODUCTION TO SC5

SC5 BRIEFLYIntroducing

CLOUD SOLUTIONS

BUSINESS APPLICATIONS

DIGITALDESIGN

10YRS

60+CUSTOMERS

200+ PROJECTS

HELJKL

65HACKERS

DESIGNERS

6MEUR

WHY SERVERLESS?

” One of the biggest revolutions we have seen in the technology world in the last few years is the rise of serverless computing. This has been largely triggered by the launch of AWS Lambda that no longer requires a server (physical or virtual) to run application code. This tremendously simplifies application development as architects only need to think about business logic and no longer need to worry about managing fleets of servers to run their software. This makes it easier to achieve the security and reliability to protect their business and their customers. After all, no server is easier to manage than no server.”

Werner Wogels, CTO / VP at Amazon.com

(https://www.linkedin.com/pulse/simplification-technology-trend-2016-werner-vogels)

A short business case

SC5 & SERVERLESS - BACKGROUND

AWS Lambda released

AWS API Gateway released

First SC5 API Gateway + Lambda project (HappyOrNot webshop) started 4 days after API Gateway release

JAWS Framework 0.0.1 released

First SC5 Jaws / Serverless project (Gasum Industryhack)

JAWS becomes Serverless Framework

4 customer projects going on, 4 delivered based on AWS Lambda (7 of which on Serverless)

A bit of history

Nov 2014

July 2015

Sep 2015Oct 2015

Dec 2015

Today

INTRODUCTION TO LAMBDA , API GATEWAY AND SERVERLESS

AWS LAMBDA

§ Compute service for running code (functions) in AWS

§ Provision resources required by single function run

§ Automatically spawns additional ”instances” if required

§ Invoiced based on actual compute time used (100 ms)

§ Input and output JSON

Serverless Compute

LAMBDA EXAMPLE: CALCULATOR

§ Open AWS Console / Lambda

§ Create new function

§ Name: ”Calculator”

§ Copy code from the right

§ Role: Create new role ”Basic execution role”

§ Once created, test with e.g. sample test from the right

§ Logs available in Cloudwatch

exports.handler = function(event, context) {console.log('a =', event.a); console.log('b =', event.b); context.succeed({

sum: event.a + event.b});

};

TEST:{”a”: 10,”b”: 20

}

API GATEWAY

§ AWS Service to implement REST (and other) APIs

§ Security via API Keys, customer authorizers

§ Connect to e.g. Lambda to publish your functions as REST interfaces

§ Input / Output mapping (e.g. URL parameters -> JSON)

§ No need for provisioning

§ Invoicing based on # of requests + data transfer + cache size

Serverless REST API

API GATEWAY EXAMPLE

§ Launch API Gateway from AWS Console

§ Create API ”Calculator”

§ Create resourse ”calculator” (from Actions)

§ Create ”POST” method for calculator resource

§ Integration type: Lambda Function

§ Deploy API to stage ”v1”

§ Copy URL displayed for resource

§ Test API with e.g. Postman

SERVERLESS FRAMEWORK

” Serverless is the application framework for building web, mobile and IoT applications exclusively on Amazon Web Services' Lambda and API Gateway. It's a command line interface that helps you build and maintain serverless apps across teams of any size. It's also completely extensible via Plugins. We believe AWS Lambda will be the focal point of the AWS cloud, and the Serverless Framework interprets AWS from Lambda's perspective.”

https://github.com/serverless/serverless

THE CHALLENGES

CHALLENGES

1. Guided challenge : Blog backend on serverless (by Alex Koslov)

2. Slackbot on serverless

3. Generic HTML form handler on serverless

4. Self-picked challenge

Pick your’s

BLOG BACKEND

Create a backend for the blog application running at http://hackathon-blog.sandbox.sc5.io/

(sources at https://github.com/SC5/aws-serverless-hackathon)

Backend must have a REST API with methods

1. POST /dev/posts

2. GET /dev/posts

3. PUT /dev/posts/{postId} - OPTIONAL

4. DELETE /dev/posts/{postId} – OPTIONAL

Use e.g. AWS DynamoDB as the database for blog posts.

Step-by-step walkthrough available at http://hackathon-blog.sandbox.sc5.io/walkthrough.pdf

Guided challenge

RESOURCES

Blog client:

https://github.com/SC5/aws-serverless-hackathon

Sample blog backend:

https://github.com/SC5/aws-serverless-hackathon-backend

This presentation:

http://hackathon-blog.sandbox.sc5.io/walkthrough.pdf

Cheatsheet:

http://hackathon-blog.sandbox.sc5.io/cheatsheet.pdf

PRE-REQUISITES / SETUP

PRE-TAKEOFF CHECKLIST

q A laptop with Node 4 (recommended) or 5 installed

q An AWS account (https://aws.amazon.com/free)

q An AWS IAM user (admin rights) with access and secret keys(http://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_create.html)

q AWS access and secret keys set up(http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/node-configuring.html)

q AWS CLI recommended (but not required)(http://docs.aws.amazon.com/cli/latest/userguide/installing.html or”brew install awscli” for OSX homebrew users)

Getting prepared

TESTING THE SETUP

> mkdir test

> cd test> npm install aws-sdk

> echo "var AWS=require('aws-sdk');

var IAM = new AWS.IAM(); IAM.getUser(function(err, data) {

console.log(data.User.UserName)

});" | node

You should get your IAM account name as a response if set up correctly.

SERVERLESS BLOG WALKTHROUGH

1. CREATE SERVERLESS PROJECT

> sls project install -n="serverless-blog" sc5-serverless-boilerplate

> cd serverless-blog

> npm install

This creates a new project serverless-blog based on sc5-serverless-boilerplate and installs the node modules required by the project.

INFO

2. CREATE DYNAMODB TABLE FOR POSTS (USINGSERVERLESS)

q Serverless uses AWS Cloudformation to deployresources.

q Add snippet on the right to ”Resources” in s-resources-cf.json

q Deploy with> sls resources deploy

Alternatively, you can create thetable in the AWS console or usingthe CLI.

”BlogTable": { "Type": "AWS::DynamoDB::Table", "DeletionPolicy": "Retain", "Properties": { "AttributeDefinitions": [ {

"AttributeName": "id", "AttributeType": "S"

} ], "KeySchema": [ {

"AttributeName": "id", "KeyType": "HASH"

} ], "ProvisionedThroughput": {

"ReadCapacityUnits": 1, "WriteCapacityUnits": 1

}, "TableName": "${stage}-blog"

}}

Sample: https://github.com/SC5/aws-serverless-hackathon-backend/blob/master/s-resources-cf.json

3. SET PERMISSIONS FOR DYNAMODB

q Lambda functions need to begiven rights to the table created

q To grant permissions, addsnippet on the right to ”Resources.IAMPolicyLambda.PolicyDocument.Statement” in s-resources-cf.json

q Deploy with

> sls resources deploy

{ "Effect": "Allow", "Action": [ "dynamodb:Scan", "dynamodb:PutItem", "dynamodb:DeleteItem"

], "Resource": "arn:aws:dynamodb:${region}:*:*"

}

Sample: https://github.com/SC5/aws-serverless-hackathon-backend/blob/master/s-resources-cf.json

4. CREATE FUNCTION AND SET ENDPOINTS

> sls function create blog/posts

§ Select ”nodejs4.3” as your runtime and ”Create Endpoint”

§ In blog/posts/s-function.json, you will find an endpoint for GET in ”endpoints”

§ Based on the GET endpoint, create additional endpoints POST, PUT and DELETE

§ Set ”RequestTemplates” for the endpoints to ”$${restGet}” , ”$${restPost}”, ”$${restPut}” and ”$${restDelete}”. These aremappings defined in s-template.json

Sample: https://github.com/SC5/aws-serverless-hackathon-backend/blob/master/blog/posts/s-function.json

5. ENABLE CORS HEADERS FOR ENDPOINTS

q CORS headers need to beenabled so that theapplication can access theendpoints

q To add CORS headers, addsnippet on the right to ”custom” in blog/posts/s-function.json

"cors": { "allowOrigin": "*", "allowHeaders": ["Content-Type", "X-Amz-Date", "Authorization","X-Api-Key"

] }

Sample: https://github.com/SC5/aws-serverless-hackathon-backend/blob/master/blog/posts/s-function.json

6. IMPLEMENT THE LOGIC

§ Implement the logic for the function into blog/posts. The entrypoint for the Lambda function is handler.js in that folder.

§ Use e.g. AWS.DynamoDB.DocumentClient to access the databasetable. The table name is ${process.env.SERVERLESS_DATA_MODEL_STAGE}-blog

Samples: https://github.com/SC5/aws-serverless-hackathon-backend/blob/master/blog/posts/handler.jshttps://github.com/SC5/aws-serverless-hackathon-backend/blob/master/blog/posts/blog-storage.js

7. TEST THE FUNCTION

q sls function run can beused to run the function with theinput defined in event.json

q Copy the snippet on the right to blog/posts/event.json and run

> sls function run

q NOTE: sls-mocha-plugin is included in the boilerplate to support more advanced TDD

{ "method": "POST", "body": { "title": "Test post", "content" : "Test content"

}}

Sample: https://github.com/SC5/aws-serverless-hackathon-backend/blob/master/blog/posts/event.json

7. DEPLOY FUNCTIONS & ENDPOINTS

> sls function deploy posts > sls endpoint deploy --all

This deploys the functions and endpoinst (including CORS headers). sls dash deploy does not (currently) deploy the CORS headers.

You will get the URLs for the endpoints as response to endpoint deploy

8. SET UP ENDPOINTS IN THE SAMPLE APP

q Launch the blog application at

http://hackathon-blog.sandbox.sc5.io/

q Enter the endpoint URL (https://…/dev/posts) to the form and save

q Try writing, editing, deleting posts

9. YOU DID IT! CONGRATS!

Next, check opportunities at https://sc5.io/careers

THANK [email protected]