Slide 1

Slide 1 text

Fun With Serverless JS Lorna Mitchell, IBM

Slide 2

Slide 2 text

The Serverless Revolution The big secret is: there ARE servers! @lornajane

Slide 3

Slide 3 text

What is Serverless? Backend functions, deployed to the cloud, scaling on demand. Pay as you go. @lornajane

Slide 4

Slide 4 text

The Serverless Revolution FaaS: Functions as a Service Developer focus: • the outputs • the inputs • the logic in between Charges are usually per GBsec @lornajane

Slide 5

Slide 5 text

When To Go Serverless • For occasional server needs (contact form on static site) • For very variable traffic levels (millions of browsers requesting update) • To provide extra compute resource without extending existing platform (classic example: PDF generation) @lornajane

Slide 6

Slide 6 text

Serverless Platforms • Amazon Lambda • IBM Cloud Functions (aka OpenWhisk) • Twilio Functions • Azure Functions • Google Cloud Functions • Iron Functions • .... and more. Every day there are more. @lornajane

Slide 7

Slide 7 text

Getting Started @lornajane

Slide 8

Slide 8 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 9

Slide 9 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 10

Slide 10 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 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

IBM Cloud Functions Get the wsk CLI tool or the bx tool with wsk plugin, then log in Zip and deploy/update your code like this: zip hello.zip index.js wsk action update --kind nodejs:6 demo/hello1 hello.zip demo is the package name @lornajane

Slide 14

Slide 14 text

Bluemix OpenWhisk Run your action from the CLI: wsk action invoke --blocking demo/hello1 Enable web access and web request your action: wsk action update demo/hello1 --web true curl https://openwhisk.ng.bluemix.net/api/v1/web/ \ Lorna.Mitchell_Working/demo/hello1.json @lornajane

Slide 15

Slide 15 text

Serverless + HTTP FaaS + HTTP = Microservices! @lornajane

Slide 16

Slide 16 text

The Fun Part @lornajane

Slide 17

Slide 17 text

Alexa: Amazon Echo You speak, the device sends the sound to the cloud and speaks back the response @lornajane

Slide 18

Slide 18 text

Invoking Alexa @lornajane

Slide 19

Slide 19 text

Invoking Alexa @lornajane

Slide 20

Slide 20 text

Invoking Alexa @lornajane

Slide 21

Slide 21 text

Invoking Alexa @lornajane

Slide 22

Slide 22 text

Example: Project Codename Existing npm library: https://www.npmjs.com/package/project-name-generator Skill code on GitHub: https://github.com/lornajane/alexa-project-codename "Alexa, ask Project Codename for a new project name" @lornajane

Slide 23

Slide 23 text

Project Codename: Code function main(args) { var generate = require('project-name-generator'); var random = generate().spaced; var response = { "version": "1.0", "response" :{ "shouldEndSession": true, "outputSpeech": { "type": "PlainText", "text": "project codename. " + random } } } return(response); } exports.main = main; @lornajane

Slide 24

Slide 24 text

Project Codename: Run wsk action invoke --blocking alexa/project-codename ... random 2-word responses appear. @lornajane

Slide 25

Slide 25 text

Redis Integration As an example, storing the new project codename in Redis: if(args.redisURL) { bluebird.promisifyAll(redis.RedisClient.prototype); var client = redis.createClient(args.redisURL); return client.setAsync(["codenames", random]) .then(function (result) { return response; }); } else { return response; } @lornajane

Slide 26

Slide 26 text

A Fun Conversation @lornajane

Slide 27

Slide 27 text

Quiz: the Setup This skill asks the user a simple maths question • Intent: Quiz • Slots: Number (there are predefined data types) The "dialog" feature helps to prompt the user for input There is a "session" to store data between requests @lornajane

Slide 28

Slide 28 text

Quiz: the Conversation Human Alexa, ask Lorna for a quiz Alexa Ready? What is three plus five? Human Eight! Alexa Well done! Eight is correct. OR Human Fourty-two! Alexa You said fourty-two, but the correct answer is eight. @lornajane

Slide 29

Slide 29 text

Quiz: Step by Step Human Alexa, ask Lorna for a quiz { "version":"1.0", "session":{ "new":true }, "request":{ "type":"IntentRequest", "intent":{"name":"Quiz", "confirmationStatus":"NONE", "slots":{ "Number":{ "name":"Number", "confirmationStatus":"NONE" } } }, "dialogState":"STARTED" } } @lornajane

Slide 30

Slide 30 text

Quiz: Step by Step Alexa Ready? What is three plus five? { "outputSpeech":{ "type":"PlainText", "text":"Ready? What is 3 plus 5?" }, "directives":[ { "type":"Dialog.ElicitSlot", "slotToElicit":"Number" } ], "shouldEndSession":"false" } Store "What is three plus five?" and "8" in the session @lornajane

Slide 31

Slide 31 text

Quiz: Step by Step Human Eight! { "version": "1.0", "session": { "new": false }, "request": { "type": "IntentRequest", "intent": { "name": "Quiz", "confirmationStatus": "NONE", "slots": { "Number": { "name": "Number", "value": "8", "confirmationStatu } }, "dialogState": "IN_PROGRESS" } } @lornajane

Slide 32

Slide 32 text

Quiz: Step by Step Alexa Well done! Eight is correct. { "outputSpeech":{ "type":"PlainText", "text":"Well done! 8 is correct" }, "shouldEndSession":"true" } @lornajane

Slide 33

Slide 33 text

Serverless In The Real World Beyond the trivial example, many things are possible: • connect to a datastore • make an API call • trigger other actions • ... your imagination is the limit @lornajane

Slide 34

Slide 34 text

The Serverless Revolution @lornajane

Slide 35

Slide 35 text

Resources • Project codename skill: https://github.com/lornajane/alexa-project-codename • Serverless framework: https://github.com/serverless/serverless • IBM Cloud Functions: https://www.ibm.com/cloud-computing/bluemix/openwhisk • My blog: https://lornajane.net @lornajane