an automated laser pointer for your dog : aws iot & lambda

21
AWS IOT & LAMBDA A TALE ABOUT A DOG AND A LASER POINTER BY LISA KESTER Twitter @injectgeek Walkthrough at https://www.hackster.io/happy-puppy-or-not/fido-s-automated-lazer-pointer-9be48a

Upload: lisa-kester

Post on 16-Apr-2017

364 views

Category:

Software


0 download

TRANSCRIPT

Page 1: An Automated Laser Pointer for Your Dog : Aws IoT & Lambda

A W S I O T & L A M B D A A TALE ABOUT A DOG AND A LASER POINTER

BY LISA KESTERTwitter @injectgeek

Walkthrough athttps://www.hackster.io/happy-puppy-or-not/fido-s-automated-lazer-pointer-9be48a

Page 2: An Automated Laser Pointer for Your Dog : Aws IoT & Lambda

FIRST THERE WAS…

Boredom Sleeping Play Time

Page 3: An Automated Laser Pointer for Your Dog : Aws IoT & Lambda

WHAT’S INVOLVED IN AUTOMATING THE LAZER POINTER?

• AWS IoT service (device communication)

• AWS Lambda function (REST API access)

• Servo movement program on Arduino

• Raspberry PI program to listen for commands

• Connect hardware: servos, Raspberry pi, Arduino, breadboard, wires

• 3D print, design, and assemble the X/Y Bot

BRING DOG!

Page 4: An Automated Laser Pointer for Your Dog : Aws IoT & Lambda

EXPLODED VIEW OF X/Y BOT

Designed, assembled, and 3D modeled by ERIC SARELLANA

Finished automated laser pointer device

Page 5: An Automated Laser Pointer for Your Dog : Aws IoT & Lambda

WHAT ARE WE GOING TO TALK ABOUT?(WHILE THE LASER POINTER RANDOMLY GOES OFF)

• IoT service overview• How to setup the IoT service. (DEMO)• Raspberry pi script (AWS IoT sdk)• Lambda service overview• How to setup the lambda service (DEMO)• How to add a REST API call for the Lambda function (DEMO)• How to schedule the Lambda function (DEMO)• Lambda function testing and aliases (DEMO)

Page 6: An Automated Laser Pointer for Your Dog : Aws IoT & Lambda

IOT SERVICE OVERVIEW

HOW IT WORKS• Thing, Certificate, and Policy.• A message is sent to the device (JSON)• Shadow saves the message as internal state and sends it to the device (JSON)• Shadow retains state when device is off• Device reacts to JSON via pub/sub

model• Optional Rules Engine (sends JSON to

Lambda, Dynamo DB, some others…).

Device“Thing”

Sends JSON Message with REST

AWS Services

Shadow

Rules EngineWHAT’S IT FOR? The IoT service gives you a way to control your device, route data to AWS services. It includes security and control over your users.

Page 7: An Automated Laser Pointer for Your Dog : Aws IoT & Lambda

DEMO IOT SETUP

• Create thing• Create certificate • Create policy• Attach certificate to policy and thing via “Actions” button• NOTE: To delete an item, click on it and select “Delete” from the “Actions” button.

You may have to disable the certificate or detach the thing and policy to delete individual items.

OR… After you create a thing, click on it, there is an option to “Connect to a Device” in the Thing Details. Follow the prompts.

Page 8: An Automated Laser Pointer for Your Dog : Aws IoT & Lambda

CREATE THING(IF DEMO DIDN’T WORK)

Page 9: An Automated Laser Pointer for Your Dog : Aws IoT & Lambda

CREATE CERTIFICATE & POLICYThe certificate and policy allow your device to interact with the IoT service and receive messages from the cloud. You’ll need additional policy(s)/roles for any rules you create.

Page 10: An Automated Laser Pointer for Your Dog : Aws IoT & Lambda

GET JSONMAKE SURE YOU• Download all the certificates – some

aren’t available again!• Save the JSON in this screen. You’ll use it

when you write the JavaScript program on your device.

• README on GitHub has root-CA.crt. (rename the extension if it’s wrong).

