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
Tweet

More Decks by Lorna Mitchell

Other Decks in Technology

Transcript

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

    Apache OpenWhisk) • Twilio Functions • Azure Functions • Google Cloud Functions • .... and others arriving all the time @lornajane
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. Microservices Microservices are: • small and modular • loosely coupled

    • independently developed and deployed • great for building components • decentralised ... they are basically small HTTP APIs @lornajane
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. Serverless Ideal for working with many small parts Paired with

    API Gateway for routing and security: perfect candidate for microservices @lornajane
  19. 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