Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Serverless Microservices Are The New Black

Serverless Microservices Are The New Black

Presentation for my local Ladies of Code chapter in Manchester


Lorna Mitchell

March 06, 2018


  1. Serverless Microservices Are The New Black Lorna Mitchell, IBM

  2. Serverless The secret is: there ARE servers! @lornajane

  3. 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
  4. 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
  5. Serverless Platforms • Amazon Lambda • IBM Cloud Functions (aka

    Apache OpenWhisk) • Twilio Functions • Azure Functions • Google Cloud Functions • .... and others arriving all the time @lornajane
  6. Getting Started @lornajane

  7. 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
  8. 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
  9. 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
  10. Hello World Lambda exports.handler = function(event, context) { context.succeed("Hello, World!");

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

    }; @lornajane
  12. 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
  13. 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
  14. FaaS + HTTP = Microservices! @lornajane

  15. Microservices Microservices are: • small and modular • loosely coupled

    • independently developed and deployed • great for building components • decentralised ... they are basically small HTTP APIs @lornajane
  16. 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
  17. 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
  18. Microservices + API Gateway @lornajane

  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. Serverless Microservices @lornajane

  26. Serverless Ideal for working with many small parts Paired with

    API Gateway for routing and security: perfect candidate for microservices @lornajane
  27. 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
  28. Resources • Example code: https://github.com/lornajane/plans-microservice • IBM Cloud Functions: https://www.ibm.com/cloud/functions

    • My blog: https://lornajane.net @lornajane