welcome to the aws serverless hackathon - AWS

Transcription

welcome to the aws serverless hackathon - AWS
WELCOME TO THE
AWS SERVERLESS HACKATHON
Mikael Puittinen, CTO
25.4.2016
1
A SHORT INTRODUCTION TO SC5
Introducing
SC5 BRIEFLY
CLOUD
SOLUTIONS
BUSINESS
APPLICATIONS
DIGITAL
DESIGN
10
60+
200+
YRS
CUSTOMERS
PROJECTS
65
HEL
JKL
6
HACKERS
DESIGNERS
MEUR
A short business case
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 bit of history
SC5 & SERVERLESS - BACKGROUND
Nov 2014
AWS Lambda released
AWS API Gateway released
July 2015
Sep 2015
Oct 2015
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)
Dec 2015
JAWS becomes Serverless Framework
Today
4 customer projects going on, 4 delivered based on AWS
Lambda (7 of which on Serverless)
INTRODUCTION TO LAMBDA ,
API GATEWAY AND
SERVERLESS
Serverless Compute
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
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
}
Serverless REST API
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
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
Pick your’s
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
Guided challenge
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
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
Getting prepared
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)
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
INFO
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-serverlessboilerplate and installs the node modules required by the project.
2. CREATE DYNAMODB TABLE FOR POSTS (USING
SERVERLESS)
q Serverless uses AWS
Cloudformation to deploy
resources.
q Add snippet on the right to
”Resources” in s-resourcescf.json
q Deploy with
> sls resources deploy
Alternatively, you can create the
table in the AWS console or using
the CLI.
”BlogTable": {
"Type": "AWS::DynamoDB::Table",
"DeletionPolicy": "Retain",
"Properties": {
"AttributeDefinitions ": [ {
"AttributeName": "id",
"AttributeType": "S"
} ],
"KeySchema": [ {
"AttributeName": "id",
"KeyType": "HASH"
} ],
"ProvisionedThroughpu t": {
"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 be
given rights to the table created
q To grant permissions, add
snippet on the right to
”Resources.IAMPolicyLambda
.PolicyDocument.Statement”
in s-resources-cf.json
{
"Effect": "Allow",
"Action": [
"dynamodb:Scan",
"dynamodb:PutItem",
"dynamodb:DeleteItem"
],
"Resource": "arn:aws:dynamodb:${re gion }:*: *"
}
q Deploy with
> sls resources deploy
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 are
mappings 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 be
enabled so that the
application can access the
endpoints
q To add CORS headers, add
snippet on the right to
”custom” in blog/posts/sfunction.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 entry
point for the Lambda function is handler.js in that folder.
§ Use e.g. AWS.DynamoDB.DocumentClient to access the database
table. 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.js
https://github.com/SC5/aws-serverless-hackathon-backend/blob/master/blog/posts/blog-storage.js
7. TEST THE FUNCTION
q sls function run can be
used to run the function with the
input defined in event.json
q Copy the snippet on the right to
blog/posts/event.json and
run
{
"method": "POST",
"body": {
"title": "Test post",
"content" : "Test content"
}
}
> sls function run
q NOTE: sls-mocha-plugin is
included in the boilerplate to
support more advanced TDD
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 YOU!
Mikael.puittinen@sc5.io