Serverless FaaS: Functions as a Service • write a function (many languages supported) • deploy it to the cloud (Lambda, Cloud Functions, etc) • only pay while the function is running (charged per GBsec) • your platform scales on demand @lornajane
When To Go Serverless • For occasional server needs (contact form on static site) • To provide compute power (processing quantities of data) • To move heavy lifting off web platform (classic example: PDF generation) • To create a small, scalable application (focussed API, microservices) @lornajane
Amazon Lambda • Install awscli command line tool (there is also a web interface) • Set up permissions via IAM and then use aws configure to get that set up • Write some code, then zip it (e.g. index.js -> hello.zip) @lornajane
Amazon Lambda Create your lambda function by supplying the zip file and some options: aws lambda create-function \ --function-name hello1 \ --runtime nodejs6.10 \ --role "arn:aws:iam::283476131276:role/service-role/Alexa" --description "A demo first Lambda function" \ --handler index.handler \ --zip-file fileb://hello.zip @lornajane
IBM Cloud Functions Get the bx tool with Cloud_Functions plugin, then log in Zip and deploy/update your code like this: zip hello.zip index.js bx wsk action update --kind nodejs:6 demo/hello1 hello.zip demo is the package name @lornajane
Bluemix OpenWhisk Run your action from the CLI: bx wsk action invoke --blocking demo/hello1 Enable web access and web request your action: bx wsk action update demo/hello1 --web true curl https://openwhisk.ng.bluemix.net/api/v1/web/ \ Lorna.Mitchell_Working/demo/hello1.json @lornajane
Microservices Microservices are: • small and modular • loosely coupled • independently developed and deployed • great for building components • decentralised ... they are basically small HTTP APIs @lornajane
Lorna's Plans Service Keep a list of my travel plans, with locations and dates. Use a serverless platform (IBM Cloud Functions) and PostgreSQL GET /plans list all plans GET /plans/42 show one plan POST /plans create a new plan @lornajane
Microservice Design Points I prefer RESTful-ish APIs • status codes are important • headers are for metadata • URLs and verbs together define what happens • all endpoints are stateless • SSL is required • security is a solved problem; try OAuth @lornajane
Fetching Plans Make this code run when we do GET /plans zip -rq get-plans.zip index.js node_modules bx wsk action update --kind nodejs:6 --web raw \ plans-api/get-plans get-plans.zip bx wsk api create /plans GET plans-api/get-plans \ --response-type http @lornajane
Making Plans Make a plan: parse data, insert to database 1 const pgp = require('pg-promise')(); 2 function main(params) { 3 return new Promise(function(resolve, reject) { 4 var db = pgp(params.postgres_url, []); 5 var d = new Buffer(params.__ow_body, 'base64').toString(); 6 var decoded = JSON.parse(d); 7 db.one("INSERT INTO plans (location, travel_date) VALUES ($ 8 [decoded.location, decoded.travel_date]) 9 .then(function(data) { 10 var r = params.base_url + "/" + data.plan_id; 11 resolve({headers: {"Location": r}, statusCode: 303}) 12 }); @lornajane
Making Plans Deploy the code and link it to POST /plans: zip -rq write-plan.zip index.js node_modules bx wsk action update --kind nodejs:6 --web raw \ plans-api/write-plan write-plan.zip bx wsk api create /plans POST plans-api/write-plan \ --response-type http @lornajane
Microservices Service Oriented Architecture is alive and well • microservices expose endpoints • they share reusable components • specific components guard access to services • each component can be separately developed, tested and deployed @lornajane
Resources • Example code: https://github.com/lornajane/plans-microservice • IBM Cloud Functions: https://www.ibm.com/cloud/functions • My blog: https://lornajane.net @lornajane