-
-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
.serverless |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<!-- | ||
title: AWS Serverless IoT Event example in NodeJS | ||
description: This example demonstrates how to setup a AWS IoT Rule to send events to a Lambda function. | ||
layout: Doc | ||
--> | ||
# Serverless IoT Event | ||
|
||
This example demonstrates how to setup a AWS IoT Rule to send events to a Lambda function. | ||
|
||
## Use-cases | ||
|
||
- Analytics for IoT events | ||
- Reacting on IoT events | ||
|
||
## Setup | ||
|
||
In order to deploy the function simply run | ||
|
||
```bash | ||
serverless deploy | ||
``` | ||
|
||
The expected result should be similar to: | ||
|
||
```bash | ||
Serverless: Packaging service... | ||
Serverless: Uploading CloudFormation file to S3... | ||
Serverless: Uploading service .zip file to S3 (363 B)... | ||
Serverless: Updating Stack... | ||
Serverless: Checking Stack update progress... | ||
................ | ||
Serverless: Stack update finished... | ||
Service Information | ||
service: aws-node-iot-event | ||
stage: dev | ||
region: us-east-1 | ||
api keys: | ||
None | ||
endpoints: | ||
None | ||
functions: | ||
aws-node-iot-event-dev-log: arn:aws:lambda:us-east-1:377024778620:function:aws-node-iot-event-dev-log | ||
``` | ||
|
||
## Usage | ||
|
||
In `serverless.yml` the log-function is configured to receive any event from the IoT Topic `mybutton`. We now can go to the IoT Console and visit the Tab `Test`. | ||
|
||
 | ||
|
||
There fill `mybutton` into the topic input field in the publish section. Replace existing example data with the following example and press the publish button. | ||
|
||
```json | ||
{ | ||
"message": "My first IoT event", | ||
"value": 2 | ||
} | ||
``` | ||
|
||
 | ||
|
||
To verify that our event was forwarded to our log-function run | ||
|
||
```bash | ||
serverless logs --function log | ||
``` | ||
|
||
The expected result should be similar to: | ||
|
||
```bash | ||
START RequestId: 24192153-d10f-11e6-936c-a98ff4127599 Version: $LATEST | ||
2017-01-02 18:16:04.768 (+01:00) 24192153-d10f-11e6-936c-a98ff4127599 { message: 'My first IoT event', value: 2 } | ||
END RequestId: 24192153-d10f-11e6-936c-a98ff4127599 | ||
REPORT RequestId: 24192153-d10f-11e6-936c-a98ff4127599 Duration: 23.53 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 8 MB | ||
``` | ||
In the output you can see the IoT event that has been triggered from the test console. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict'; | ||
|
||
module.exports.log = (event, context, callback) => { | ||
console.log(event); | ||
callback(null, {}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "aws-iot-example", | ||
"version": "1.0.0", | ||
"description": "Example on how to setup a AWS IoT Rule to send events to a Lambda function", | ||
"author": "", | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
service: aws-iot-example | ||
|
||
frameworkVersion: ">=1.5.0 <2.0.0" | ||
|
||
provider: | ||
name: aws | ||
runtime: nodejs4.3 | ||
|
||
functions: | ||
log: | ||
handler: handler.log | ||
events: | ||
- iot: | ||
sql: "SELECT * FROM 'mybutton'" |