• Example “echo” program is great to get you started listening for messages from the IoT Service.

Page 11: An Automated Laser Pointer for Your Dog : Aws IoT & Lambda

RASPBERRY PI SCRIPT(Using AWS IoT JavaScript SDK)

Setup• Install NodeJS & AWS IoT JavaScript SDK on the Raspberry PI

What does this do?• It allows us to listen for changes to the shadow JSON and DO something as a

result.• Subscribes to the MQTT Topic (pub/sub)• Authenticates via certificates

Page 12: An Automated Laser Pointer for Your Dog : Aws IoT & Lambda

AWS LAMBDA SERVICE OVERVIEW• Run single functions • No environment setup.• Fault-tolerant (auto-retries), auto-scaling, replication, redundancy, sync/async,

zero maintenance!• JavaScript, Java, Python supported• Full access to AWS JavaScript, Java, and Python SDKs built in. • Processing power and RAM assigned together (1.5GB Max RAM)• Lambda functions aren’t publicly accessible (Extra secure)• AWS API Gateway integration (public/private API access)• Schedule execution of lambda functions (Cloudwatch events)• Cost = execution time & processing power

Page 13: An Automated Laser Pointer for Your Dog : Aws IoT & Lambda

• Create a JavaScript NodeJS function in index.js file• Package program and dependencies in ZIP file• Event and context give you access to parameters/JSON passed to the lambda

function.

HOW TO CREATE A LAMBDA FUNCTION(USING NODEJS)

Index.jsvar aws = require('aws-sdk');exports.handler = function(event, context) { // Do Everything here}

FILE NAME for config

HANDLER NAME for config

Page 14: An Automated Laser Pointer for Your Dog : Aws IoT & Lambda

CREATE LAMBDA FUNCTION

STEPS

• Go to the Lambda Service in AWS• Create a new function• Select your preferred language • Select blueprint (for help to get

started)• Or Skip to create function from

scratch

Page 15: An Automated Laser Pointer for Your Dog : Aws IoT & Lambda

CONFIGURE LAMBDA FUNCTIONSETUP HIGHLIGHTSRuntime – determines language

Handler name MUST match the function name in code.

Index is the name of the file that contains the handler function.

Role is for permissions. Lambda has some good defaults but you can create one in IAM.

Memory determines RAM and PROCESSING POWER.

Timeout is a failsafe so your function is killed to minimize cost.

VPC is optional (as described)

Page 16: An Automated Laser Pointer for Your Dog : Aws IoT & Lambda

ADD CODE

3 OPTIONS

• Write inline code• Upload a zip file • Upload to S3

• Uploading a zip file to S3 allows you to update the lambda function by just updating the file in S3.

• If you upload a zip file inline, make sure you keep a copy. I haven’t seen a download option.

Page 17: An Automated Laser Pointer for Your Dog : Aws IoT & Lambda

CREATE API GATEWAY ENDPOINT

HIGHLIGHTS• API Name is the name of the API

created in API Gateway• Resource name is the REST endpoint

name • Security gives you options to allow

anyone to use the REST endpoint or to require a Gateway API key

Page 18: An Automated Laser Pointer for Your Dog : Aws IoT & Lambda

SCHEDULE THE LAZER POINTER EVENTS

CRON EXPRESSION Rules/Examples here

STEPS• The Event Sources tab has “Add Event

Source” • Select CloudWatch Events. • Assign a schedule expression.

Page 19: An Automated Laser Pointer for Your Dog : Aws IoT & Lambda

TESTING LAMBDA FUNCTIONS AND ALIASES

• DEMO

Page 20: An Automated Laser Pointer for Your Dog : Aws IoT & Lambda

NOW…

CATCH THAT LASER!

Page 21: An Automated Laser Pointer for Your Dog : Aws IoT & Lambda

Q U E S T I O N S ?A TALE ABOUT A DOG AND A LASER POINTER

BY LISA KESTERTwitter @injectgeek

Walkthrough athttps://www.hackster.io/happy-puppy-or-not/fido-s-automated-lazer-pointer-9be48a