srv408 deep dive on aws iot

77
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Daniel Austin, Principal Solutions Architect April 19, 2017 Deep Dive on AWS IoT Shadows, Rules, and More

Upload: amazon-web-services

Post on 21-Apr-2017

81 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: SRV408 Deep Dive on AWS IoT

© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Daniel Austin, Principal Solutions Architect

April 19, 2017

Deep Dive on AWS IoT Shadows, Rules, and More

Page 2: SRV408 Deep Dive on AWS IoT

AWS IoT

Data storage & analytics

Administration

Sensors

Actuators

Connected Farm

Control automation

Page 3: SRV408 Deep Dive on AWS IoT

Agenda

• Introduction to AWS IoT • Telemetry & analytics • Cloud control • Mobile control • Lifecycle management • Wrap up • Appendix: Managing software updates

Page 4: SRV408 Deep Dive on AWS IoT

AWS IoT

DEVICE SDK Set of client libraries to

connect, authenticate and exchange messages

DEVICE GATEWAY Communicate with devices via

MQTT and HTTP

AUTHENTICATION AUTHORIZATION

Secure with mutual authentication and encryption

RULES ENGINE Transform messages based on rules and

route to AWS services

AWS services - - - - -

3P services

DEVICE SHADOW Persistent thing state

during intermittent connections

APPLICATIONS

AWS IoT API

DEVICE REGISTRY Identity and management of

your things

Page 5: SRV408 Deep Dive on AWS IoT

Key takeaways

• Messaging • Be careful with wide fan out • No message ordering guarantees • Avoid large fan in • WebSockets for Cognito authentication

• Rules • Send data to multiple data stores at the same time • Manage device lifecycle events

• Shadows • Designed for the real world: poor connectivity, out of order messages • Fine-grained control over software rollouts • Not ideal for storing time-series analytics data

• Security • One cert per device • Set fine-grained permissions for devices and Cognito users • Naming conventions can simplify policy management

Page 6: SRV408 Deep Dive on AWS IoT

Telemetry & Analytics

Page 7: SRV408 Deep Dive on AWS IoT

Administration Actuators

Control automation

AWS IoT

Data storage & analytics

Sensors

Connected Farm

Page 8: SRV408 Deep Dive on AWS IoT

AWS IoT Telemetry & Analytics

1. Connect devices 2. Send data 3. Collect & store the data 4. Do something with the data

Page 9: SRV408 Deep Dive on AWS IoT

AWS IoT Telemetry & Analytics

DEVICE GATEWAY Communicate with devices via

MQTT and HTTP

AUTHENTICATION AUTHORIZATION

Secure with mutual authentication and encryption

RULES ENGINE Transform messages based on rules and

route to AWS services

AWS services - - - - -

3P Services

Page 10: SRV408 Deep Dive on AWS IoT

1) Connect the devices

1. Provision a certificate

2. Attach policy

3. Connect over MQTT

Page 11: SRV408 Deep Dive on AWS IoT

2) Send data

PUBLISH macdonald/sensors/123 (qos: 0)

{

"timestamp": "2016-01-29T10:00:00",

"temperature": 55

"humidity": 39,

"ph": 6.7

}

Page 12: SRV408 Deep Dive on AWS IoT

3) Collect the data

AWS IoT Data storage & analytics

Sensors ?

Page 13: SRV408 Deep Dive on AWS IoT

Single consumer (don’t do this)

AWS IoT instance database

PUBLISH sensors/123

PUBLISH sensors/456

SUBSCRIBE sensors/+

PUBLISH sensors/789

Page 14: SRV408 Deep Dive on AWS IoT

Don’t do this: scalability

AWS IoT instance

SUBSCRIBE #

Page 15: SRV408 Deep Dive on AWS IoT

Don’t do this: availability

AWS IoT instance

Page 16: SRV408 Deep Dive on AWS IoT

Don’t do this: maintainability

AWS IoT

Page 17: SRV408 Deep Dive on AWS IoT

Store it in the device shadow (don’t do this)

Sensors

DEVICE SHADOWS

Page 18: SRV408 Deep Dive on AWS IoT

1. AWS Services (Direct Integration)

