Slide 1

Slide 1 text

Serverless Microservices Are The New Black Lorna Mitchell, IBM

Slide 2

Slide 2 text

Serverless The secret is: there ARE servers! @lornajane

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Serverless Platforms • Amazon Lambda • IBM Cloud Functions (aka Apache OpenWhisk) • Twilio Functions • Azure Functions • Google Cloud Functions • .... and others arriving all the time @lornajane

Slide 6

Slide 6 text

Getting Started @lornajane

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Amazon Lambda (if you want to edit your code and redeploy it) aws lambda update-function-code \ --function-name hello1 \ --zip-file fileb://hello.zip Run your lambda function: aws lambda invoke --function-name hello1 output.txt @lornajane

Slide 10

Slide 10 text

Hello World Lambda exports.handler = function(event, context) { context.succeed("Hello, World!"); }; @lornajane

Slide 11

Slide 11 text

Hello World OpenWhisk exports.main = function(args) { return({"message": "Hello, World!"}); }; @lornajane

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

FaaS + HTTP = Microservices! @lornajane

Slide 15

Slide 15 text

Microservices Microservices are: • small and modular • loosely coupled • independently developed and deployed • great for building components • decentralised ... they are basically small HTTP APIs @lornajane

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

Microservices + API Gateway @lornajane

Slide 19

Slide 19 text

Fetching Plans Code for retrieving the plans from the 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 db.any("SELECT * FROM plans", [true]) 6 .then(function(data) { 7 resolve({body: {plans: data}, statusCode: 200}); 8 }) 9 .catch(function(error) { 10 reject({body: "An error occurred", statusCode: 400}); 11 }); 12 }); @lornajane

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Fetching Plans Now let's actually fetch them! $ curl https://service.eu.apiconnect.ibmcloud.com/.../plans { "plans": [{ "plan_id": 1, "travel_date": "2018-02-28T00:00:00.000Z", "location": "London" }, { "plan_id": 2, "travel_date": "2018-05-12T00:00:00.000Z", "location": "Copenhagen" }] } @lornajane

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Making Plans Time to actually make plans! $ curl -L -H "Content-Type: application/json" \ https://service.eu.apiconnect.ibmcloud.com/.../plans \ -d '{"location": "Turin", "travel_date": "2018-04-11"}' { "plans": [{ "plan_id": 3, "travel_date": "2018-04-11T00:00:00.000Z", "location": "Turin" }] } @lornajane

Slide 25

Slide 25 text

Serverless Microservices @lornajane

Slide 26

Slide 26 text

Serverless Ideal for working with many small parts Paired with API Gateway for routing and security: perfect candidate for microservices @lornajane

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Resources • Example code: https://github.com/lornajane/plans-microservice • IBM Cloud Functions: https://www.ibm.com/cloud/functions • My blog: https://lornajane.net @lornajane