Skip to content

Commit c4be530

Browse files
authored
Merge pull request #45 from serverless/iot-event
add iot example
2 parents 17f9f09 + a58a520 commit c4be530

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

aws-node-iot-event/README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Serverless IoT Event
2+
3+
This example demonstrates how to setup a AWS IoT Rule to send events to a Lambda function.
4+
5+
## Use-cases
6+
7+
- Analytics for IoT events
8+
- Reacting on IoT events
9+
10+
## Setup
11+
12+
In order to deploy the function simply run
13+
14+
```bash
15+
serverless deploy
16+
```
17+
18+
The expected result should be similar to:
19+
20+
```bash
21+
Serverless: Packaging service...
22+
Serverless: Uploading CloudFormation file to S3...
23+
Serverless: Uploading service .zip file to S3 (363 B)...
24+
Serverless: Updating Stack...
25+
Serverless: Checking Stack update progress...
26+
................
27+
Serverless: Stack update finished...
28+
Service Information
29+
service: aws-node-iot-event
30+
stage: dev
31+
region: us-east-1
32+
api keys:
33+
None
34+
endpoints:
35+
None
36+
functions:
37+
aws-node-iot-event-dev-log: arn:aws:lambda:us-east-1:377024778620:function:aws-node-iot-event-dev-log
38+
```
39+
40+
## Usage
41+
42+
In `serverless.yml` the log-function is configured to receive any event from the IoT Topic `my-button`. We now can go to the IoT Console and visit the Tab `Test`.
43+
44+
![iot-console-test](https://cloud.githubusercontent.com/assets/223045/21593597/352be866-d119-11e6-9639-994b9c495571.png)
45+
46+
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.
47+
48+
```json
49+
{
50+
"message": "My first IoT event",
51+
"value": 2
52+
}
53+
```
54+
55+
![iot-console-form](https://cloud.githubusercontent.com/assets/223045/21593596/352be71c-d119-11e6-979a-7aa70abd2bf2.png)
56+
57+
To verify that our event was forwarded to our log-function run
58+
59+
```bash
60+
serverless logs --function log
61+
```
62+
63+
The expected result should be similar to:
64+
65+
```bash
66+
START RequestId: 24192153-d10f-11e6-936c-a98ff4127599 Version: $LATEST
67+
2017-01-02 18:16:04.768 (+01:00) 24192153-d10f-11e6-936c-a98ff4127599 { message: 'My first IoT event', value: 2 }
68+
END RequestId: 24192153-d10f-11e6-936c-a98ff4127599
69+
REPORT RequestId: 24192153-d10f-11e6-936c-a98ff4127599 Duration: 23.53 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 8 MB
70+
```
71+
72+
In the output you can see the IoT event that has been triggered from the test console.

aws-node-iot-event/handler.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
module.exports.log = (event, context, callback) => {
4+
// eslint-disable-next-line no-console
5+
console.log(event);
6+
callback(null, {});
7+
};

aws-node-iot-event/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "aws-iot-event",
3+
"version": "1.0.0",
4+
"description": "Example on how to setup a AWS IoT Rule to send events to a Lambda function",
5+
"author": "",
6+
"license": "MIT"
7+
}

aws-node-iot-event/serverless.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
service: aws-node-iot-event
2+
3+
frameworkVersion: ">=1.5.0 <2.0.0"
4+
5+
provider:
6+
name: aws
7+
runtime: nodejs4.3
8+
9+
functions:
10+
log:
11+
handler: handler.log
12+
events:
13+
- iot:
14+
sql: "SELECT * FROM 'mybutton'"

0 commit comments

Comments
 (0)