Rules Engine

Actions

AWS IoT Rules Engine

Lambda SNS SQS

S3 Amazon Kinesis DynamoDB RDS

Amazon Redshift

Amazon Glacier

EC2

3. External Endpoints (via Lambda and SNS)

Rules Engine connects AWS IoT to external endpoints and AWS services.

2. Rest of AWS (via Amazon Kinesis, Lambda, S3, and more)

Page 19: SRV408 Deep Dive on AWS IoT

Example rule { "rule": { "sql": "SELECT * AS message FROM 'sensors/#'", "description": "Store all sensor data into dynamodb and firehose", "actions": [{ "dynamoDB": { "tableName": "sensor_data", "roleArn": "arn:aws:iam::123456789012:role/aws_iot_dynamoDB", "hashKeyField": "sensor_id", "hashKeyValue": "${topic(2)}", "rangeKeyField": "timestamp“ "rangeKeyValue": "${timestamp()}", } }, { "firehose": { "roleArn": "arn:aws:iam::123456789012:role/aws_iot_firehose", "deliveryStreamName": "my_firehose_stream" } }] } }

Page 20: SRV408 Deep Dive on AWS IoT

Different Data Scenarios

Want to run a lot of queries constantly? Use Amazon Kinesis Firehose to write into Amazon Redshift Need fast lookups, e.g. in Rules or Lambda functions? Write into DynamoDB, add indexes if necessary Have a need for heavy queries but not always-on? Use Firehose & S3, process with Amazon EMR.

Page 21: SRV408 Deep Dive on AWS IoT

Takeaways

• Avoid single “firehose” MQTT consumer architecture

• Rules scalably route data into the rest of AWS

• Fork data into multiple data stores simultaneously

• Avoid the device shadow for analytics

Page 22: SRV408 Deep Dive on AWS IoT

Cloud Control

Page 23: SRV408 Deep Dive on AWS IoT

Administration

AWS IoT

Data storage & analytics

Sensors

Connected Farm

Actuators

Control automation

Page 24: SRV408 Deep Dive on AWS IoT

Automated Sprinkler Service

Amazon Kinesis

Amazon Machine Learning

Amazon Redshift

Rules Engine

Device Gateway

Sensor

Sprinkler

Amazon Kinesis– enabled app

Page 25: SRV408 Deep Dive on AWS IoT

Talking back to the sprinklers

Amazon Kinesis

Amazon Machine Learning

Amazon Redshift

Rules Engine

Sensor

Device Gateway

Sprinkler

Amazon Kinesis– enabled app

Page 26: SRV408 Deep Dive on AWS IoT

Publish on/off to the sprinkler (don’t do this)

Device Gateway

Sprinkler Control

logic

SUBSCRIBE macdonald/sprinkler-456

Page 27: SRV408 Deep Dive on AWS IoT

Publish on/off to the sprinkler (don’t do this)

Device Gateway

Sprinkler Control

logic

PUBLISH macdonald/sprinkler-456 { "water": "on" }

Page 28: SRV408 Deep Dive on AWS IoT

Direct publishing: why not?

Device Gateway

Sprinkler (offline) Control

logic

PUBLISH macdonald/sprinkler-456 { "water": "on" }

Page 29: SRV408 Deep Dive on AWS IoT

Direct publishing: why not?

Sprinkler

Control logic

on

off

Device Gateway

off

on

Page 30: SRV408 Deep Dive on AWS IoT

Direct publishing: why not?

• Messages aren’t ordered

• Connection blips

So then what?

Page 31: SRV408 Deep Dive on AWS IoT

Device Shadows

Shadow State

Apps

offline

Page 32: SRV408 Deep Dive on AWS IoT

Device Shadows

Device Controller

reported state

desired state

Page 33: SRV408 Deep Dive on AWS IoT

Device Shadows

Device Controller

reported state

desired state

HTTP/REST WebSockets

MQTT

Page 34: SRV408 Deep Dive on AWS IoT

AWS IoT Shadow - Simple Yet Powerful

{ "state" : { “desired" : { "lights": { "color": "RED" }, "engine" : "ON" }, "reported" : { "lights" : { "color": "GREEN" }, "engine" : "ON" }, "delta" : { "lights" : { "color": "RED" } } }, "version" : 10

}

Thing

Report its current state to one or multiple shadows Retrieve its desired state from shadow

Mobile App

Set the desired state of a device Get the last reported state of the device Delete the shadow

Shadow

Shadow reports delta, desired and reported states along with metadata and version

Page 35: SRV408 Deep Dive on AWS IoT

Device Shadows and versioning

Sprinkler

Control logic

on (version=1)

off (version=2)

Device Gateway

off (version=2)

on (version=1)

(old message ignored by device)

Page 36: SRV408 Deep Dive on AWS IoT

Takeaways

• Plan for devices losing connectivity

• Send devices commands through shadows

• Query device state through shadows

• Version numbers control concurrency

Page 37: SRV408 Deep Dive on AWS IoT

Mobile Control

Page 38: SRV408 Deep Dive on AWS IoT

Data storage & analytics

Sensors

Talking back to the sprinklers: manual override

Control automation

AWS IoT

Administration Actuators

Page 39: SRV408 Deep Dive on AWS IoT

AWS IoT

DEVICE SHADOW Persistent thing state

during intermittent connections

APPLICATIONS

Page 40: SRV408 Deep Dive on AWS IoT

Using Cognito with IoT

DEVICE SHADOW Persistent thing state

during intermittent connections

APPLICATIONS

AMAZON COGNITO PERMISSIONS APIs

Configure device and Cognito User permissions

end-user (farmer)

Page 41: SRV408 Deep Dive on AWS IoT

end-user (farmer)

Using Cognito with IoT

DEVICE SHADOW Persistent thing state

during intermittent connections

APPLICATIONS

AMAZON COGNITO PERMISSIONS APIs

Configure device and Cognito User permissions

Page 42: SRV408 Deep Dive on AWS IoT

Policy for Cognito with IoT

Cognito identity pool policy: { "Effect": "Allow", "Action": "iot:UpdateThingShadow", "Resource": "*" } Specific policy for Old Macdonald Cognito user: { "Effect": "Allow", "Action": "iot:UpdateThingShadow", "Resource": "arn:aws:iot:…:thing/macdonald-sprinkler123" }

Page 43: SRV408 Deep Dive on AWS IoT

Overall Cognito “pairing” workflow

1. Create a Cognito identity pool 2. Customer signs in using mobile app 3. Associate their user with their “farm” 4. Create a scope-down policy in IoT for their user 5. Attach that policy to their Cognito user in IoT

Page 44: SRV408 Deep Dive on AWS IoT

Managing fine-grained permissions

• One “farm owner” needs permissions to many shadows • "arn:aws:iot:…:thing/sprinkler123abc" • "arn:aws:iot:…:thing/sprinkler456def" • …

• Listing each is tedious

Page 45: SRV408 Deep Dive on AWS IoT

Best practice: Thing name prefixing

• Prefix thing name with logical owner • sensor123abc -> macdonald-sensor123abc

• Aspen policy supports wildcards

• "arn:aws:iot:…:thing/sensor123abc"

• "arn:aws:iot:…:thing/sensor123abc"

• "arn:aws:iot:…:thing/sensor456def"

• …

• "arn:aws:iot:…:thing/macdonald-*"

Page 46: SRV408 Deep Dive on AWS IoT

Takeaways: Cognito authorization

• Cognito enables secure human control over IoT devices

• IoT scope-down policy supports fine-grained control

• Naming conventions simplify policy management

Page 47: SRV408 Deep Dive on AWS IoT

Lifecycle Management

Page 48: SRV408 Deep Dive on AWS IoT

Actuators

Data storage & analytics

Device lifecycle management

Control automation

AWS IoT

Sensors

Maintenance

1

Page 49: SRV408 Deep Dive on AWS IoT

Lifecycle workflow

Notify operator

1

Connected Disconnected Still disconnected?

Page 50: SRV408 Deep Dive on AWS IoT

AWS IoT Rules Engine & Amazon SNS

Push Notifications Apple APNS Endpoint, Google GCM Endpoint, Amazon ADM Endpoint, Windows WNS Amazon SNS -> HTTP Endpoint (or SMS or email) Call HTTP based 3rd party endpoints through SNS with subscription and retry support

SNS

2

Page 51: SRV408 Deep Dive on AWS IoT

Detecting disconnects

Disconnected Connected

Graceful disconnect

Crash

Back online

Page 52: SRV408 Deep Dive on AWS IoT

Lifecycle events

• Connect • PUBLISH lifecycle/sensor-123

{"status": "online"}

• Disconnect (graceful) • PUBLISH lifecycle/sensor-123

{"status": "offline"}

• Disconnect (crash) • PUBLISH lifecycle/sensor-123

{"status": "offline", "isCrash": true}

Page 53: SRV408 Deep Dive on AWS IoT

AWS IoT Rules Engine’s Flexibility

SELECT *, clientId() as MQTTClientId FROM 'one/rule' WHERE startsWith(topic(2), 'IME33') AND (state = 'INIT' OR hydro_temp > surface_temp)", "actions": [{ "republish": { "topic": "controllers/${substring(topic(3), 3, 5)}", }]

Page 54: SRV408 Deep Dive on AWS IoT

Handling lifecycle events

SELECT

status,

topic(2) as deviceId,

timestamp() as time,

isCrash

FROM lifecycle/#

WHERE status='offline'

- Look up mobile push ID for device owner - Send SNS mobile push

AWS Lambda Function

Page 55: SRV408 Deep Dive on AWS IoT

Delayed lifecycle events

SELECT

status,

topic(2) as deviceId,

timestamp() as time,

isCrash

FROM lifecycle/#

Device Status Time sensor-123 connected 11:30 …

- Double-check the status in DynamoDB - Send SNS push notification if still offline

- Store update device status in DynamoDB - If offline: enqueue an SQS message with

DelaySeconds

AWS Lambda Function

SQS Message (15 minutes later)

Amazon DynamoDB

Page 56: SRV408 Deep Dive on AWS IoT

Generating lifecycle events

• Connect • PUBLISH lifecycle/sensor-123

{"status": "online"}

• Disconnect (graceful) • PUBLISH lifecycle/sensor-123

{"status": "offline"}

• Disconnect (crash) • PUBLISH lifecycle/sensor-123

{"status": "offline", "isCrash": true}

Page 57: SRV408 Deep Dive on AWS IoT

Lifecycle events: connecting

1. CONNECT 2. PUBLISH lifecycle/sensor-123 {"state": "online"}

Page 58: SRV408 Deep Dive on AWS IoT

Lifecycle events: disconnecting

1. PUBLISH lifecycle/sensor-123 {“state”: “offline”} 2. DISCONNECT

Page 59: SRV408 Deep Dive on AWS IoT

Last Will and Testament

CONNECT message parts:

Protocol: MQTT 3.1.1

ClientId: abc

KeepAlive: 60 seconds

LastWill PUBLISH message:

Topic: foo/bar

QoS: 1

Payload: {"foo": "bar"}

Page 60: SRV408 Deep Dive on AWS IoT

Lifecycle events: connecting

1. CONNECT with LWT: PUBLISH lifecycle/sensor-123

{“crash”: true, “state”: “offline”}

2. PUBLISH lifecycle/sensor-123 {“state”:”online”}

Page 61: SRV408 Deep Dive on AWS IoT

Lifecycle events: simplified

• Automatic lifecycle PUBLISH messages PUBLISH $aws/events/presence/connected/abc123

{

"ClientId": "abc123",

"Principal": "arn:aws:...",

"Timestamp": "2016-01-31T11:30",

"Status": "disconnected",

“GracefulDisconnect": true

}

Page 62: SRV408 Deep Dive on AWS IoT

Takeaways: lifecycle management

• Publish messages, use LWT for lifecycle events

• SQS delayed messages and DynamoDB can reduce false positives

• Automatic lifecycle events

Page 63: SRV408 Deep Dive on AWS IoT

Wrap-up

Page 64: SRV408 Deep Dive on AWS IoT

AWS IoT

Data storage & analytics

Administration

Sensors

Actuators

Connected Farm

Control automation

Page 65: SRV408 Deep Dive on AWS IoT

AWS IoT

DEVICE SDK Set of client libraries to

connect, authenticate, and exchange messages

DEVICE GATEWAY Communicate with devices via

MQTT and HTTP

AUTHENTICATION AUTHORIZATION

Secure with mutual authentication and encryption

RULES ENGINE Transform messages based on rules and

route to AWS services

AWS services - - - - -

3P services

DEVICE SHADOW Persistent thing state

during intermittent connections

APPLICATIONS

AWS IoT API

DEVICE REGISTRY Identity and management of

your things

Page 66: SRV408 Deep Dive on AWS IoT

Key takeaways

• Messaging • Be careful with wide fan out • No message ordering guarantees • Avoid large fan-in • WebSockets for Cognito authentication

• Rules • Send data to multiple data stores at the same time • Manage device lifecycle events

• Shadows • Designed for the real world: poor connectivity, out of order messages • Fine-grained control over software rollouts • Not ideal for storing time-series analytics data

• Security • One cert per device • Set fine-grained permissions for devices and Cognito users • Naming conventions can simplify policy management

Page 67: SRV408 Deep Dive on AWS IoT

Thank you! Kudos to Brett Frantzis, Olewale Oladehin,

and especially David Yanacek!

Page 68: SRV408 Deep Dive on AWS IoT

Appendix: Managing Software Updates

Page 69: SRV408 Deep Dive on AWS IoT

Data storage & analytics

Managing software updates

Control automation

AWS IoT

Administration Actuators

Sensors

Page 70: SRV408 Deep Dive on AWS IoT

Firmware topic (don’t do this)

• Have all devices subscribe to a topic • Publish updated binaries to this topic

SUBSCRIBE sensor/firmware

SUBSCRIBE sensor/firmware

SUBSCRIBE sensor/firmware

PUBLISH sensor/firmware 01100100 01101111 00100000 01101110 01101111 01110100 00100000 01100100 01101111 00100000 01110100 01101000 01101001 01110011

Page 71: SRV408 Deep Dive on AWS IoT

Firmware topic (don’t do this)

Pros: • Sending an update is easy

Cons: • Large messages not supported • Offline devices miss updates • No control over rollout

Page 72: SRV408 Deep Dive on AWS IoT

Firmware version shadow (don’t do this)

• One thing shadow for the current firmware version • All devices subscribe to shadow updates • Messages include a CloudFront download URL

SUBSCRIBE $aws/shadow/firmware-thing

PUBLISH $aws/shadow/firmware-thing { "desired": { "version": “123.45" "url": “https://abc123.cloudfront.net/newversion" } }

SUBSCRIBE $aws/shadow/firmware-thing

Page 73: SRV408 Deep Dive on AWS IoT

Firmware version shadow (don’t do this)

Pros: • Sending an update is easy • Offline devices eventually see updates • Bulk download happens through CloudFront

Cons: • No control over rollout • Shadow protocol is chatty

Page 74: SRV408 Deep Dive on AWS IoT

Firmware in device shadows

• Set each device’s shadow to its desired firmware version • Devices subscribe to their own shadow • Messages include a CloudFront download URL

Page 75: SRV408 Deep Dive on AWS IoT

Firmware in device shadows

SUBSCRIBE $aws/shadow/sensor-abc123

PUBLISH $aws/shadow/sensor-abc123 { "desired": { "version": “123.45" "url": "https://abc123.cloudfront.net/newversion" } }

SUBSCRIBE $aws/shadow/sensor-def456

PUBLISH $aws/shadow/sensor-def456 { "desired": { "version": “123.45" "url": "https://abc123.cloudfront.net/newversion" } }

Page 76: SRV408 Deep Dive on AWS IoT

Firmware in device shadows

Pros: • Full control over rollout / rollback • Offline devices eventually see updates • Bulk download happens through CloudFront

Cons: • Sending updates requires sending multiple messages

Page 77: SRV408 Deep Dive on AWS IoT

Takeaway

• Be careful with wide fan out to millions of devices

• Wide fan out is supported, but slow

• Encourage safe device management