an automated laser pointer for your dog : aws iot & lambda
TRANSCRIPT
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
FIRST THERE WAS…
Boredom Sleeping Play Time
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!
EXPLODED VIEW OF X/Y BOT
Designed, assembled, and 3D modeled by ERIC SARELLANA
Finished automated laser pointer device
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)
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.
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.
CREATE THING(IF DEMO DIDN’T WORK)
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.
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.
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
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
• 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
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
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)
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.
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
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.
TESTING LAMBDA FUNCTIONS AND ALIASES
• DEMO
NOW…
CATCH THAT LASER!
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