AWS IoT Developer Guide
AWS IoT Developer Guide
AWS IoT Developer Guide
Developer Guide
AWS IoT Developer Guide
Table of Contents
What Is AWS IoT? ............................................................................................................................. 1
AWS IoT Components ................................................................................................................. 1
How to Get Started with AWS IoT ................................................................................................. 2
Accessing AWS IoT .................................................................................................................... 2
Related Services ........................................................................................................................ 2
How AWS IoT Works .................................................................................................................. 3
AWS IoT Button Quickstarts ................................................................................................................. 4
AWS IoT Button Wizard Quickstart ................................................................................................ 5
AWS IoT Button AWS CloudFormation Quickstart .......................................................................... 13
Next Steps ............................................................................................................................... 18
Getting Started with AWS IoT ............................................................................................................. 19
Sign in to the AWS IoT Console ................................................................................................. 20
Register a Device in the Thing Registry ....................................................................................... 20
Create and Activate a Device Certificate ...................................................................................... 22
Attach an AWS IoT Policy .......................................................................................................... 24
Attach an AWS IoT Policy to a Device Certificate .......................................................................... 26
Attach a Certificate to a Thing .................................................................................................... 29
Configure Your Device ............................................................................................................... 31
Configure an AWS IoT Button ............................................................................................ 31
Configure a Different Device ............................................................................................... 32
View Device MQTT Messages with the AWS IoT MQTT Client ......................................................... 32
Configure and Test Rules .......................................................................................................... 35
Create an SNS Topic ........................................................................................................ 35
Subscribe to an Amazon SNS Topic .................................................................................... 37
Create a Rule .................................................................................................................. 37
Test the Amazon SNS Rule ............................................................................................... 44
Next Steps ....................................................................................................................... 45
AWS IoT Rule Tutorials ..................................................................................................................... 46
Creating a DynamoDB Rule ....................................................................................................... 46
Creating a Lambda Rule ............................................................................................................ 55
Create the Lambda Function .............................................................................................. 55
Test Your Lambda Function ............................................................................................... 63
Creating a Lambda Rule .................................................................................................... 65
Test Your Lambda Rule ..................................................................................................... 68
AWS IoT SDK Tutorials ..................................................................................................................... 71
Connecting Your Raspberry Pi .................................................................................................... 71
Prerequisites .................................................................................................................... 71
Sign in to the AWS IoT Console ......................................................................................... 72
Create and Attach a Thing (Device) ..................................................................................... 73
Using the AWS IoT Embedded C SDK ........................................................................................ 80
Set Up the Runtime Environment for the AWS IoT Embedded C SDK ....................................... 80
Sample App Configuration .................................................................................................. 80
Run Sample Applications ................................................................................................... 82
Using the AWS IoT Device SDK for JavaScript ............................................................................. 82
Set Up the Runtime Environment for the AWS IoT Device SDK for JavaScript ............................. 83
Install the AWS IoT Device SDK for JavaScript ..................................................................... 85
Prepare to Run the Sample Applications .............................................................................. 85
Run the Sample Applications .............................................................................................. 85
Managing Things with AWS IoT .......................................................................................................... 87
Managing Things with the Thing Registry ..................................................................................... 87
Create a thing .................................................................................................................. 87
List things ........................................................................................................................ 88
Search for things .............................................................................................................. 88
Update a thing ................................................................................................................. 89
iii
AWS IoT Developer Guide
iv
AWS IoT Developer Guide
v
AWS IoT Developer Guide
vi
AWS IoT Developer Guide
AWS IoT Components
AWS IoT provides secure, bi-directional communication between Internet-connected things (such as
sensors, actuators, embedded devices, or smart appliances) and the AWS cloud. This enables you
to collect telemetry data from multiple devices and store and analyze the data. You can also create
applications that enable your users to control these devices from their phones or tablets.
Device gateway
Provides a secure mechanism for things and AWS IoT applications to publish and receive messages
from each other. You can use either the MQTT protocol directly or MQTT over WebSocket to publish
and subscribe. You can use the HTTP REST interface to publish.
Rules engine
Provides message processing and integration with other AWS services. You can use a SQL-based
language to select data from message payloads, process and send the data to other services, such
as Amazon S3, Amazon DynamoDB, and AWS Lambda. You can also use the message broker to
republish messages to other subscribers.
Security and Identity service
Provides shared responsibility for security in the AWS cloud. Your things must keep their credentials
safe in order to securely send data to the message broker. The message broker and rules engine use
AWS security features to send data securely to devices or other AWS services.
Thing registry
Sometimes referred to as the device registry. Organizes the resources associated with each thing.
You register your things and associate up to three custom attributes with each thing. You can also
associate certificates and MQTT client IDs with each thing to improve your ability to manage and
troubleshoot your things.
1
AWS IoT Developer Guide
How to Get Started with AWS IoT
Thing shadow
Sometimes referred to as a device shadow. A JSON document used to store and retrieve current state
information for a thing (device, app, and so on).
Thing Shadows service
Provides persistent representations of your things in the AWS cloud. You can publish updated state
information to a thing shadow, and your thing can synchronize its state when it connects. Your things
can also publish their current state to a thing shadow for use by applications or devices.
AWS Command Line Interface (AWS CLI)Run commands for AWS IoT on Windows, OS X, and
Linux. These commands allow you to create and manage things, certificates, rules, and policies. To get
started, see the AWS Command Line Interface User Guide. For more information about the commands
for AWS IoT, see iot in the AWS Command Line Interface Reference.
AWS IoT APIBuild your IoT applications using HTTP or HTTPS requests. These API allow you to
programmatically create and manage things, certificates, rules, and policies. For more information about
the API actions for AWS IoT, see Actions in the AWS IoT API Reference.
AWS SDKsBuild your IoT applications using language-specific APIs. These SDKs wrap the HTTP/
HTTPS API and allow you to program in any of the supported languages. For more information, see AWS
SDKs and Tools.
AWS IoT Device SDKsBuild applications that run on your devices that send messages to and receive
messages from AWS IoT. For more information see, AWS IoT SDKs
Related Services
AWS IoT integrates directly with the following AWS services:
Amazon Simple Storage ServiceProvides scalable storage in the AWS cloud. For more information,
see Amazon S3.
Amazon DynamoDBProvides managed NoSQL databases. For more information, see Amazon
DynamoDB.
Amazon KinesisEnables real-time processing of streaming data at a massive scale. For more
information, see Amazon Kinesis.
AWS LambdaRuns your code on virtual servers from Amazon EC2 in response to events. For more
information, see AWS Lambda.
Amazon Simple Notification ServiceSends or receives notifications. For more information, see
Amazon SNS.
Amazon Simple Queue ServiceStores data in a queue to be retrieved by applications. For more
information, see Amazon SQS.
2
AWS IoT Developer Guide
How AWS IoT Works
Things report their state by publishing messages, in JSON format, on MQTT topics. Each MQTT topic has
a hierarchical name that identifies the thing whose state is being updated. When a message is published
on an MQTT topic, the message is sent to the AWS IoT MQTT message broker, which is responsible for
sending all messages published on an MQTT topic to all clients subscribed to that topic.
Communication between a thing and AWS IoT is protected through the use of X.509 certificates. AWS IoT
can generate a certificate for you or you can use your own. In either case, the certificate must be registered
and activated with AWS IoT, and then copied onto your thing. When your thing communicates with AWS
IoT, it presents the certificate to AWS IoT as a credential.
We recommend all things that connect to AWS IoT have an entry in the thing registry. The thing registry
stores information about a thing and the certificates that are used by the thing to secure communication
with AWS IoT.
You can create rules that define one or more actions to perform based on the data in a message. For
example, you can insert, update, or query a DynamoDB table or invoke a Lambda function. Rules use
expressions to filter messages. When a rule matches a message, the rules engine invokes the action
using the selected properties. Rules also contain an IAM role that grants AWS IoT permission to the AWS
resources used to perform the action.
Each thing has a thing shadow that stores and retrieves state information. Each item in the state
information has two entries: the state last reported by the thing and the desired state requested by an
application. An application can request the current state information for a thing. The shadow responds
to the request by providing a JSON document with the state information (both reported and desired),
metadata, and a version number. An application can control a thing by requesting a change in its state.
The shadow accepts the state change request, updates its state information, and sends a message to
indicate the state information has been updated. The thing receives the message, changes its state, and
then reports its new state.
3
AWS IoT Developer Guide
The two quickstarts in this section show you how to configure and use the AWS IoT button. You can use
the AWS IoT button wizard in the AWS Lambda console to easily and quickly configure your AWS IoT
button. The AWS Lambda console contains a blueprint that will automate the process of setting up your
AWS IoT button by:
Creating and activating an X.509 certificate and private key for authenticating with AWS IoT.
Walking you through the configuration of your AWS IoT button in order to connect to your Wi-Fi network.
Walking you through the copying of your certificate and private key to your AWS IoT button.
Creating and attaching to the certificate an AWS IoT policy that gives the button permission to make calls
to AWS IoT.
Creating an AWS IoT rule that invokes a Lambda function when your AWS IoT button is pressed.
Creating an IAM role and policy that allows the Lambda function to send email messages using Amazon
SNS.
Creating a Lambda function that sends an email message to the address specified in the Lambda
function code.
You can also configure the AWS IoT button by using an AWS CloudFormation template. The second
quickstart shows you how to configure the AWS IoT resources required to process the MQTT messages
that are sent when the AWS IoT button is pressed, by using an AWS CloudFormation template.
If you do not have a button, you can purchase one here. For more information about AWS IoT, see What Is
AWS IoT (p. 1).
4
AWS IoT Developer Guide
AWS IoT Button Wizard Quickstart
Topics
AWS IoT Button Wizard Quickstart (p. 5)
AWS IoT Button AWS CloudFormation Quickstart (p. 13)
Next Steps (p. 18)
1. Open the AWS home page and choose Create an AWS Account.
2. Follow the online instructions. Part of the sign-up procedure involves receiving a phone call and
entering a PIN using your phone's keypad.
1. Sign in to the AWS Management Console and open the AWS Lambda console.
2. If this is your first time in the AWS Lambda console, you will see the following page. Choose the Get
Started Now button.
If you have used the AWS Lambda console before, you will see the following page. Choose the Create
a Lambda function button.
5
AWS IoT Developer Guide
AWS IoT Button Wizard Quickstart
3. On the Select blueprint page, from the Runtime drop-down menu, choose Node.js 4.3. In the filter
text box, type button. To choose the iot-button-email blueprint, double-click it or choose the Next
button.
6
AWS IoT Developer Guide
AWS IoT Button Wizard Quickstart
4. On the Configure triggers page, from the IoT Type drop-down menu, choose IoT Button.
Type the serial number for your device. You'll find the device serial number (DSN) on the back of the
button.
Use the links on the page to download the device certificate and the private key.
7
AWS IoT Developer Guide
AWS IoT Button Wizard Quickstart
The page also includes instructions for configuring your AWS IoT button. On step 3, you will choose
a link to open a web page that allows you to connect the AWS IoT button to your network. Under Wi-
Fi Configuration, type the network ID (SSID) and network password for your Wi-Fi network. Under
AWS IoT Configuration, choose the certificate and private key you downloaded earlier. This will copy
your certificate and private key to your AWS IoT button. Select the check box to agree to the AWS IoT
button terms and conditions, and then choose the Configure button.
8
AWS IoT Developer Guide
AWS IoT Button Wizard Quickstart
5. Close the Configure tab and go back to the AWS Lambda console page. Choose Enable trigger, and
then choose Next.
On the Configure function page, type a name for your function. The description, runtime, and
Lambda function code will be entered for you.
9
AWS IoT Developer Guide
AWS IoT Button Wizard Quickstart
In the Lambda function code, replace the example email address with your own email address.
10
AWS IoT Developer Guide
AWS IoT Button Wizard Quickstart
In the Lambda function handler and role section, from the Role drop-down menu, choose Create
new role from template(s). Type a unique name for the role.
Review the settings for the Lambda function, and then choose Create function.
11
AWS IoT Developer Guide
AWS IoT Button Wizard Quickstart
You should see a page that confirms your Lambda function has been created:
12
AWS IoT Developer Guide
AWS IoT Button AWS CloudFormation Quickstart
6. To test your Lambda function, choose the Test button. After about a minute, you should receive an
email message with AWS Notification - Subscription Confirmation in the subject line. Choose
the link in the email message to confirm the subscription to an SNS topic created by the Lambda
function. When AWS IoT receives a message from your button, it will send a message to Amazon
SNS. The Lambda function created a subscription to the Amazon SNS topic using the email address
you added in the code. When Amazon SNS receives a message on this Amazon SNS topic, it will
forward the message to your subscribed email address.
Press your button to send a message to AWS IoT. The message will cause your Lambda rule to be
triggered, and then your Lambda function will be invoked. The Lambda function will check to see if your
SNS topic exists. The Lambda function will then send the contents of the message to the Amazon SNS
topic. Amazon SNS will then forward the message to the email address you specified in the Lambda
function code.
You will need an AWS account and an AWS IoT button to complete the steps in this quickstart.
13
AWS IoT Developer Guide
AWS IoT Button AWS CloudFormation Quickstart
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Creates required AWS resources to allow an AWS IoT button to send
information through an Amazon Simple Notification Service (Amazon SNS) topic to an
email address.",
"Parameters": {
"IoTButtonDSN": {
"Type": "String",
"AllowedPattern": "G030JF05[0-9][0-5][0-9][1-7][0-9A-HJ-NP-X][0-9A-HJ-NP-X]
[0-9A-HJ-NP-X][0-9A-HJ-NP-X]",
"Description": "The device serial number (DSN) of the AWS IoT Button. This
can be found on the back of the button. The DSN must match the pattern of
'G030JF05[0-9][0-5][0-9][1-7][0-9A-HJ-NP-X][0-9A-HJ-NP-X][0-9A-HJ-NP-X][0-9A-HJ-
NP-X]'."
},
"CertificateARN": {
"Type": "String",
"Description": "The Amazon Resource Name (ARN) of the existing AWS IoT
certificate."
},
"SNSTopicName": {
"Type": "String",
"Default": "aws-iot-button-sns-topic",
"Description": "The name of the Amazon SNS topic for AWS CloudFormation to
create."
},
"SNSTopicRoleName": {
"Type": "String",
"Default": "aws-iot-button-sns-topic-role",
"Description": "The name of the IAM role for AWS CloudFormation to create. This
IAM role allows AWS IoT to send notifications to the Amazon SNS topic."
},
"EmailAddress": {
"Type": "String",
"Description": "The email address for the Amazon SNS topic to send information
to."
}
},
"Resources": {
"IoTThing": {
"Type": "AWS::IoT::Thing",
14
AWS IoT Developer Guide
AWS IoT Button AWS CloudFormation Quickstart
"Properties": {
"ThingName": {
"Fn::Join" : [ "",
[
"iotbutton_",
{ "Ref": "IoTButtonDSN" }
]
]
}
}
},
"IoTPolicy": {
"Type" : "AWS::IoT::Policy",
"Properties": {
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": "iot:Publish",
"Effect": "Allow",
"Resource": {
"Fn::Join": [ "",
[
"arn:aws:iot:",
{ "Ref": "AWS::Region" },
":",
{ "Ref": "AWS::AccountId" },
":topic/iotbutton/",
{ "Ref": "IoTButtonDSN" }
]
]
}
}
]
}
}
},
"IoTPolicyPrincipalAttachment": {
"Type": "AWS::IoT::PolicyPrincipalAttachment",
"Properties": {
"PolicyName": {
"Ref": "IoTPolicy"
},
"Principal": {
"Ref": "CertificateARN"
}
}
},
"IoTThingPrincipalAttachment": {
"Type" : "AWS::IoT::ThingPrincipalAttachment",
"Properties": {
"Principal": {
"Ref": "CertificateARN"
},
"ThingName": {
"Ref": "IoTThing"
}
}
},
"SNSTopic": {
"Type": "AWS::SNS::Topic",
"Properties": {
"DisplayName": "AWS IoT Button Press Notification",
"Subscription": [
{
"Endpoint": {
15
AWS IoT Developer Guide
AWS IoT Button AWS CloudFormation Quickstart
"Ref": "EmailAddress"
},
"Protocol": "email"
}
],
"TopicName": {
"Ref": "SNSTopicName"
}
}
},
"SNSTopicRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "iot.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
},
"Path": "/",
"Policies": [
{
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sns:Publish",
"Resource": {
"Fn::Join": [ "",
[
"arn:aws:sns:",
{ "Ref": "AWS::Region" },
":",
{ "Ref": "AWS::AccountId" },
":",
{ "Ref": "SNSTopicName" }
]
]
}
}
]
},
"PolicyName": {
"Ref": "SNSTopicRoleName"
}
}
]
}
},
"IoTTopicRule": {
"Type": "AWS::IoT::TopicRule",
"Properties": {
"RuleName": {
"Fn::Join": [ "",
[
"iotbutton_",
{ "Ref": "IoTButtonDSN" }
]
]
16
AWS IoT Developer Guide
AWS IoT Button AWS CloudFormation Quickstart
},
"TopicRulePayload": {
"Actions": [
{
"Sns": {
"RoleArn": {
"Fn::GetAtt": [ "SNSTopicRole", "Arn" ]
},
"TargetArn": {
"Ref": "SNSTopic"
}
}
}
],
"AwsIotSqlVersion": "2015-10-08",
"RuleDisabled": false,
"Sql": {
"Fn::Join": [ "",
[
"SELECT * FROM 'iotbutton/",
{ "Ref": "IoTButtonDSN" },
"'"
]
]
}
}
}
}
}
}
17
AWS IoT Developer Guide
Next Steps
4. After you have set it up, press the button once. A white light should blink several times and then be
followed by a steady green light for a few moments. Shortly afterward, you should receive an email
message with AWS IoT Button Press Notification in the subject line. You will see information sent by
the button in the body of the email message.
5. After you are finished experimenting, you can clean up the AWS resources created by the AWS
CloudFormation template. To do this, return to the AWS CloudFormation console and delete
MyAWSIoTButtonStack. After you delete MyAWSIoTButtonStack, delete the AWS IoT certificate as
follows:
Next Steps
To learn more about the Lambda blueprint used to set up your button, see Getting Started with AWS IoT.
To learn how to use AWS CloudFormation with the AWS IoT button, see http://docs.aws.amazon.com/iot/
latest/developerguide/iot-button-cloud-formation.html
18
AWS IoT Developer Guide
This tutorial shows you how to create resources required to send, receive, and process MQTT messages
from devices using AWS IoT.
For more information about AWS IoT, see What Is AWS IoT (p. 1).
Topics
Sign in to the AWS IoT Console (p. 20)
19
AWS IoT Developer Guide
Sign in to the AWS IoT Console
1. Open the AWS home page and choose Create an AWS Account.
2. Follow the online instructions. Part of the sign-up procedure involves receiving a phone call and
entering a PIN using your phone's keypad.
3. Sign in to the AWS Management Console and open the AWS IoT console.
4. On the Welcome page, choose Get started.
If this is your first time using the AWS IoT console, you will see the Welcome to the AWS IoT
Console page.
1. On the Welcome to the AWS IoT Console page, in the left navigation pane, choose Registry to
expand the choices, and then choose Things.
20
AWS IoT Developer Guide
Register a Device in the Thing Registry
2. On the page that says You don't have any things yet, choose Register a thing.
3. On the Register a thing page, in the Name field, type a name for your device. Choose Create thing to
add your device to the thing registry.
21
AWS IoT Developer Guide
Create and Activate a Device Certificate
22
AWS IoT Developer Guide
Create and Activate a Device Certificate
3. On the Certificate created page, choose Download for the certificate, private key, and the root CA for
AWS IoT, save each of them to your computer, and then choose Activate to continue.
Note
Although it is unlikely root CA certificates are subject to expiration and/or revocation. If this
should occur, you will need to copy new a root CA certificate onto your device.
23
AWS IoT Developer Guide
Attach an AWS IoT Policy
24
AWS IoT Developer Guide
Attach an AWS IoT Policy
3. On the Create a policy page, in the Name field, type a name for the policy (for example "myPolicy"). In
the Action field, type iot:Connect. In the Resource ARN field, type *. Select the Allow checkbox. This
allows all clients to connect to AWS IoT.
Note
You can restrict which clients (devices) are able to connect by specifying a client ARN as the
resource. The client ARNs follow this format:
25
AWS IoT Developer Guide
Attach an AWS IoT Policy to a Device Certificate
arn:aws:iot:your-region:your-aws-account:client/<my-client-id>
Select the Add Statement button to add another policy statement. In the Action field, type
iot:Publish. In the Resource ARN field, type the ARN of the topic to which your device will publish.
Note
The topic ARN follows this format:
arn:aws:iot:your-region:your-aws-account:topic/iotbutton/your-button-serial-
number
For example:
arn:aws:iot:us-east-1:123456789012:topic/iotbutton/G030JF055364XVRB
You can find the serial number on the bottom of your button.
If you are not using an AWS IoT button, place the topic your device publishes to after topic/
in the ARN. For example:
arn:aws:iot:us-east-1:123456789012:topic/my/topic/here
Finally, select the Allow check box. This allows your device to publish messages to the specified topic.
4. After you have entered the information for your policy, choose Create.
26
AWS IoT Developer Guide
Attach an AWS IoT Policy to a Device Certificate
1. On the Overview page for the policy, in the left navigation area, choose the left arrow to go to the AWS
IoT Policies page.
2. On the Policies page, in the left navigation pane, under Security, choose Certificates.
27
AWS IoT Developer Guide
Attach an AWS IoT Policy to a Device Certificate
3. In the box for the certificate you created, choose ... to open a drop-down menu, and then choose
Attach policy.
4. In the Attach policies to certificate(s) dialog box, select the check box next to the policy you created
in the previous step, and then choose Attach.
28
AWS IoT Developer Guide
Attach a Certificate to a Thing
To attach a certificate to the thing representing your device in the thing registry:
1. In the box for the certificate you created, choose ... to open a drop-down menu, and then choose
Attach thing.
29
AWS IoT Developer Guide
Attach a Certificate to a Thing
2. In the Attach things to certificate(s) dialog box, select the check box next to the thing you registered,
and then choose Attach.
3. To verify the thing is attached, select the box representing the certificate. On the Details page for the
certificate, in the left navigation pane, choose Things.
4. To verify the policy is attached, on the Details page for the certificate, in the left navigation pane,
choose Policies.
30
AWS IoT Developer Guide
Configure Your Device
31
AWS IoT Developer Guide
Configure a Different Device
Copy your device certificate and private key onto your AWS IoT
button
To connect to AWS IoT, you must copy your device certificate onto the AWS IoT button.
ABCDEFG1234567.iot.us-east-2.amazonaws.com
Choose Configure. Your button should now connect to your Wi-Fi network.
32
AWS IoT Developer Guide
View Device MQTT Messages
with the AWS IoT MQTT Client
Devices publish MQTT messages on topics. You can use the AWS IoT MQTT client to subscribe to these
topics to see these messages.
1. In the AWS IoT console, in the left navigation pane, choose Test.
2. Subscribe to the topic on which your thing publishes. In the case of the AWS IoT button, you can
subscribe to iotbutton/+. In Subscribe to a topic, in the Subscription topic field, type iotbutton/+,
and then choose Subscribe to topic. This topic should appear under Subscriptions. Choose it there.
3. Press your AWS IoT button, and then view the message in the AWS IoT MQTT client. If you do not
have a button, you will simulate a button press in the next step.
33
AWS IoT Developer Guide
View Device MQTT Messages
with the AWS IoT MQTT Client
On the MQTT client page, in the Publish section, in Specify a topic, type iobutton/ABCDEFG12345. In
the message payload section, type the following JSON:
{
"serialNumber": "ABCDEFG12345",
"clickType": "SINGLE",
"batteryVoltage": "2000 mV"
}
Choose Publish to topic. You should see the message in the AWS IoT MQTT client.
34
AWS IoT Developer Guide
Configure and Test Rules
In the upper-right corner of this page, there is a Filter View drop-down list. For instructions for testing your
rule by using the AWS IoT button, choose AWS IoT Button. For instructions for testing your rule by using
the AWS IoT MQTT client, choose MQTT Client.
35
AWS IoT Developer Guide
Create an SNS Topic
4. Type a topic name and a display name, and then choose Create topic.
5. Make a note of the ARN for the topic you just created.
36
AWS IoT Developer Guide
Subscribe to an Amazon SNS Topic
1. In the Amazon SNS console, select the check box next to the topic you just created. From the Actions
menu, choose Subscribe to topic.
In the Endpoint field, type the phone number of an SMS-enabled cell phone, and then choose Create
subscription.
Note
Enter the phone number using numbers and dashes only.
You will receive a text message that confirms you successfully created the subscription.
Create a Rule
AWS IoT rules consist of a topic filter, a rule action, and, in most cases, an IAM role. Messages published
on topics that match the topic filter trigger the rule. The rule action defines which action to take when the
rule is triggered. The IAM role contains one or more IAM policies that determine which AWS services the
rule can access. You can create multiple rules that listen on a single topic. Likewise, you can create a
single rule that is triggered by multiple topics. The AWS IoT rules engine continuously processes messages
published on topics that match the topic filters defined in the rules.
37
AWS IoT Developer Guide
Create a Rule
In this example, you will create a rule that uses Amazon SNS to send an SMS notification to a cell phone
number.
1. In the AWS IoT console, in the left navigation pane, choose Rules.
3. On the Create a rule page, in the Name field, type a name for your rule. In the Description field, type
a description for the rule.
38
AWS IoT Developer Guide
Create a Rule
4. Scroll down to Message source. Choose the latest version from the Using SQL version drop-down
list. In the Attribute field, type *. This specifies that you want to send the entire MQTT message that
triggered the rule.
5. The rules engine uses the topic filter to determine which rules to trigger when an MQTT message is
received. In the Topic filter field, type iotbutton/your-button-DSN. If you are not using an AWS IoT
button, type my/topic or the topic used in the rule.
Note
You can find the DSN on the bottom of the button.
39
AWS IoT Developer Guide
Create a Rule
7. On the Select an action page, select Send a message as an SNS push notification, and then
choose Configure action.
40
AWS IoT Developer Guide
Create a Rule
8. On the Configure action page, from the SNS target drop-down list, choose the Amazon SNS topic
you created earlier.
41
AWS IoT Developer Guide
Create a Rule
9. Now you need to give AWS IoT permission to publish to the Amazon SNS topic on your behalf when
the rule is triggered. Choose Create a new role. Enter a name for your new role in the IAM role name
field. After you have entered the name, choose Create a new role again. Select the newly created role
from the IAM role name drop-down list.
10. Choose Update role to apply the permissions to the newly created role, and then choose Add action.
42
AWS IoT Developer Guide
Create a Rule
12. On the Overview page for the rule, choose the left arrow to return to the AWS IoT dashboard.
43
AWS IoT Developer Guide
Test the Amazon SNS Rule
For more information about creating rules, see AWS IoT Rules .
1. In the AWS IoT console, in the left navigation pane, choose Test.
2. On the MQTT client page, in the Publish section, in Specify a topic, type my/topic or the topic you
used in the rule. In the message payload section, type the following JSON:
{
"message": "Hello, from AWS IoT console"
}
Note
If you are using a button, type iotbutton/your-button-DSN instead of my/topic in the
Specify a topic field.
44
AWS IoT Developer Guide
Next Steps
3. Choose Publish to topic. You should receive an Amazon SNS message on your cell phone.
Congratulations! You have successfully created and configured a rule that sends data received from a
device to an Amazon SNS topic.
Next Steps
For more information about AWS IoT rules, see AWS IoT Rule Tutorials (p. 46) and AWS IoT
Rules (p. 140).
45
AWS IoT Developer Guide
Creating a DynamoDB Rule
This guide includes tutorials that walk you through the creation and testing of AWS IoT rules. If you have
not completed the AWS IoT Getting Started Tutorial (p. 19), we recommend you do that first. It shows you
how to create an AWS account and connect your device to AWS IoT.
An AWS IoT rule consists of a SQL SELECT statement, a topic filter, and a rule action. Devices send
information to AWS IoT by publishing messages to MQTT topics. The SQL SELECT statement allows
you to extract data from an incoming MQTT message. The topic filter of an AWS IoT rule specifies one
or more MQTT topics. The rule is triggered when an MQTT message is received on a topic that matches
the topic filter. Rule actions allow you to take the information extracted from an MQTT message and send
it to another AWS service. Rule actions are defined for AWS services like Amazon DynamoDB, AWS
Lambda, Amazon SNS, and Amazon S3. By using a Lambda rule, you can call other AWS or third-party
web services. For a complete list of rule actions, see AWS IoT Rule Actions (p. 148).
In these tutorials we assume you are using the AWS IoT button and will use iotbutton/+ as the topic filter
in the rules. If you do not have an AWS IoT button, you can buy one here.
The AWS IoT button sends a JSON payload that looks like this:
{
"serialNumber" : "ABCDEFG12345",
"batteryVoltage" : "2000mV",
"clickType" : "SINGLE"
}
You can emulate the AWS IoT button by using an MQTT client like the AWS IoT MQTT client in the AWS
IoT console. To emulate the AWS IoT button, publish a similar message on the iotbutton/ABCDEFG12345
topic. The number after the / is arbitrary. It will be used as the serial number for the button.
You can use your own device, but you will need to know on which MQTT topic your device publishes so
you can specify it as the topic filter in the rule. For more information, see AWS IoT Rules (p. 140).
46
AWS IoT Developer Guide
Creating a DynamoDB Rule
1. In the AWS IoT console, in the left navigation pane, choose Rules.
3. On the Create a rule page, in the Name field, type a name for your rule. In the Description field, type
a description for the rule.
47
AWS IoT Developer Guide
Creating a DynamoDB Rule
4. Scroll down to Message source. Choose the latest version from the Using SQL version drop-down
list. In the Attribute field, type *. This specifies that you want to send the entire MQTT message that
triggered the rule.
5. The rules engine uses the topic filter to determine which rules to trigger when an MQTT message is
received. In the Topic filter field, type iotbutton/your-button-DSN. If you are not using an AWS IoT
button, type my/topic or the topic used in the rule.
Note
You can find the DSN on the bottom of the button.
48
AWS IoT Developer Guide
Creating a DynamoDB Rule
7. On the Select an action page, select Insert a message into a DynamoDB table, and then choose
Configure action.
49
AWS IoT Developer Guide
Creating a DynamoDB Rule
50
AWS IoT Developer Guide
Creating a DynamoDB Rule
10. On the Create DynamoDB table page, type a name in the Table name field. In Partition key, type
SerialNumber. Select the Add sort key check box, then type ClickType in the Sort key field. Select
String for both the partition and sort keys.
51
AWS IoT Developer Guide
Creating a DynamoDB Rule
11. Choose Create. It will take a few seconds to create your DynamoDB table. Close the browser tab
where the Amazon DynamoDB console is open. If you do not close the tab, your DynamoDB table will
not be displayed in the Table name drop-down list on the AWS IoT Configure action page.
12. On the Configure action page, choose your new table from the Table name drop-down list. In Hash
key value, type ${serialNumber}. This instructs the rule to take the value of the serialNumber
attribute from the MQTT message and write it into the SerialNumber column in the DynamoDB table.
In Range key value, type ${clickType}. This writes the value of the clickType attribute into the
ClickType column. Leave Write message data to this column blank. By default, the entire message
will be written to a column in the table called Payload. Choose Create a new role.
52
AWS IoT Developer Guide
Creating a DynamoDB Rule
13. Type a unique name in IAM role name, and then choose the Create a new role button again. Choose
the role you just created and choose Update role. Then choose Add action.
53
AWS IoT Developer Guide
Creating a DynamoDB Rule
15. A confirmation message shows the rule has been created. Choose the left arrow to return to the Rules
page.
16. Test the rule by pressing your configured AWS IoT button or using an MQTT client to publish a
message on a topic that matches your rule's topic filter. Finally, return to the DynamoDB console and
select the table you created to view the entry for your button press or message.
54
AWS IoT Developer Guide
Creating a Lambda Rule
In this tutorial, we assume you have completed the AWS IoT Getting Started Tutorial (p. 19) in which you
create and subscribe to an Amazon SNS topic using your cell phone number. You will create a Lambda
function that publishes a message to the Amazon SNS topic you created in the AWS IoT Getting Started
Tutorial (p. 19). You will also create a Lambda rule that calls the Lambda function, passing in some data
from the MQTT message that triggered the rule.
In this tutorial, we also assume you are using an AWS IoT button to trigger the Lambda rule. If you do not
have an AWS IoT button, you can buy one here or you can use an MQTT client to send an MQTT message
that will trigger the rule.
1. In the AWS Lambda console, choose Get Started Now or, if you have created a Lambda function
before, choose Create a Lambda function.
55
AWS IoT Developer Guide
Create the Lambda Function
2. On the Select blueprint page, in the Filter field, type hello-world, and then choose the hello-world
blueprint.
3. On the Configure triggers page, select the box to the left of the Lambda icon, and select AWS IoT
from the drop-down menu.
56
AWS IoT Developer Guide
Create the Lambda Function
4. In the Device Serial Number field, enter your button's device serial number (DSN). Your DSN is
printed on the back of your AWS IoT button. If you have not already generated a certificate and private
key for your AWS IoT button, choose Generate certificate and keys. Otherwise, skip to step 6.
57
AWS IoT Developer Guide
Create the Lambda Function
5. Choose the links to download your certificate PEM and private key. Save these files in a secure
location on your computer.
Follow the instructions on the screen to configure your AWS IoT button.
6. Make sure that the Enable trigger check box is selected and choose Next.
58
AWS IoT Developer Guide
Create the Lambda Function
7. On the Configure function page, type a name and description for the Lambda function. In Runtime,
choose Node.js 4.3.
8. Scroll down to the Lambda function code section of the page. Replace the existing code with the
following:
console.log('Loading function');
// Load the AWS SDK
var AWS = require("aws-sdk");
// Log a message to the console, you can view this text in the Monitoring tab
in the Lambda console or in the CloudWatch Logs console
console.log("Received event:", eventText);
// Create a string extracting the click type and serial number from the message
sent by the AWS IoT button
var messageText = "Received " + event.clickType + " message from button ID: "
+ event.serialNumber;
59
AWS IoT Developer Guide
Create the Lambda Function
Note
Replace the value of the TopicArn with the ARN of the Amazon SNS topic you created
previously.
9. Scroll down to the Lambda function handler and role section of the page. For Role, choose Create a
custom role. The IAM console will open, allowing you to create an IAM role that Lambda can assume
when executing the Lambda function.
To edit the role's policy to give it permission to publish to your Amazon SNS topic:
60
AWS IoT Developer Guide
Create the Lambda Function
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": [
"sns:Publish"
],
"Resource": "arn:aws:sns:us-east-1:123456789012:MyIoTButtonSNSTopic"
}
]
}
This policy document adds permission to publish to your Amazon SNS topic.
Note
Replace the value of the second Resource with the ARN of the Amazon SNS topic you
created previously.
10. Choose Allow.
11. Leave the settings on the Advanced settings page at their defaults, and choose Next.
61
AWS IoT Developer Guide
Create the Lambda Function
62
AWS IoT Developer Guide
Test Your Lambda Function
2. Copy and paste the following JSON into the Input test event page, and then choose Save and test.
{
"serialNumber": "ABCDEFG12345",
"clickType": "SINGLE",
"batteryVoltage": "2000 mV"
}
63
AWS IoT Developer Guide
Test Your Lambda Function
3. In the AWS Lambda console, scroll to the bottom of the page. The Log output section displays the
output the Lambda function has written to the console.
64
AWS IoT Developer Guide
Creating a Lambda Rule
1. In the AWS IoT console, in the left navigation pane, choose Rules.
65
AWS IoT Developer Guide
Creating a Lambda Rule
66
AWS IoT Developer Guide
Creating a Lambda Rule
6. On the Select an action page, select Invoke a Lambda function passing the message data and
then choose Configure action.
67
AWS IoT Developer Guide
Test Your Lambda Rule
7. From the Function name drop-down list, choose your Lambda function name, then choose Add
action.
68
AWS IoT Developer Guide
Test Your Lambda Rule
Now that your button is configured and connected to Wi-Fi and you have configured an Amazon SNS topic,
you can press the button to test your Lambda rule. You should receive an SMS text message on your
phone that contains the serial number of your button, the type of button press (SINGLE or DOUBLE), and
the battery voltage.
IOT BUTTON> {
"serialNumber" : "ABCDEFG12345",
"clickType" : "SINGLE",
"batteryVoltage" : "2000 mV"
}
If you do not have a button, you can buy one here or you can use the AWS IoT MQTT client instead.
2. On the MQTT client page, in the Publish section, in Specify a topic, type iotbutton/ABCDEFG12345.
In Payload, type the following JSON, and then choose Publish to topic.
{
"serialNumber" : "ABCDEFG12345",
"clickType" : "SINGLE",
"batteryVoltage" : "2000 mV"
}
69
AWS IoT Developer Guide
Test Your Lambda Rule
70
AWS IoT Developer Guide
Connecting Your Raspberry Pi
The AWS IoT Device SDKs help you to easily and quickly connect your devices to AWS IoT. The AWS IoT
Device SDKs include open-source libraries, developer guides with samples, and porting guides so that you
can build innovative IoT products or solutions on your choice of hardware platforms.
This guide provides step-by-step instructions for connecting your Raspberry Pi to the AWS IoT platform and
setting it up for use with the AWS IoT Embedded C SDK and Device SDK for Javascript. After following the
steps in this guide, you will be able to connect to the AWS IoT platform and run sample apps included with
these AWS IoT SDKs.
Contents
Connecting Your Raspberry Pi (p. 71)
Using the AWS IoT Embedded C SDK (p. 80)
Using the AWS IoT Device SDK for JavaScript (p. 82)
Prerequisites
A fully set up Raspberry Pi board with Internet access
For information about setting up your Raspberry Pi, see Raspberry Pi Quickstart Guide.
Chrome or Firefox (Iceweasel) browser
For information about installing Iceweasel, see the instructions on the Embedded Linux wiki.
Raspberry Pi 2 Model B
Raspbian Wheezy
Raspbian Jessie
Iceweasel browser
71
AWS IoT Developer Guide
Sign in to the AWS IoT Console
3. If this is your first time using the AWS IoT console, you will see the Welcome to the AWS IoT
Console page. In the left navigation pane, choose Registry to expand the choices, and then choose
Things.
4. On the page that says You don't have any things yet, choose Register a thing. (If you have created
a thing before, choose Create.)
72
AWS IoT Developer Guide
Create and Attach a Thing (Device)
1. Type a name for the thing, and then choose Create thing.
73
AWS IoT Developer Guide
Create and Attach a Thing (Device)
3. Make a note of the REST API endpoint. You will need this value later. Choose Security.
4. Choose Create certificate. This will generate an X.509 certificate and key pair.
74
AWS IoT Developer Guide
Create and Attach a Thing (Device)
5. Create a working directory called deviceSDK where your files will be stored. Choose the links to
download your public and private keys, certificate, and root CA and save them in the deviceSDK
directory. Choose Activate to activate the X.509 certificate, then choose Attach a policy.
75
AWS IoT Developer Guide
Create and Attach a Thing (Device)
7. On the Create a policy page, in the Name field, type a name for the policy. In the Action field, type
iot:*. In the Resource ARN field, type *. Select the Allow check box. This allows your Raspberry Pi
to publish messages to AWS IoT.
8. Choose Create.
76
AWS IoT Developer Guide
Create and Attach a Thing (Device)
77
AWS IoT Developer Guide
Create and Attach a Thing (Device)
11. In the box for the certificate you created, choose ... to open a drop-down menu, and then choose
Attach policy.
12. In the Attach policies to certificate(s) dialog box, select the check box next to the policy you created,
and then choose Attach.
78
AWS IoT Developer Guide
Create and Attach a Thing (Device)
13. In the box for the certificate you created, choose ... to open a drop-down menu, and then choose
Attach thing.
14. In the Attach things to certificate(s) dialog box, select the check box next to the thing you created to
represent your Raspberry Pi, and then choose Attach.
79
AWS IoT Developer Guide
Using the AWS IoT Embedded C SDK
1. Copy your certificate, private key and root CA certificate into the deviceSDK/certs directory.
If you did not get a copy of the root CA certificate, you can download it here. Copy the root CA text
from the browser, paste it into a file, and then copy it into the deviceSDK/certs directory.
Note
Device and root CA certificates are subject to expiration and/or revocation. If this should
occur, you will need to copy new a CA certificate or a new private key and device certificate
onto your device.
2. Navigate to the deviceSDK/sample_apps/subscribe_publish_sample directory. You will need to
configure your personal endpoint, private key, and certificate. The personal endpoint is the REST API
80
AWS IoT Developer Guide
Sample App Configuration
endpoint you noted earlier. If you don't remember the endpoint and you have access to a machine with
the AWS CLI installed, you can use the aws iot describe-endpoint command to find your personal
endpoint URL. Or, go to the AWS IoT console. Choose Registry, choose Things, and then choose the
thing that represents your Raspberry Pi. On the Details page for the thing, in the left navigation pane,
choose Interact. Copy everything, including ".com", from REST API endpoint.
3. Open the aws_iot_config.h file and, in the //Get from console section, update the values for the
following:
AWS_IOT_MQTT_HOST
Your certificate.
AWS_IOT_PRIVATE_KEY_FILENAME
For example:
81
AWS IoT Developer Guide
Run Sample Applications
make -f Makefile
2. Now run the subscribe_publish_sample_app. You should see output similar to the following:
Your Raspberry Pi is now connected to AWS IoT using the AWS IoT Device SDK for C.
82
AWS IoT Developer Guide
Set Up the Runtime Environment for the
AWS IoT Device SDK for JavaScript
1. To add the Node repository, open a terminal and run the following command:
83
AWS IoT Developer Guide
Set Up the Runtime Environment for the
AWS IoT Device SDK for JavaScript
node -v
and
npm -v
84
AWS IoT Developer Guide
Install the AWS IoT Device SDK for JavaScript
After the installation is complete, you should find a node_modules directory in your ~/deviceSDK directory.
Edit the file ~/deviceSDK/aws-iot-device-sdk/examples/lib/cmdline.js to change the default names for the
private key (privateKey), certificate (clientCert), and CA root certificate (caCert) used by the samples.
For example:
default: {
region: 'us-east-1',
clientId: clientIdDefault,
privateKey: '4bbdc778b9-private.pem.key',
clientCert: '4bbdc778b9-certificate.pem.crt',
caCert: 'root-CA.crt,
testMode: 1,
reconnectPeriod: 3 * 1000, /* milliseconds */
delay: 4 * 1000 /* milliseconds */
};
85
AWS IoT Developer Guide
Run the Sample Applications
If you want to create a configuration file for use with the command line option --configuration-file (-F),
create a file (in JSON format) with the following properties. For example:
{
"host": "a22j5sm6o3yzc5.iot.us-east-1.amazonaws.com"
"port": 8883
"clientId": "MyRaspberryPi"
"thingName": "MyRaspberryPi"
"caCert": "root-CA.crt"
"clientCert": "4bbdc778b9-certificate.pem.crt"
"privateKey": "4bbdc778b9-private.pem.key"
}
Your Raspberry Pi is now connected to AWS IoT using the AWS IoT SDK for JavaScript.
86
AWS IoT Developer Guide
Managing Things with the Thing Registry
AWS IoT provides a hing registry that helps you manage your things. A thing is a representation of a
specific device or logical entity. It can be a physical device or sensor (for example, a light bulb or a switch
on a wall). It can also be a logical entity like an instance of an application or physical entity that does not
connect to AWS IoT but is related to other devices that do (for example, a car that has engine sensors or a
control panel).
Information about a thing is stored in the thing registry as JSON data. Here is an example thing:
{
"version": 3,
"thingName": "MyLightBulb",
"defaultClientId": "MyLightBulb",
"thingTypeName": "LightBulb",
"attributes": {
"model": "123",
"wattage": "75"
}
}
Things are identified by a name. Things can also have attributes, which are name-value pairs you can use
to store information about the thing, such as its serial number or manufacturer.
A typical device use case involves the use of the thing name as the default MQTT client ID. Although we
do not enforce a mapping between a things registry name and its use of MQTT client IDs, certificates,
or shadow state, we recommend you choose a thing name and use it as the MQTT client ID for both the
thing registry and the Thing Shadows service. This provides organization and convenience to your IoT fleet
without removing the flexibility of the underlying device certificate model or thing shadows.
You do not need to create a thing in the thing registry to connect it to AWS IoT. Adding your things in the
thing registry allows you to manage and search for them more easily.
Create a thing
The following command shows how to use the AWS IoT create-thing CLI command to create a thing:
87
AWS IoT Developer Guide
List things
The create-thing API will display the name and ARN of your new thing:
{
"thingArn": "arn:aws:iot:us-east-1:803981987763:thing/MyLightBulb",
"thingName": "MyLightBulb"
}
List things
You can use the list-things API to list all things in your account:
You can use the list-things API to search for all things associated with a thing type name:
88
AWS IoT Developer Guide
Update a thing
"things": [
{
"thingTypeName": "LightBulb",
"attributes": {
"model": "123",
"wattage": "75"
},
"version": 1,
"thingName": "MyRGBLight"
},
{
"thingTypeName": "LightBulb",
"attributes": {
"model": "123",
"wattage": "75"
},
"version": 1,
"thingName": "MySecondLightBulb"
}
]
}
You can use the list-things API to search for all things that have an attribute with a specific value:
{
"things": [
{
"thingTypeName": "StopLight",
"attributes": {
"model": "123",
"wattage": "75"
},
"version": 3,
"thingName": "MyLightBulb"
},
{
"thingTypeName": "LightBulb",
"attributes": {
"model": "123",
"wattage": "75"
},
"version": 1,
"thingName": "MyRGBLight"
},
{
"thingTypeName": "LightBulb",
"attributes": {
"model": "123",
"wattage": "75"
},
"version": 1,
"thingName": "MySecondLightBulb"
}
]
}
Update a thing
You can use the update-thing API to update a thing:
89
AWS IoT Developer Guide
Delete a thing
The update-thing command does not produce output. You can use the describe-thing API to see the
result:
Delete a thing
You can use the delete-thing API to delete a thing:
Thing Types
Thing types allow you to store description and configuration information that is common to all things
associated with the same thing type. This simplifies the management of things in the thing registry. For
example, you can define a LightBulb thing type. All things associated with the LightBulb thing type share a
set of attributes: serial number, manufacturer, and wattage. When you create a thing of type LightBulb (or
change the type of an existing thing to LightBulb) you can specify values for each of the attributes defined
in the LightBulb thing type.
Although thing types are optional, their use provides better discovery of things.
90
AWS IoT Developer Guide
Create a Thing Type
Thing types are immutable. You cannot change a thing type name after it has been created. You can
deprecate a thing type at any time to prevent new things from being associated with it. You can also delete
thing types that have no things associated with them.
The create-thing-type command returns a response that contains the thing type and its ARN:
{
"thingTypeName": "LightBulb",
"thingTypeArn": "arn:aws:iot:us-west-2:803981987763:thingtype/LightBulb"
}
The list-thing-types command returns a list of the thing types defined in your AWS account:
{
"thingTypes": [
{
"thingTypeName": "LightBulb",
"thingTypeProperties": {
"deprecated": false,
"creationDate": 1468423800950,
"searchableAttributes": [
"wattage",
"model"
],
"thingTypeDescription": "light bulb type"
}
}
]
}
91
AWS IoT Developer Guide
Associate a thing type with a thing
The describe-thing-type API responds with information about the specified type:
{
"thingTypeName": "LightBulb",
"thingTypeProperties": {
"deprecated": false,
"creationDate": 1468423800950,
"searchableAttributes": [
"wattage",
"model"
],
"thingTypeDescription": "light bulb type"
}
}
You can use the update-thing API at any time to change the thing type associated with a thing:
You can also use the update-thing API to disassociate a thing from a thing type.
{
"thingTypeName": "StopLight",
"thingTypeProperties": {
"deprecated": true,
"creationDate": 1468425854308,
"searchableAttributes": [
"wattage",
"numOfLights",
"model"
],
"thingTypeDescription": "traffic light type",
"deprecationDate": 1468446026349
}
92
AWS IoT Developer Guide
Delete a thing type
Deprecating a thing type is a reversible operation. You can undo a deprecation by using the --undo-
deprecate flag with the deprecate-thing-type CLI command:
You can use the deprecate-thing-type CLI command to see the result:
{
"thingTypeName": "StopLight",
"thingTypeProperties": {
"deprecated": false,
"creationDate": 1468425854308,
"searchableAttributes": [
"wattage",
"numOfLights",
"model"
],
"thingTypeDescription": "traffic light type"
}
}
Note
You must wait five minutes after you deprecate a thing type before you can delete it.
93
AWS IoT Developer Guide
Authentication in AWS IoT
Each connected device must have a credential to access the message broker or the Thing Shadows
service. All traffic to and from AWS IoT must be encrypted over Transport Layer Security (TLS). Device
credentials must be kept safe in order to send data securely to the message broker. AWS cloud security
mechanisms protect data as it moves between AWS IoT and other devices or AWS services.
You are responsible for managing device credentials (X.509 certificates, AWS credentials) on your
devices and policies in AWS IoT. You are responsible for assigning unique identities to each device and
managing the permissions for a device or group of devices.
Devices connect using your choice of identity (X.509 certificates, IAM users and groups, or Amazon
Cognito identities) over a secure connection according to the AWS IoT connection model.
The AWS IoT message broker authenticates and authorizes all actions in your account. The message
broker is responsible for authenticating your devices, securely ingesting device data, and adhering to the
access permissions you place on devices using policies.
The AWS IoT rules engine forwards device data to other devices and other AWS services according
to rules you define. It uses AWS access management systems to securely transfer data to its final
destination.
94
AWS IoT Developer Guide
X.509 Certificates
X.509 certificates
IAM users, groups, and roles
Amazon Cognito identities
Federated identities
These identities can be used with mobile applications, web applications, or desktop applications. They
can even be used by a user typing AWS IoT CLI commands. Typically, AWS IoT devices use X.509
certificates, while mobile applications use Amazon Cognito identities. Web and desktop applications use
IAM or federated identities. CLI commands use IAM.
X.509 Certificates
X.509 certificates are digital certificates that use the X.509 public key infrastructure standard to associate a
public key with an identity contained in a certificate. X.509 certificates are issued by a trusted entity called
a certification authority (CA). The CA maintains one or more special certificates called CA certificates that it
uses to issue X.509 certificates. Only the certification authority has access to CA certificates.
Note
We recommend that each device be given a unique certificate to enable fine-grained management
including certificate revocation.
Note
Devices must support rotation and replacement of certificates in order to ensure smooth operation
as certificates expire.
SHA256WITHRSA
SHA384WITHRSA
SHA384WITHRSA
SHA512WITHRSA
RSASSAPSS
DSA_WITH_SHA256
ECDSA-WITH-SHA256
ECDSA-WITH-SHA384
ECDSA-WITH-SHA512
Certificates provide several benefits over other identification and authentication mechanisms. Certificates
enable asymmetric keys to be used with devices. This means you can burn private keys into secure storage
on a device. This way, sensitive cryptographic material never leaves the device. Certificates provide
stronger client authentication over other schemes, such as user name and password or bearer tokens,
because the secret key never leaves the device.
AWS IoT authenticates certificates using the TLS protocols client authentication mode. TLS is available in
many programming languages and operating systems and is commonly used for encrypting data. In TLS
client authentication, AWS IoT requests a client X.509 certificate and validates the certificates status and
AWS account against a registry of certificates. It then challenges the client for proof of ownership of the
private key that corresponds to the public key contained in the certificate.
To use AWS IoT certificates, clients must support all of the following in their TLS implementation:
TLS 1.2.
SHA-256 RSA certificate signature validation.
95
AWS IoT Developer Guide
X.509 Certificates
One of the cipher suites from the TLS cipher suite support section.
To use a certificate that is not created by AWS IoT, you must register a CA certificate. All device certificates
must be signed by the CA certificate you register.
You can use the AWS IoT console or CLI to perform the following operations:
For more information about the CLI commands to use to perform these operations, see AWS IoT CLI
Reference.
For more information about using the AWS IoT console to create certificates, see Create and Activate a
Device Certificate.
Server Authentication
Device certificates allow AWS IoT to authenticate devices. To make sure your device is communicating with
AWS IoT and not another server impersonating AWS IoT, copy the VeriSign Class 3 Public Primary G5 root
CA certificate onto your device.
Note
This CA certificate is valid until July 2036, but the CA certificate may need to be replaced before
then. You should ensure that you can update the root CA certificate on all of your devices to
ensure ongoing connectivity and to keep up to date with security best practices.
Reference the CA root certificate in your device code when you connect to AWS IoT. For more information,
see the AWS IoT Device SDKs (p. 235).
Note
You cannot use your own CA certificate to authenticate the AWS IoT server. You must use the
VeriSign Class 3 Public Primary G5 root CA certificate.
96
AWS IoT Developer Guide
X.509 Certificates
1. Sign in to the AWS Management Console and open the AWS IoT console at https://
console.aws.amazon.com/iot.
2. In the left navigation pane, choose Security to expand the choices, and then choose Certicates.
Choose Create.
3. Choose One-click certificate creation - Create certificate. Alternatively, to generate a certificate with
a certificate signing request (CSR), choose Create with CSR.
4. Use the links to the public key, private key, and certificate to download each to a secure location.
5. Choose Activate.
create-keys-and-certificate
The CreateKeysAndCertificate API creates a private key, public key, and X.509 certificate.
create-certificate-from-csr
Contents
Registering Your CA Certificate (p. 98)
Creating a Device Certificate Using Your CA Certificate (p. 99)
Registering a Device Certificate (p. 99)
Registering Device Certificates Manually (p. 100)
Using Automatic/Just-in-Time Registration for Device Certificates (p. 100)
Deactivate the CA Certificate (p. 101)
Revoke the Device Certificate (p. 101)
If you do not have a CA certificate, you can use OpenSSL tools to create one.
To create a CA certificate
97
AWS IoT Developer Guide
X.509 Certificates
2. Use the private key from the key pair to generate a CA certificate.
openssl req -x509 -new -nodes -key rootCA.key -sha256 -days 1024 -out rootCA.pem
The Common Name field in the private key verification certificate must be set to the registration code
generated by the get-registration-code CLI command. A single registration code is generated per
AWS account. You can use the register-ca-certificate command or the AWS IoT console to register
CA certificates.
To register a CA certificate
1. Get a registration code from AWS IoT. This code will be used as the Common Name of the private key
verification certificate.
3. Create a CSR for the private key verification certificate. Set the Common Name field of the certificate to
your registration code.
You will be prompted for some information, including the Common Name, for the certificate.
5. Register the CA certificate with AWS IoT. Pass in the CA certificate and the private key verification
certificate to the register-ca-certificate CLI command.
98
AWS IoT Developer Guide
X.509 Certificates
You can use a CA certificate registered with AWS IoT to create a device certificate. The device certificate
must be registered with AWS IoT before use.
openssl x509 -req -in deviceCert.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -
out deviceCert.pem -days 500 -sha256
Note
You must use the CA certificate registered with AWS IoT to create device certificates. If you
have more than one CA certificate (with the same subject field and public key) registered in
your AWS account, you must specify the CA certificate used to create the device certificate
when you register your device certificate.
4. Register a device certificate.
You must use the CA certificate registered with AWS IoT to sign device certificates. If you have more than
one CA certificate (with the same subject field and public key) registered in your AWS account, you must
specify the CA certificate used to sign the device certificate when you register your device certificate. You
99
AWS IoT Developer Guide
X.509 Certificates
can register each device certificate manually, or you can use automatic registration, which allows devices to
register their certificate when they connect to AWS IoT for the first time.
You can also set the auto-registration-status to ENABLE when you use the register-ca-certificate
API to register your CA certificate:
When a device first attempts to connect to AWS IoT, as part of the TLS handshake, it must present a
registered CA certificate and a device certificate. AWS IoT recognizes the CA certificate as a registered CA
certificate and automatically registers the device certificate and sets its status to PENDING_ACTIVATION. This
means the device certificate was automatically registered and is awaiting activation. A certificate must be
in the ACTIVE state before it can be used to connect to AWS IoT. When AWS IoT automatically registers a
certificate or when a certificate in PENDING_ACTIVATION status connects, AWS IoT publishes a message
to the following MQTT topic:
$aws/events/certificates/registered/caCertificateID
Where caCertificateID is the ID of the CA certificate that issued the device certificate.
{
"certificateId": "certificateID",
"caCertificateId": "caCertificateId",
"timestamp": timestamp,
"certificateStatus": "PENDING_ACTIVATION",
"awsAccountId": "awsAccountId",
"certificateRegistrationTimestamp": "certificateRegistrationTimestamp"
}
You can create a rule that listens on this topic and performs some actions. We recommend that you create
a Lambda rule that verifies the device certificate is not on a certificate revocation list (CRL), activates the
certificate, and creates and attaches a policy to the certificate. The policy determines which resources the
device is able to access. For more information about how to create a Lambda rule that listens on the $aws/
events/certificates/registered/caCertificateID topic and performs these actions, see Just-in-Time
Registration.
100
AWS IoT Developer Guide
IAM Users, Groups, and Roles
When you register a device certificate, AWS will check if the associated CA certificate is ACTIVE. If the CA
certificate is INACTIVE, AWS IoT does not allow the device certificate to be registered. By marking the CA
certificate as INACTIVE, you prevent any new device certificates issued by the compromised CA to be
registered in your account. You can use the update-ca-certificate API to deactivate the CA certificate:
Note
Any registered device certificates that were signed by the compromised CA certificate will continue
to work until you explicitly revoke them.
Use the ListCertificatesByCA API to get a list of all registered device certificates that were signed by
the compromised CA. For each device certificate signed by the compromised CA certificate, use the
UpdateCertificate API to revoke the device certificate to prevent it from being used.
If you detect suspicious activity on a registered device certificate, you can use the update-certificate API
to revoke it:
If any error or exception occurs during the auto-registration of the device certificates, AWS IoT sends
events or messages to your logs in CloudWatch Logs. For more information about setting up the logs for
your account, see the Amazon CloudWatch documentation.
IAM roles also allow AWS IoT to access other AWS resources in your account on your behalf. For example,
if you want to have a device publish its state to a DynamoDB table, IAM roles allow AWS IoT to interact
with Amazon DynamoDB. For more information, see IAM Roles.
For message broker connections over HTTP, AWS IoT authenticates IAM users, groups, and roles using
the Signature Version 4 signing process. For information, see Signing AWS API Requests.
When using AWS Signature Version 4 with AWS IoT, clients must support the following in their TLS
implementation:
101
AWS IoT Developer Guide
Authorization
AWS IoT extends Amazon Cognito and allows policy attachment to Amazon Cognito identities. You can
attach a policy to an Amazon Cognito identity and give fine-grained permissions to an individual user of
your AWS IoT application. In this way, you can assign permissions between specific customers and their
devices. For more information, see Amazon Cognito Identity.
Authorization
Policies determine what an authenticated identity can do. An authenticated identity is used by devices,
mobile applications, web applications, and desktop applications. An authenticated identity can even be a
user typing AWS IoT CLI commands. The identity can execute AWS IoT operations only if it has a policy
that grants it permission.
Both AWS IoT policies and IAM policies are used with AWS IoT to control the operations an identity (also
called a principal) can perform. The policy type you use depends on the type of identity you are using to
authenticate with AWS IoT. The following table shows the identity types, the protocols they use, and the
policy types that can be used for authorization.
Control plane API allows you to perform administrative tasks like creating or updating certificates, things,
rules, and so on.
Data plane API allows you send data to and receive data from AWS IoT.
The type of policy you use depends on whether you are using control plane or data plane API.
102
AWS IoT Developer Guide
AWS IoT Policies
AWS IoT policies are attached to X.509 certificates or Amazon Cognito identities. IAM policies are attached
to an IAM user, group, or role. If you use the AWS IoT console or the AWS IoT CLI to attach the policy (to a
certificate or Amazon Cognito Identity), you use an AWS IoT policy. Otherwise, you use an IAM policy.
Policy-based authorization is a powerful tool. It gives you complete control over what a device, user, or
application can do in AWS IoT. For example, consider a device connecting to AWS IoT with a certificate.
You can allow the device to access all MQTT topics, or you can restrict its access to a single topic. In
another example, consider a user typing CLI commands at the command line. By using a policy, you
can allow or deny access to any command or AWS IoT resource for the user. You can also control an
application's access to AWS IoT resources.
AWS IoT defines a set of policy actions that describe the operations and resources to which you can grant
or deny access. For example:
103
AWS IoT Developer Guide
AWS IoT Policies
AWS IoT policies allow you to control access to the AWS IoT data plane. The AWS IoT data plane
consists of operations that allow you to connect to the AWS IoT message broker, send and receive MQTT
messages, and get or update thing shadows. For more information, see AWS IoT Policy Actions (p. 104).
An AWS IoT policy is a JSON document that contains one or more policy statements. Each statement
contains an Effect, an Action, and a Resource. The Effect specifies whether the action will be allowed
or denied. The Action specifies the action the policy is allowing or denying. The Resource specifies the
resource or resources on which the action is allowed or denied. The following policy grants all devices
permission to connect to the AWS IoT message broker, but restricts the device to publishing on a specific
MQTT topic:
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action":["iot:Publish"],
"Resource": ["arn:aws:iot:us-east-1:123456789012:topic/foo/bar"]
},
{
"Effect": "Allow",
"Action": ["iot:Connect"],
"Resource": ["*"]
}]
}
iot:Connect
Represents the permission to connect to the AWS IoT message broker. The iot:Connect permission
is checked every time a CONNECT request is sent to the broker. The message broker does not allow two
clients with the same client ID to stay connected at the same time. After the second client connects, the
broker detects this case and disconnects one of the clients. The iot:Connect permission can be used
to ensure only authorized clients can connect using a specific client ID.
iot:Publish
Represents the permission to publish on an MQTT topic. This permission is checked every time a
PUBLISH request is sent to the broker. This can be used to allow clients to publish to specific topic
patterns.
Note
You must also grant iot:Connect permission to grant iot:Publish permission.
iot:Receive
Represents the permission to receive a message from AWS IoT The iot:Receive permission is
checked every time a message is delivered to a client. Because this permission is checked on every
delivery, it can be used to revoke permissions to clients that are currently subscribed to a topic.
iot:Subscribe
Represents the permission to subscribe to an MQTT topic or topic filter. This permission is checked
every time a SUBSCRIBE request is sent to the broker. This can be used to allow clients to subscribe
to topics that match specific topic patterns.
104
AWS IoT Developer Guide
AWS IoT Policies
Note
You must also grant iot:Connect permission to grant iot:Subscribe permission.
iot:DeleteThingShadow
Action Resources
To specify a resource for an AWS IoT policy action, you must use the ARN of the resource. All resource
ARNs are of the following form:
The following table shows the resource to specify for each action type:
Action Resource
105
AWS IoT Developer Guide
AWS IoT Policies
policy variable would be replaced in the policy document by "100-234-3456". For more information about
policy variables, see IAM Policy Variables and Multi-Value Conditions.
iot:ClientId: The client ID used to connect to the AWS IoT message broker.
aws:SourceIp: The IP address of the client connected to the AWS IoT message broker.
The following AWS IoT policy shows the use of policy variables:
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": ["iot:Connect"],
"Resource": [
"arn:aws:iot:us-east-1:123451234510:client/${iot:ClientId}"
]
},
{
"Effect": "Allow",
"Action": ["iot:Publish"],
"Resource": [
"arn:aws:iot:us-east-1:123451234510:topic/foo/bar/${iot:ClientId}"
]
}]
}
In these examples ${iot:ClientId} will be replaced by the ID of the client connected to the AWS IoT
message broker when the policy is evaluated.When you use policy variables like ${iot:ClientId},
you can inadvertently open access to unintended topics. For example, if you use a policy that uses
${iot:ClientId} to specify a topic filter:
{
"Effect": "Allow",
"Action": ["iot:Subscribe"],
"Resource": [
"arn:aws:iot:us-east-1:123456789012:topicfilter/foo/${iot:ClientId}/bar"
]
}
A client can connect using + as the client ID. This would allow the user to subscribe to any topic matching
the topic filter foo/+/bar. To protect against such security gaps, use the iot:Connect policy action to
control which client IDs are able to connect. For example, this policy will allow only clients whose client ID is
clientid1 to connect:
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": ["iot:Connect"],
"Resource": [
"arn:aws:iot:us-east-1:123456789012:client/clientid1"
]
}]
}
106
AWS IoT Developer Guide
AWS IoT Policies
Issuer Attributes
The following AWS IoT policy variables allow you to allow or deny permissions based on certificate
attributes set by the certificate issuer.
iot:Certificate.Issuer.DistinguishedNameQualifier
iot:Certificate.Issuer.Country
iot:Certificate.Issuer.Organization
iot:Certificate.Issuer.OrganizationalUnit
iot:Certificate.Issuer.State
iot:Certificate.Issuer.CommonName
iot:Certificate.Issuer.SerialNumber
iot:Certificate.Issuer.Title
iot:Certificate.Issuer.Surname
iot:Certificate.Issuer.GivenName
iot:Certificate.Issuer.Initials
iot:Certificate.Issuer.Pseudonym
iot:Certificate.Issuer.GenerationQualifier
Subject Attributes
The following AWS IoT policy variables allow you to grant or deny permissions based on certificate subject
attributes set by the certificate issuer.
iot:Certificate.Subject.DistinguishedNameQualifier
iot:Certificate.Subject.Country
iot:Certificate.Subject.Organization
iot:Certificate.Subject.OrganizationalUnit
iot:Certificate.Subject.State
iot:Certificate.Subject.CommonName
iot:Certificate.Subject.SerialNumber
iot:Certificate.Subject.Title
iot:Certificate.Subject.Surname
iot:Certificate.Subject.GivenName
iot:Certificate.Subject.Initials
iot:Certificate.Subject.Pseudonym
iot:Certificate.Subject.GenerationQualifier
X.509 certificates allow these attributes to contain one or more values. By default, the policy variables for
each multi-value attribute return the first value. For example, the Certificate.Subject.Country attribute
might contain a list of country names. When evaluated in a policy, iot:Certificate.Subject.Country is
replaced by the first country name. You can request a specific attribute value using a zero-based index.
For example, iot:Certificate.Subject.Country#1 is replaced by the second country name in the
Certificate.Subject.Country attribute. If you specify an attribute value that does not exist (for example,
if you ask for a third value when there are only two values assigned to the attribute), no substitution will
be made and authorization will fail. You can use the .List suffix on the policy variable name to specify
107
AWS IoT Developer Guide
AWS IoT Policies
all values of the attribute. The following example policy allows any client to connect to AWS IoT, but
restricts publishing rights to those clients with certificates whose Certificate.Subject.Organization
attribute is set to "Example Corp" or "AnyCompany". This is done through the use of a "Condition"
attribute that specifies a condition for the preceding action. The condition in this case is that the
Certificate.Subject.Organization attribute of the certificate must include one of the listed values.
{
"Version":"2012-10-17",
"Statement":[
{
"Effect":"Allow",
"Action":[
"iot:Connect"
],
"Resource":[
"*"
]
},
{
"Effect":"Allow",
"Action":[
"iot:Publish"
],
"Resource":[
"*"
],
"Condition":{
"ForAllValues:StringEquals":{
"iot:Certificate.Subject.Organization.List":[
"Example Corp",
"AnyCompany"
]
}
}
}
]
}
iot:Certificate.Issuer.AlternativeName.RFC822Name
iot:Certificate.Issuer.AlternativeName.DNSName
iot:Certificate.Issuer.AlternativeName.DirectoryName
iot:Certificate.Issuer.AlternativeName.UniformResourceIdentifier
iot:Certificate.Issuer.AlternativeName.IPAddress
iot:Certificate.Subject.AlternativeName.RFC822Name
iot:Certificate.Subject.AlternativeName.DNSName
iot:Certificate.Subject.AlternativeName.DirectoryName
iot:Certificate.Subject.AlternativeName.UniformResourceIdentifier
iot:Certificate.Subject.AlternativeName.IPAddress
108
AWS IoT Developer Guide
AWS IoT Policies
Other Attributes
You can use iot:Certificate.SerialNumber to allow or deny access to AWS IoT resources based on the
serial number of a certificate. The iot:Certificate.AvailableKeys policy variable contains the name of
all certificate policy variables that contain values.
Wildcards
If wildcard characters are present in certificate attributes, the policy variable will not be replaced by
the certificate attribute value, leaving the ${policy-variable} text in the policy document. This might
cause authorization failure.
Array fields
Certificate attributes that contain arrays are limited to five items. Additional items will be ignored.
String length
All string values are limited to 1024 characters. If a certificate attribute contains a string longer than
1024 characters, the policy variable will not be replaced by the certificate attribute value, leaving the
${policy-variable} in the policy document. This might cause authorization failure.
iot:Connection.Thing.ThingName
iot:Connection.Thing.ThingTypeName
iot:Connection.Thing.Attributes[attributeName]
iot:Connection.Thing.IsAttached
iot:Connection.Thing.ThingName
This resolves to the name of the thing for which the policy is being evaluated. The thing name is set to the
client ID of the MQTT/Websocket connection. This policy variable is available only when connecting over
MQTT or MQTT over the WebSocket protocol.
iot:Connection.Thing.ThingTypeName
This resolves to the thing type associated with the thing for which the policy is being evaluated. The thing
name is set to the client ID of the MQTT/Websocket connection. The thing type name is obtained by a call
to the DescribeThing API. This policy variable is available only when connecting over MQTT or MQTT over
the WebSocket protocol.
iot:Connection.Thing.Attributes[attributeName]
This resolves to the value of the specified attribute associated with the thing for which the policy is being
evaluated. A thing can have up to 50 attributes. Each attribute will be available as a policy variable:
109
AWS IoT Developer Guide
AWS IoT Policies
iot:Connection.Thing.IsAttached
This resolves to true if the thing has a certificate or Amazon Cognito attached.
Example Policies
AWS IoT policies are specified in a JSON document. These are the components of an AWS IoT policy:
Version
Client - arn:aws:iot:region:account-id:client/client-id
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": [
"arn:aws:iot:us-east-1:123456789012:client/clientid1",
"arn:aws:iot:us-east-1:123456789012:client/clientid2",
"arn:aws:iot:us-east-1:123456789012:client/clientid3"
]
},
110
AWS IoT Developer Guide
AWS IoT Policies
{
"Effect": "Allow",
"Action": [
"iot:Publish",
"iot:Subscribe",
"iot:Receive"
],
"Resource": [
"*"
]
}
]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Deny",
"Action": [
"iot:Connect"
],
"Resource": [
"arn:aws:iot:us-east-1:123456789012:client/clientid1",
"arn:aws:iot:us-east-1:123456789012:client/clientid2"
]
},
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": [
"*"
]
}
]
}
The following policy allows the certificate holder using any client ID to subscribe to topic filter foo/*:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": [
"*"
]
},
{
"Effect": "Allow",
"Action": [
"iot:Subscribe"
],
"Resource": [
111
AWS IoT Developer Guide
AWS IoT Policies
"arn:aws:iot:us-east-1:123456789012:topicfilter/foo/*"
]
}
]
}
When you specify topic filters in AWS IoT policies for MQTT clients, MQTT wildcard characters "+" and
"#" will be treated as literal characters. Their use might result in unexpected behavior. For example, the
following policy will allow a client to subscribe to the topic filter foo/+/bar only:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": [
"*"
]
},
{
"Effect": "Allow",
"Action": [
"iot:Subscribe"
],
"Resource": [
"arn:aws:iot:us-east-1:123456789012:topicfilter/foo/+/bar"
]
}
]
}
Note
Attempts to subscribe to topic filters that match the pattern foo/+/bar like foo/baz/bar or foo/
goo/bar will fail and cause the client to disconnect.
You can use "*" as a wildcard in the resource attribute of the policy. For example, the following policy allows
the certificate holder to publish to all topics and subscribe to all topic filters in the AWS account:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:*"
],
"Resource": [
"*"
]
112
AWS IoT Developer Guide
AWS IoT Policies
}
]
}
The following policy allows the certificate holder to publish to all topics in the AWS account:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Publish",
"iot:Connect"
],
"Resource": [
"*"
]
}
]
}
You can also use the "*" wildcard at the end of a topic filter. For example, the following policy allows the
certificate holder to subscribe to a topic or topic filter matching the pattern foo/bar/*:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": [
"*"
]
},
{
"Effect": "Allow",
"Action": [
"iot:Subscribe"
],
"Resource": [
"arn:aws:iot:us-east-1:123456789012:topicfilter/foo/bar/*"
]
}
]
}
The following policy allows the certificate holder to publish to the foo/bar and foo/baz topics:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": [
"*"
]
113
AWS IoT Developer Guide
AWS IoT Policies
},
{
"Effect": "Allow",
"Action": [
"iot:Publish"
],
"Resource": [
"arn:aws:iot:us-east-1:123456789012:topic/foo/bar",
"arn:aws:iot:us-east-1:123456789012:topic/foo/baz"
]
}
]
}
The following policy prevents the certificate holder from publishing to the foo/bar topic:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": [
"*"
]
},
{
"Effect": "Deny",
"Action": [
"iot:Publish"
],
"Resource": [
"arn:aws:iot:us-east-1:123456789012:topic/foo/bar"
]
}
]
}
The following policy allows the certificate holder to publish on topic foo and prevents the certificate holder
from publishing to topic bar:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": [
"*"
]
},
{
"Effect": "Allow",
"Action": [
"iot:Publish"
],
"Resource": [
"arn:aws:iot:us-east-1:123456789012:topic/foo"
]
114
AWS IoT Developer Guide
AWS IoT Policies
},
{
"Effect": "Deny",
"Action": [
"iot:Publish"
],
"Resource": [
"arn:aws:iot:us-east-1:123456789012:topic/bar"
]
}
]
}
The following policy allows the certificate holder to subscribe to topic filter foo/bar:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": [
"*"
]
},
{
"Effect": "Allow",
"Action": [
"iot:Subscribe"
],
"Resource": [
"arn:aws:iot:us-east-1:123456789012:topicfilter/foo/bar"
]
}
]
}
The following policy allows the certificate holder to publish on the arn:aws:iot:us-
east-1:123456789012:topic/iotmonitor/provisioning/8050373158915119971 topic and allows the
certificate holder to subscribe to the topic filter arn:aws:iot:us-east-1:123456789012:topicfilter/
iotmonitor/provisioning/8050373158915119971:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": [
"*"
]
},
{
"Effect": "Allow",
"Action": [
"iot:Publish",
"iot:Receive"
],
"Resource": [
115
AWS IoT Developer Guide
AWS IoT Policies
"arn:aws:iot:us-east-1:123456789012:topic/iotmonitor/
provisioning/8050373158915119971"
]
},
{
"Effect": "Allow",
"Action": [
"iot:Subscribe"
],
"Resource": [
"arn:aws:iot:us-east-1:123456789012:topicfilter/iotmonitor/
provisioning/8050373158915119971"
]
}
]
}
iot:Connect
iot:Publish
iot:Subscribe
iot:Receive
iot:GetThingShadow
iot:UpdateThingShadow
iot:DeleteThingShadow
Note
For other AWS IoT operations or for unauthenticated identities, AWS IoT does not scope down
the permissions attached to the Amazon Cognito identity pool role. For both authenticated and
unauthenticated identities, this is the most permissive policy that we recommend attaching to the
Amazon Cognito pool role.
To allow unauthenticated Amazon Cognito identities to publish messages over HTTP on any topic, attach
the following policy to the Amazon Cognito identity pool role:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect",
"iot:Publish",
"iot:Subscribe",
"iot:Receive",
"iot:GetThingShadow",
"iot:UpdateThingShadow",
"iot:DeleteThingShadow"
],
"Resource": ["*"]
}]
}
116
AWS IoT Developer Guide
AWS IoT Policies
To allow unauthenticated Amazon Cognito identities to publish MQTT messages over HTTP on any topic in
your account, attach the following policy to the Amazon Cognito identity pool role:
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": ["iot:Publish"],
"Resource": ["*"]
}]
}
Note
This example is for illustration only. Unless your service absolutely requires it, we recommend the
use of a more restrictive policy, one that does not allow unauthenticated Amazon Cognito identities
to publish on any topic.
To allow unauthenticated Amazon Cognito identities to publish MQTT messages over HTTP on topic1 in
your account, attach the following policy to your Amazon Cognito identity pool role:
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": ["iot:Publish"],
"Resource": ["arn:aws:iot:us-east-1:123456789012:topic/topic1"]
}]
}
For an authenticated Amazon Cognito identity to publish MQTT messages over HTTP on topic1 in your
AWS account, you must specify two policies, as outlined here. The first policy must be attached to an
Amazon Cognito identity pool role. It allows identities from that pool to make a publish call. The second
policy must be attached to an Amazon Cognito user using the AWS IoT AttachPrincipalPolicy API. It allows
the specified Amazon Cognito user access to the topic1 topic.
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [ "iot:Publish"],
"Resource": ["arn:aws:iot:us-east-1:123456789012:topic/topic1"]
}]
}
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": ["iot:Publish"],
"Resource": ["arn:aws:iot:us-east-1:123456789012:topic/topic1"]
}]
}
Similarly, the following example policy allows the Amazon Cognito user to publish MQTT messages over
HTTP on the topic1 and topic2 topics. Two policies are required. The first policy gives the Amazon
117
AWS IoT Developer Guide
AWS IoT Policies
Cognito identity pool role the ability to make the publish call. The second policy gives the Amazon Cognito
user access to the topic1 and topic2 topics.
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": ["iot:Publish"],
"Resource": ["*"]
}]
}
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": ["iot:Publish"],
"Resource": [
"arn:aws:iot:us-east-1:123456789012:topic/topic1",
"arn:aws:iot:us-east-1:123456789012:topic/topic2"
]
}]
}
The following policies allow multiple Amazon Cognito users to publish to a topic. Two policies per Amazon
Cognito identity are required. The first policy gives the Amazon Cognito identity pool role the ability to make
the publish call. The second and third policies give the Amazon Cognito users access to the topics topic1
and topic2, respectively.
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": ["iot:Publish"],
"Resource": ["*"]
}]
}
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": ["iot:Publish"],
"Resource": ["arn:aws:iot:us-east-1:123456789012:topic/topic1"]
}]
}
{
"Version": "2012-10-17",
"Statement": [{
118
AWS IoT Developer Guide
AWS IoT Policies
"Effect": "Allow",
"Action": ["iot:Publish"],
"Resource": ["arn:aws:iot:us-east-1:123456789012:topic/topic2"]
}]
}
The following policy prevents the certificate holder using any client ID from receiving messages from a
topic:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Deny",
"Action": [
"iot:Receive"
],
"Resource": [
"arn:aws:iot:us-east-1:123456789012:topic/foo/restricted"
]
},
{
"Effect": "Allow",
"Action": [
"iot:*"
],
"Resource": [
"*"
]
}
]
}
The following policy allows the certificate holder using any client ID to subscribe and receive messages on
one topic:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": [*]
},
{
"Effect": "Allow",
"Action": [
"iot:Subscribe"
],
"Resource": [
"arn:aws:iot:us-east-1:123456789012:topicfilter/foo/bar"
]
},
{
"Effect": "Allow",
"Action": [
"iot:Receive"
],
"Resource": [
119
AWS IoT Developer Guide
AWS IoT Policies
"arn:aws:iot:us-east-1:123456789012:topic/foo/bar"
]
}
]
}
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action":["iot:Publish"],
"Resource": ["arn:aws:iot:us-east-1:123456789012:topic/${iot:CertificateId}"]
},
{
"Effect": "Allow",
"Action": ["iot:Connect"],
"Resource": ["*"]
}]
}
The following policy allows a device to publish on a topic whose name is equal to the subject's common
name field of the certificate with which the device authenticated itself:
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action":["iot:Publish"],
"Resource": ["arn:aws:iot:us-east-1:123456789012:topic/
${iot:Certificate.Issuer.CommonName}"]
},
{
"Effect": "Allow",
"Action": ["iot:Connect"],
"Resource": ["*"]
}]
}
The following policy allows a device to publish on a topic that is prefixed with "admin/" when the certificate
used to authenticate the device has its Subject.CommonName.2 field set to "Administrator":
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": ["iot:Connect"],
"Resource": ["*"]
},
{
"Effect": "Allow",
"Action": ["iot:Publish"],
"Resource": ["arn:aws:iot:us-east-1:123456789012:topic/admin/*"],
"Condition": {
"StringEquals": {
"iot:Certificate.Subject.CommonName.2": "Administrator"
}
120
AWS IoT Developer Guide
AWS IoT Policies
}
}]
}
The following policy allows a device to publish on a topic that is prefixed with "admin/" when the certificate
used to authenticate the device has any one of its Subject.Common fields set to "Administrator":
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": ["iot:Connect"],
"Resource": ["*"]
},
{
"Effect": "Allow",
"Action": ["iot:Publish"],
"Resource": ["arn:aws:iot:us-east-1:123456789012:topic/admin/*"],
"Condition": {
"ForAnyValue:StringEquals": {
"iot:Certificate.Subject.CommonName.List": "Administrator"
}
}
}]
}
{
"Version":"2012-10-17",
"Statement":[{
"Effect":"Allow",
"Action":["iot:Publish"],
"Resource":[
"arn:aws:iot:us-east-1:123456789012:topic/
${iot:Connection.Thing.ThingTypeName}/${iot:Connection.Thing.ThingName}"
]
}]
}
The following policy allows the device to connect if it is attached to the certificate used to authenticate with
AWS IoT.
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": ["iot:Connect"],
"Resource": ["*"],
"Condition":{
"Bool":{
"iot:Connection.Thing.IsAttached":["true"]
}
}
}]
}
The following policy allows a device to publish on a set of topics ("/foo/bar" and "/foo/baz") if:
121
AWS IoT Developer Guide
AWS IoT Policies
The thing associated with the device has an attribute called "Manufacturer" with a value of "foo", "bar", or
"baz".
The thing associated with the device exists in the thing registry and is attached to the certificate used to
connect to AWS IoT.
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": ["iot:Publish"],
"Resource": [
"arn:aws:iot:us-east-1:123456789012:topic/foo/bar",
"arn:aws:iot:us-east-1:123456789012:topic/foo/baz"
],
"Condition": {
"ForAnyValue:StringLike": {
"iot:Connection.Thing.Attributes[Manufacturer]": [
"foo",
"bar",
"baz"
]
}
}
}]
}
The topic is composed of the thing type name, a '/', and the thing name.
The thing exists in the thing registry.
The thing is attached to the certificate used to connect to AWS IoT.
{
"Version":"2012-10-17",
"Statement":[{
"Effect":"Allow",
"Action":["iot:Publish"],
"Resource":[
"arn:aws:iot:us-east-1:123456789012:topic/
${iot:Connection.Thing.ThingTypeName}/${iot:Connection.Thing.ThingName}"
]
}]
}
The following policy allows a device to publish only on its own thing shadow topic, if the thing exists in the
thing registry.
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": ["iot:Publish"],
"Resource": [
"arn:aws:iot:us-east-1:123456789012:topic/$aws/things/
${iot:Connection.Thing.ThingName}/shadow/update"
]
}]
}
122
AWS IoT Developer Guide
IAM IoT Policies
AttachPrincipalPolicy iot:AttachPrincipalPolicy
arn:aws:iot:region:account-id:cert/cert-id
AttachThingPrincipal iot:AttachThingPrincipal
arn:aws:iot:region:account-id:cert/cert-id
CancelCertificateTransfer
iot:CancelCertificateTransfer
arn:aws:iot:region:account-id:cert/cert-id
Note
The AWS account specified in the ARN must be the
account to which the certificate is being transferred.
CreateCertificateFromCsr
iot:CreateCertificateFromCsr
*
CreateKeysAndCertificate
iot:CreateKeysAndCertificate
*
CreatePolicy iot:CreatePolicy *
CreatePolicyVersion iot:CreatePolicyVersion
arn:aws:iot:region:account-id:policy/policy-name
Note
This must be an AWS IoT policy, not an IAM policy.
DeleteCACertificate iot:DeleteCACertificate
arn:aws:iot:region:account-id:cacert/cert-id
DeletePolicyVersion iot:DeletePolicyVersion
arn:aws:iot:region:account-id:policy/policy-name
DeleteRegistrationCode
iot:DeleteRegistrationCode
*
DeprecateThingType iot:DeprecateThingType
arn:aws:iot:region:account-id:thingtype/thing-type-name
DescribeCaCertificateiot:DescribeCaCertificate
arn:aws:iot:region:account-id:cacert/cert-id
123
AWS IoT Developer Guide
IAM IoT Policies
DescribeCertificate iot:DescribeCertificatearn:aws:iot:region:account-id:cert/cert-id
DescribeEndpoint iot:DescribeEndpoint *
DescribeThingType iot:DescribeThingTypearn:aws:iot:region:account-id:thingtype/thing-type-name
DetachPrincipalPolicyiot:DetachPrincipalPolicy
arn:aws:iot:region:account-id:cert/cert-id
DetachThingPrincipaliot:DetachThingPrincipal
arn:aws:iot:region:account-id:cert/cert-id
GetLoggingOptions iot:GetLoggingOptions*
GetRegistrationCode iot:GetRegistrationCode
*
ListCaCertificates iot:ListCaCertificates *
ListCertificates iot:ListCertificates *
iot:ListCertificatesByCa
iot:ListCertificatesByCa
*
ListOutgoingCertificates
iot:ListOutgoingCertificates
*
ListPolicies iot:ListPolicies *
ListPrincipalPolicies iot:ListPrincipalPolicies
The ARN of the certificate: arn:aws:iot:region:account-
id:cert/cert-id
ListThings iot:ListThings *
ListThingTypes iot:ListThingTypes *
ListTopicRules iot:ListTopicRules *
RegisterCACertificateiot:RegisterCACertificate
*
124
AWS IoT Developer Guide
Cross Account Access
RegisterCertificate iot:RegisterCertificate*
RejectCertificateTransfer
iot:RejectCertificateTransfer
arn:aws:iot:region:account-id:cert/cert-id
ReplaceTopicRule iot:ReplaceTopicRulearn:aws:iot:region:account-id:rule/rule-name
SetDefaultPolicyVersion
iot:SetDefaultPolicyVersion
arn:aws:iot:region:account-id:policy/policy-name
SetLoggingOptions iot:SetLoggingOptionsarn:aws:iot:region:account-id:role/role-name
TransferCertificate iot:TransferCertificatearn:aws:iot:region:account-id:cert/cert-id
UpdateCACertificate iot:UpdateCACertificate
arn:aws:iot:region:account-id:cacert/cert-id
125
AWS IoT Developer Guide
Transport Security
First, create an IAM policy just like you would for other users and certificates in your AWS account. For
example, the following policy grants permissions to connect and publish to the /foo/bar topic.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": [
"*"
]
},
{
"Effect": "Allow",
"Action": [
"iot:Publish"
],
"Resource": [
"arn:aws:iot:us-east-1:123456789012:topic/foo/bar"
]
}]
}
Next, follow the steps in Creating a Role for an IAM User. Enter the AWS account ID of the AWS account
with which you want to share access. Then, in the final step, attach the policy you just created to the role. If,
at a later time, you need to modify the AWS account ID to which you are granting access, you can use the
following trust policy format to do so.
{
"Version":"2012-10-17",
"Statement":[{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam:us-east-1:111111111111:user/MyUser"
},
"Action": "sts:AssumeRole"
}]
}
Transport Security
The AWS IoT message broker and Thing Shadows service encrypt all communication with TLS. TLS is
used to ensure the confidentiality of the application protocols (MQTT, HTTP) supported by AWS IoT. TLS is
available in a number of programming languages and operating systems.
For MQTT, TLS encrypts the connection between the device and the broker. TLS client authentication is
used by AWS IoT to identify devices. For HTTP, TLS encrypts the connection between the device and the
broker. Authentication is delegated to AWS Signature Version 4.
ECDHE-ECDSA-AES128-GCM-SHA256 (recommended)
126
AWS IoT Developer Guide
TLS Cipher Suite Support
ECDHE-RSA-AES128-GCM-SHA256 (recommended)
ECDHE-ECDSA-AES128-SHA256
ECDHE-RSA-AES128-SHA256
ECDHE-ECDSA-AES128-SHA
ECDHE-RSA-AES128-SHA
ECDHE-ECDSA-AES256-GCM-SHA384
ECDHE-RSA-AES256-GCM-SHA384
ECDHE-ECDSA-AES256-SHA384
ECDHE-RSA-AES256-SHA384
ECDHE-RSA-AES256-SHA
ECDHE-ECDSA-AES256-SHA
AES128-GCM-SHA256
AES128-SHA256
AES128-SHA
AES256-GCM-SHA384
AES256-SHA256
AES256-SHA
127
AWS IoT Developer Guide
Protocols
The AWS IoT message broker is a publish/subscribe broker service that enables the sending and receiving
of messages to and from AWS IoT. When communicating with AWS IoT, a client sends a message
addressed to a topic like Sensor/temp/room1. The message broker, in turn, sends the message to all
clients that have registered to receive messages for that topic. The act of sending the message is referred
to as publishing. The act of registering to receive messages for a topic filter is referred to as subscribing.
The topic namespace is isolated for each AWS account and region pair. For example, the Sensor/temp/
room1 topic for an AWS account is independent from the Sensor/temp/room1 topic for another AWS
account. This is true of regions, too. The Sensor/temp/room1 topic in the same AWS account in us-
east-1 is independent from the same topic in us-west-2. AWS IoT does not support sending and receiving
messages across AWS accounts and regions.
The message broker maintains a list of all client sessions and the subscriptions for each session. When a
message is published on a topic, the broker checks for sessions with subscriptions that map to the topic.
The broker then forwards the publish message to all sessions that have a currently connected client.
Protocols
The message broker supports the use of the MQTT protocol to publish and subscribe and the HTTPS
protocol to publish. Both protocols are supported through IP version 4 and IP version 6. The message
broker also supports MQTT over the WebSocket protocol.
Protocol/Port Mappings
The following table shows each protocol supported by AWS IoT, the authentication method, and port used
for each protocol.
128
AWS IoT Developer Guide
MQTT
MQTT
MQTT is a widely adopted lightweight messaging protocol designed for constrained devices. For more
information, see MQTT.
Although the AWS IoT message broker implementation is based on MQTT version 3.1.1, it deviates from
the specification as follows:
In AWS IoT, subscribing to a topic with Quality of Service (QoS) 0 means a message will be delivered
zero or more times. A message might be delivered more than once. Messages delivered more than once
might be sent with a different packet ID. In these cases, the DUP flag is not set.
AWS IoT does not support publishing and subscribing with QoS 2. The AWS IoT message broker does
not send a PUBACK or SUBACK when QoS 2 is requested.
The QoS levels for publishing and subscribing to a topic have no relation to each other. One client can
subscribe to a topic using QoS 1 while another client can publish to the same topic using QoS 0.
When responding to a connection request, the message broker sends a CONNACK message. This
message contains a flag to indicate if the connection is resuming a previous session. The value of this
flag might be incorrect if two MQTT clients connect with the same client ID simultaneously.
When a client subscribes to a topic, there might be a delay between the time the message broker sends
a SUBACK and the time the client starts receiving new matching messages.
The MQTT specification provides a provision for the publisher to request that the broker retain the last
message sent to a topic and send it to all future topic subscribers. AWS IoT does not support retained
messages. If a request is made to retain messages, the connection is disconnected.
The message broker uses the client ID to identify each client. The client ID is passed in from the client to
the message broker as part of the MQTT payload. Two clients with the same client ID are not allowed to
be connected concurrently to the message broker. When a client connects to the message broker using
a client ID that another client is using, a CONNACK message will be sent to both clients and the currently
connected client will be disconnected.
The message broker does not support persistent sessions (clean session set to 0). All sessions are
assumed to be clean sessions and messages are not stored across sessions. If an MQTT client sends a
message with the clean session attribute set to false, the client will be disconnected.
On rare occasions, the message broker might resend the same logical PUBLISH message with a
different packet ID.
The message broker does not guarantee the order in which messages and ACK are received.
HTTP
The message broker supports clients connecting with the HTTP protocol using a REST API. Clients can
publish by sending a POST message to <AWS IoT Endpoint>/topics/<url_encoded_topic_name>?
qos=1".
For example, you can use curl to emulate a button press. If you followed the tutorial in Getting Started with
AWS IoT (p. 19), rather than using the AWS IoT MQTT client to publish a message as you did in AWS IoT
MQTT Client (p. 44), use something like the following command:
129
AWS IoT Developer Guide
MQTT Over the WebSocket Protocol
--tlsv1.2
Use TLSv1.2 (SSL). curl must be installed with OpenSSL and you must use version 1.2 of TLS.
--cacert <filename>
The HTTP POST data you want to publish. In this case, we emulate the data sent by a single button
press.
"https://..."
The URL. In this case the REST API endpoint for the thing. (To find the endpoint for a thing, from the
AWS IoT console choose Registry to expand your choices. Choose Things, choose the thing, and
then choose Interact.) After the endpoint add the port (:8443) followed by the topic and, finally, specify
the quality of service in a query string (?qos=1).
A WebSocket connection is initiated on a client by sending an HTTP GET request. The URL you use is of
the following form:
wss://<endpoint>.iot.<region>.amazonaws.com/mqtt
wss
Your AWS account-specific AWS IoT endpoint. You can use the AWS IoT CLI describe-endpoint
command to find this endpoint.
region
Specifies you will be sending MQTT messages over the WebSocket protocol.
130
AWS IoT Developer Guide
MQTT Over the WebSocket Protocol
When the server responds, the client sends an upgrade request to indicate to the server it will communicate
using the WebSocket protocol. After the server acknowledges the upgrade request, all communication is
performed using the WebSocket protocol. The WebSocket implementation you use acts as a transport
protocol. The data you send over the WebSocket protocol are MQTT messages.
The following JavaScript defines some utility functions used in generating a Signature Version 4 request.
/**
* utilities to do sigv4
* @class SigV4Utils
*/
function SigV4Utils() {}
131
AWS IoT Developer Guide
MQTT Over the WebSocket Protocol
2. Create a string to sign, generate a signing key, and sign the string.
Take the canonical URL you created in the previous step and assemble it into a string to sign. You
do this by creating a string composed of the hashing algorithm, the date, the credential scope, and
the SHA of the canonical request. Next, generate the signing key and sign the string, as shown in the
following JavaScript code.
The following JavaScript code shows how to add the signing information to the query string.
4. If you have session credentials (from an STS server, AssumeRole, or Amazon Cognito), append the
session token to the end of the URL string after signing:
canonicalQuerystring += '&X-Amz-Security-Token=' +
encodeURIComponent(credentials.sessionToken);
132
AWS IoT Developer Guide
Topics
The following JavaScript code shows how to create a Paho MQTT client and call CONNECT to AWS
IoT. The endpoint argument is your AWS account-specific endpoint. The clientId is a text identifier
that is unique among all clients simultaneously connected in your AWS account.
Node.js
iOS
Android
For a reference implementation for connecting a web application to AWS IoT using MQTT over the
WebSocket protocol, see AWS Labs WebSocket sample.
If you are using a programming or scripting language that is not currently supported, any existing
WebSocket library can be used as long as the initial WebSocket upgrade request (HTTP POST) is signed
using AWS Signature Version 4. Some MQTT clients, such as Eclipse Paho for JavaScript, support the
WebSocket protocol natively.
Topics
The message broker uses topics to route messages from publishing clients to subscribing clients. The
forward slash (/) is used to separate topic hierarchy. The following table lists the wildcards that can be used
in the topic filter when you subscribe.
Topic Wildcards
Wildcard Description
133
AWS IoT Developer Guide
Reserved Topics
Wildcard Description
published to Sensor/, Sensor/temp, Sensor/temp/
room1, but not the messages published to Sensor.
Reserved Topics
Any topics beginning with $ are considered reserved and are not supported for publishing and subscribing
except for those topics listed below. Any other attemps to publish or subscribe on topics beginning with $
will result in a terminated connection.
134
AWS IoT Developer Guide
Reserved Topics
135
AWS IoT Developer Guide
Reserved Topics
136
AWS IoT Developer Guide
Lifecycle Events
Lifecycle Events
AWS IoT publishes lifecycle events on the MQTT topics discussed in the following sections. These
messages allow you to be notified of lifecycle events from the message broker.
Note
Lifecycle messages might be sent out of order and you might receive duplicate messages.
{
"Version":"2012-10-17",
"Statement":[{
"Effect":"Allow",
"Action":[
"iot:Subscribe",
"iot:Receive"
],
"Resource":[
"arn:aws:iot:region:account:topicfilter/$aws/events/*"
]
}]
}
Connect/Disconnect Events
AWS IoT publishes a message to the following MQTT topics when a client connects or disconnects:
$aws/events/presence/connected/clientId
or
$aws/events/presence/disconnected/clientId
Where clientId is the MQTT client ID that connects to or disconnects from the AWS IoT message broker.
{
"clientId": "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6",
"timestamp": 1460065214626,
"eventType": "connected",
"sessionIdentifier": "00000000-0000-0000-0000-000000000000",
"principalIdentifier": "000000000000/ABCDEFGHIJKLMNOPQRSTU:some-user/
ABCDEFGHIJKLMNOPQRSTU:some-user"
}
The following is a list of JSON elements that are contained in the connection/disconnection messages
published to the $aws/events/presence/connected/clientId topic.
clientId
137
AWS IoT Developer Guide
Subscribe/Unsubscribe Events
Note
Client IDs that contain # or + will not receive lifecycle events.
eventType
The credential used to authenticate. For TLS mutual authentication certificates, this is the certificate ID.
For other connections, this is IAM credentials.
sessionIdentifier
A globally unique identifier in AWS IoT that exists for the life of the session.
timestamp
An approximation of when the event occurred, expressed in milliseconds since the Unix epoch. The
accuracy of the timestamp is +/- 2 minutes.
Subscribe/Unsubscribe Events
AWS IoT publishes a message to the following MQTT topic when a client subscribes or unsubscribes to an
MQTT topic:
$aws/events/subscriptions/subscribed/clientId
or
$aws/events/subscriptions/unsubscribed/clientId
Where clientId is the MQTT client ID that connects to the AWS IoT message broker.
{
"clientId": "186b5",
"timestamp": 1460065214626,
"eventType": "subscribed" | "unsubscribed",
"sessionIdentifier": "00000000-0000-0000-0000-000000000000",
"principalIdentifier": "000000000000/ABCDEFGHIJKLMNOPQRSTU:some-user/
ABCDEFGHIJKLMNOPQRSTU:some-user"
"topics" : ["foo/bar","device/data","dog/cat"]
}
The following is a list of JSON elements that are contained in the subscribed and unsubscribed messages
published to the $aws/events/subscriptions/subscribed/clientId and $aws/events/subscriptions/
unsubscribed/clientId topics.
clientId
138
AWS IoT Developer Guide
Subscribe/Unsubscribe Events
principalIdentifier
The credential used to authenticate. For TLS mutual authentication certificates, this is the certificate ID.
For other connections, this is IAM credentials.
sessionIdentifier
A globally unique identifier in AWS IoT that exists for the life of the session.
timestamp
An approximation of when the event occurred, expressed in milliseconds since the Unix epoch. The
accuracy of the timestamp is +/- 2 minutes.
topics
Note
Lifecycle messages might be sent out of order. You might receive duplicate messages.
139
AWS IoT Developer Guide
Rules give your devices the ability to interact with AWS services. Rules are analyzed and actions are
performed based on the MQTT topic stream. You can use rules to support tasks like these:
Before AWS IoT can perform these actions, you must grant it permission to access your AWS resources
on your behalf. When the actions are performed, you incur the standard charges for the AWS services you
use.
Contents
Granting AWS IoT the Required Access (p. 141)
Pass Role Permissions (p. 142)
Creating an AWS IoT Rule (p. 143)
Viewing Your Rules (p. 146)
SQL Versions (p. 146)
Troubleshooting a Rule (p. 148)
140
AWS IoT Developer Guide
Granting AWS IoT the Required Access
1. Save the following trust policy document, which grants AWS IoT permission to assume the role, to a
file called iot-role-trust.json:
{
"Version":"2012-10-17",
"Statement":[{
"Effect": "Allow",
"Principal": {
"Service": "iot.amazonaws.com"
},
"Action": "sts:AssumeRole"
}]
}
Use the create-role command to create an IAM role specifying the iot-role-trust.json file:
{
"Role": {
"AssumeRolePolicyDocument": "url-encoded-json",
"RoleId": "AKIAIOSFODNN7EXAMPLE",
"CreateDate": "2015-09-30T18:43:32.821Z",
"RoleName": "my-iot-role",
"Path": "/",
"Arn": "arn:aws:iam::123456789012:role/my-iot-role"
}
}
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": "dynamodb:*",
"Resource": "*"
}]
}
This JSON is an example policy document that grants AWS IoT administrator access to DynamoDB.
141
AWS IoT Developer Guide
Pass Role Permissions
Use the create-policy command to grant AWS IoT access to your AWS resources upon assuming the
role, passing in the iot-policy.json file:
For more information about how to grant access to AWS services in policies for AWS IoT, see Creating
an AWS IoT Rule (p. 143).
The output of the create-policy command will contain the ARN of the policy. You will need to attach the
policy to a role.
{
"Policy": {
"PolicyName": "my-iot-policy",
"CreateDate": "2015-09-30T19:31:18.620Z",
"AttachmentCount": 0,
"IsAttachable": true,
"PolicyId": "ZXR6A36LTYANPAI7NJ5UV",
"DefaultVersionId": "v1",
"Path": "/",
"Arn": "arn:aws:iam::123456789012:policy/my-iot-policy",
"UpdateDate": "2015-09-30T19:31:18.620Z"
}
}
When creating or replacing a rule you are, in effect, passing a role to the rules engine. The user that
performing this operation requires the iam:PassRole permission. To ensure you have this permission,
create a policy that grants the iam:PassRole permission and attach it to your IAM user. The following policy
shows how to allow iam:PassRole permission for a role.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1",
"Effect": "Allow",
"Action": [
"iam:PassRole"
],
"Resource": [
"arn:aws:iam::123456789012:role/myRole"
]
}
]
}
142
AWS IoT Developer Guide
Creating an AWS IoT Rule
In this policy example, the iam:PassRole permission is granted for the role myRole. The role is specified
using the role's ARN. You must attach this policy to your IAM user or role to which your user belongs. For
more information, see Working with Managed Policies.
Note
Lambda functions use resource-based policy, where the policy is attached directly to the Lambda
function itself. When creating a rule that invokes a Lambda function, you do not pass a role, so the
user creating the rule does not need the iam:PassRole permission. For more information about
Lambda function authorization, see Granting Permissions Using a Resource Policy.
Rule name
A simplified SQL syntax to filter messages received on an MQTT topic and push the data elsewhere.
For more information, see AWS IoT SQL Reference (p. 158).
SQL version
The version of the SQL rules engine to use when evaluating the rule. Although this property is optional,
we strongly recommend that you specify the SQL version. If this property is not set, the default,
2015-10-08, will be used.
One or more actions
The actions AWS IoT performs when executing the rule. For example, you can insert data into a
DynamoDB table, write data to an Amazon S3 bucket, publish to an Amazon SNS topic, or invoke a
Lambda function.
When you create a rule, be aware of how much data you are publishing on topics. If you create rules
that include a wildcard topic pattern, they might match a large percentage of your messages, and you
might need to increase the capacity of the AWS resources used by the target actions. Also, if you create
a republish rule that includes a wildcard topic pattern, you can end up with a circular rule that causes an
infinite loop.
Note
Creating and updating rules are administrator-level actions. Any user who has permission to create
or update rules will be able to access data processed by the rules.
The following is an example payload file with a rule that inserts all messages sent to the iot/test topic into
the specified DynamoDB table. The SQL statement filters the messages and the role ARN grants AWS IoT
permission to write to the DynamoDB table.
143
AWS IoT Developer Guide
Creating an AWS IoT Rule
{
"sql": "SELECT * FROM 'iot/test'",
"ruleDisabled": false,
"awsIotSqlVersion": "2016-03-23",
"actions": [{
"dynamoDB": {
"tableName": "my-dynamodb-table",
"roleArn": "arn:aws:iam::123456789012:role/my-iot-role",
"hashKeyField": "topic",
"hashKeyValue": "${topic(2)}",
"rangeKeyField": "timestamp",
"rangeKeyValue": "${timestamp()}"
}
}]
}
The following is an example payload file with a rule that inserts all messages sent to the iot/test topic
into the specified S3 bucket. The SQL statement filters the messages, and the role ARN grants AWS IoT
permission to write to the Amazon S3 bucket.
{
"rule": {
"awsIotSqlVersion": "2016-03-23",
"sql": "SELECT * FROM 'iot/test'",
"ruleDisabled": false,
"actions": [
{
"s3": {
"roleArn": "arn:aws:iam::123456789012:role/aws_iot_s3",
"bucketName": "my-bucket",
"key": "myS3Key"
}
}
],
"ruleName": "MyS3Rule"
}
}
The following is an example payload file with a rule that pushes data to Amazon ES:
{
"sql":"SELECT *, timestamp() as timestamp FROM 'iot/test'",
"ruleDisabled":false,
"awsIotSqlVersion": "2016-03-23",
"actions":[
{
"elasticsearch":{
"roleArn":"arn:aws:iam::123456789012:role/aws_iot_es",
"endpoint":"https://my-endpoint",
"index":"my-index",
"type":"my-type",
"id":"${newuuid()}"
}
}
]
}
The following is an example payload file with a rule that invokes a Lambda function:
144
AWS IoT Developer Guide
Creating an AWS IoT Rule
"sql": "expression",
"ruleDisabled": false,
"awsIotSqlVersion": "2016-03-23",
"actions": [{
"lambda": {
"functionArn": "arn:aws:lambda:us-west-2:123456789012:function:my-lambda-
function"
}
}]
}
The following is an example payload file with a rule that publishes to an Amazon SNS topic:
{
"sql": "expression",
"ruleDisabled": false,
"awsIotSqlVersion": "2016-03-23",
"actions": [{
"sns": {
"targetArn": "arn:aws:sns:us-west-2:123456789012:my-sns-topic",
"roleArn": "arn:aws:iam::123456789012:role/my-iot-role"
}
}]
}
The following is an example payload file with a rule that republishes on a different MQTT topic:
{
"sql": "expression",
"ruleDisabled": false,
"awsIotSqlVersion": "2016-03-23",
"actions": [{
"republish": {
"topic": "my-mqtt-topic",
"roleArn": "arn:aws:iam::123456789012:role/my-iot-role"
}
}]
}
The following is an example payload file with a rule that pushes data to an Amazon Kinesis Firehose
stream:
{
"sql": "SELECT * FROM 'my-topic'",
"ruleDisabled": false,
"awsIotSqlVersion": "2016-03-23",
"actions": [{
"firehose": {
"roleArn": ""arn:aws:iam::123456789012:role/my-iot-role",
"deliveryStreamName": "my-stream-name"
}
}]
}
The following is an example payload file with a rule that uses the Amazon Machine Learning
machinelearning_predict function to republish to a topic if the data in the MQTT payload is classified as a
1.
145
AWS IoT Developer Guide
Viewing Your Rules
SQL Versions
The AWS IoT rules engine uses an SQL-like syntax to select data from MQTT messages. The SQL
statements are interpreted based on a SQL version specified with the awsIotSqlVersion property in a
JSON document that describes the rule. For more information about the structure of JSON rule documents,
see Creating a Rule (p. 143). The awsIotSqlVersion property allows you to specify which version of the
AWS IoT SQL rules engine you want to use. When a new version is deployed, you can continue to use an
older version or change your rule to use the new version. Your current rules will continue to use the version
with which they were created.
The following JSON example shows how to specify the SQL version using the awsIotSqlVersion property:
{
"sql": "expression",
"ruleDisabled": false,
"awsIotSqlVersion": "2016-03-23",
"actions": [{
"republish": {
"topic": "my-mqtt-topic",
"roleArn": "arn:aws:iam::123456789012:role/my-iot-role"
}
}]
}
146
AWS IoT Developer Guide
What's New in the 2016-03-23 SQL Rules Engine Version
Inter-Object Queries
This feature allows you to query for an attribute in a JSON object. For example, given the following MQTT
message:
{
"e": [
{ "n": "temperature", "u": "Cel", "t": 1234, "v":22.5 },
{ "n": "light", "u": "lm", "t": 1235, "v":135 },
{ "n": "acidity", "u": "pH", "t": 1235, "v":7 }
]
}
{"temperature": [{"v":22.5}]}
Using the same MQTT message, given a slightly more complicated rule such as:
{"temperature":22.5}
{
"a": {"b":"c"},
"arr":[1,2,3,4]
}
147
AWS IoT Developer Guide
Troubleshooting a Rule
[1,2,3,4]
Encode Function
Encodes the payload, which potentially might be non-JSON data, into its string representation based on the
specified encoding scheme.
Troubleshooting a Rule
If you are having an issue with your rules, you should enable CloudWatch Logs. By analyzing your
logs, you can determine whether the issue is authorization or whether, for example, a WHERE clause
condition did not match. For more information about using Amazon CloudWatch Logs, see Setting Up
CloudWatchLogs.
Deleting a Rule
When you are finished with a rule, you can delete it.
148
AWS IoT Developer Guide
CloudWatch Alarm Action
Note
The AWS IoT rules engine does not currently retry delivery for messages that fail to be published
to another service.
roleArn
The value of the alarm state. Acceptable values are OK, ALARM, INSUFFICIENT_DATA.
Note
Ensure the role associated with the rule has a policy granting the cloudwatch:SetAlarmState
permission.
The following JSON example shows how to define a CloudWatch alarm action in an AWS IoT rule:
{
"rule": {
"sql": "SELECT * FROM 'some/topic'",
"ruleDisabled": false,
"actions": [{
"cloudwatchAlarm": {
"roleArn": "arn:aws:iam::123456789012:role/aws_iot_cw",
"alarmName": "IotAlarm",
"stateReason": "Temperature stabilized.",
"stateValue": "OK"
}
}]
}
}
149
AWS IoT Developer Guide
DynamoDB Action
roleArn
Note
Ensure the role associated with the rule has a policy granting the cloudwatch:PutMetricData
permission.
The following JSON example shows how to define a CloudWatch metric action in an AWS IoT rule:
{
"rule": {
"sql": "SELECT * FROM 'some/topic'",
"ruleDisabled": false,
"actions": [{
"cloudwatchMetric": {
"roleArn": "arn:aws:iam::123456789012:role/aws_iot_cw",
"metricNamespace": "IotNamespace",
"metricName": "IotMetric",
"metricValue": "1",
"metricUnit": "Count",
"metricTimestamp": "1456821314"
}
}]
}
}
DynamoDB Action
The dynamoDB action allows you to write all or part of an MQTT message to a DynamoDB table. When
creating a DynamoDB rule, you must specify the following information:
hashKeyType
The data type of the hash key (also called the partition key). Valid values are: "STRING" or "NUMBER".
hashKeyField
The name of the hash key (also called the partition key).
150
AWS IoT Developer Guide
DynamoDB Action
hashKeyValue
Optional. The data type of the range key (also called the sort key). Valid values are: "STRING" or
"NUMBER".
rangeKeyField
Optional. The name of the range key (also called the sort key).
rangeKeyValue
Optional. The type of operation to be performed. This follows the substitution template, so it can be
${operation}, but the substitution must result in one of the following: INSERT, UPDATE, or DELETE.
payloadField
Optional. The name of the field where the payload will be written. If this value is omitted, the payload is
written to payload field.
table
The IAM role that allows access to the DynamoDB table. At a minimum, the role must allow the
dynamoDB:PutItem IAM action.
The data written to the DynamoDB table is the result from the SQL statement of the rule. The
hashKeyValue and rangeKeyValue fields are usually composed of expressions (for example, "${topic()}" or
"${timestamp()}").
Note
Non-JSON data is written to DynamoDB as binary data. The DynamoDB console will display the
data as Base64-encoded text.
Ensure the role associated with the rule has a policy granting the dynamodb:PutItem permission.
The following JSON example shows how to define a dynamoDB action in an AWS IoT rule:
{
"rule": {
"ruleDisabled": false,
"sql": "SELECT * AS message FROM 'some/topic'",
"description": "A test Dynamo DB rule",
"actions": [{
"dynamoDB": {
"hashKeyField": "key",
"roleArn": "arn:aws:iam::123456789012:role/aws_iot_dynamoDB",
"tableName": "my_ddb_table",
"hashKeyValue": "${topic()}",
"rangeKeyValue": "${timestamp()}",
"rangeKeyField": "timestamp"
}
}]
}
}
151
AWS IoT Developer Guide
DynamoDBv2 Action
For more information, see the Amazon DynamoDB Getting Started Guide.
DynamoDBv2 Action
The dynamoDBv2 action allows you to write all or part of an MQTT message to a DynamoDB table. Each
attribute in the payload is written to a separate column in the DynamoDB database. When creating a
DynamoDB rule, you must specify the following information:
roleARN
The IAM role that allows access to the DynamoDB table. At a minimum, the role must allow the
dynamoDB:PutItem IAM action.
tableName
Note
The MQTT message payload must contain a root-level key that matches the table's primary
partition key and a root-level key that matches the table's primary sort key, if one is defined.
The data written to the DynamoDB table is the result from the SQL statement of the rule.
Note
Ensure the role associated with the rule has a policy granting the dynamodb:PutItem permission.
The following JSON example shows how to define a dynamoDB action in an AWS IoT rule:
{
"rule": {
"ruleDisabled": false,
"sql": "SELECT * AS message FROM 'some/topic'",
"description": "A test DynamoDBv2 rule",
"actions": [{
"dynamoDBv2": {
"roleArn": "arn:aws:iam::123456789012:role/aws_iot_dynamoDBv2",
"putItem": {
"tableName": "my_ddb_table"
}
}
}]
}
}
For more information, see the Amazon DynamoDB Getting Started Guide.
Amazon ES Action
The elasticsearch action allows you to write data from MQTT messages to an Amazon Elasticsearch
Service domain. Data in Amazon ES can then be queried and visualized by using tools like Kibana. When
you create an AWS IoT rule with an elasticsearch action, you must specify the following information:
endpoint
152
AWS IoT Developer Guide
Firehose Action
type
Note
Ensure the role associated with the rule has a policy granting the es:ESHttpPut permission.
The following JSON example shows how to define an elasticsearch action in an AWS IoT rule:
{
"rule":{
"sql":"SELECT *, timestamp() as timestamp FROM 'iot/test'",
"ruleDisabled":false,
"actions":[
{
"elasticsearch":{
"roleArn":"arn:aws:iam::123456789012:role/aws_iot_es",
"endpoint":"https://my-endpoint",
"index":"my-index",
"type":"my-type",
"id":"${newuuid()}"
}
}
]
}
}
Firehose Action
A firehose action sends data from an MQTT message that triggered the rule to an Firehose stream. When
creating a rule with a firehose action, you must specify the following information:
deliveryStreamName
A character separator that will be used to separate records written to the firehose stream. Valid values
are: '\n' (newline), '\t' (tab), '\r\n' (Windows newline), ',' (comma).
Note
Make sure the role associated with the rule has a policy granting the firehose:PutRecord
permission.
The following JSON example shows how to create an AWS IoT rule with a firehose action:
{
"rule": {
"sql": "SELECT * FROM 'some/topic'",
153
AWS IoT Developer Guide
Kinesis Action
"ruleDisabled": false,
"actions": [{
"firehose": {
"roleArn": "arn:aws:iam::123456789012:role/aws_iot_firehose",
"deliveryStreamName": "my_firehose_stream"
}
}]
}
}
Kinesis Action
The kinesis action allows you to write data from MQTT messages into an Amazon Kinesis stream. When
creating an AWS IoT rule with a kinesis action, you must specify the following information:
stream
The partition key used to determine to which shard the data is written. The partition key is usually
composed of an expression (for example, "${topic()}" or "${timestamp()}").
Note
Ensure that the policy associated with the rule has the kinesis:PutRecord permission.
The following JSON example shows how to define a kinesis action in an AWS IoT rule:
{
"rule": {
"sql": "SELECT * FROM 'some/topic'",
"ruleDisabled": false,
"actions": [{
"kinesis": {
"roleArn": "arn:aws:iam::123456789012:role/aws_iot_kinesis",
"streamName": "my_kinesis_stream",
"partitionKey": "${topic()}"
}
}],
}
}
Lambda Action
A lambda action calls a Lambda function, passing in the MQTT message that triggered the rule. In order
for AWS IoT to call a Lambda function, you must configure a policy granting the lambda:InvokeFunction
permission to AWS IoT. Lambda functions use resource-based policies, so you must attach the
policy to the Lambda function itself. Use the following CLI command to attach a policy granting
lambda:InvokeFunction permission:
154
AWS IoT Developer Guide
Republish Action
--function-name
Name of the Lambda function whose resource policy you are updating by adding a new permission.
--region
The principal who is getting the permission. This should be iot.amazonaws.com to allow AWS IoT
permission to call a Lambda function.
--source-arn
The ARN of the rule. You can use the get-topic-rule CLI command to get the ARN of a rule.
--source-account
The Lambda action you want to allow in this statement. In this case, we want to allow AWS IoT to
invoke a Lambda function, so we specify lambda:InvokeFunction.
Note
If you add a permission for a AWS IoT principal without providing the source ARN, any AWS
account that creates a rule with your Lambda action can trigger rules to invoke your Lambda
function from AWS IoT
When creating a rule with a lambda action, you must specify the Lambda function to invoke when the rule is
triggered.
The following JSON example shows a rule that calls a Lambda function:
{
"rule": {
"sql": "SELECT * FROM 'some/topic'",
"ruleDisabled": false,
"actions": [{
"lambda": {
"functionArn": "arn:aws:lambda:us-
east-1:123456789012:function:myLambdaFunction"
}
}]
}
}
Republish Action
The republish action allows you to republish the message that triggered the role to another MQTT topic.
When creating a rule with a republish action, you must specify the following information:
155
AWS IoT Developer Guide
S3 Action
topic
Note
Make sure the role associated with the rule has a policy granting the iot:Publish permission.
{
"rule": {
"sql": "SELECT * FROM 'some/topic'",
"ruleDisabled": false,
"actions": [{
"republish": {
"topic": "another/topic",
"roleArn": "arn:aws:iam::123456789012:role/aws_iot_republish"
}
}]
}
}
S3 Action
A s3 action writes the data from the MQTT message that triggered the rule to an Amazon S3 bucket. When
creating an AWS IoT rule with an s3 action, you must specify the following information:
bucket
The Amazon S3 canned ACL that controls access to the object identified by the object key. For more
information, see S3 Canned ACLs.
key
The path to the file where the data is written. For example, if the value of this argument is "${topic()}/
${timestamp()}", the topic the message was sent to is "this/is/my/topic,", and the current timestamp
is 1460685389 the data will be written to a file called "1460685389" in the "this/is/my/topic" folder on
Amazon S3.
Note
Using a static key will result in a single file in Amazon S3 being overwritten for each invocation
of the rule. More common use cases are to use the message timestamp or another unique
message identifier, so that a new file will be saved in Amazon S3 for each message received.
roleArn
Note
Make sure the role associated with the rule has a policy granting the s3:PutObject permission.
The following JSON example shows how to define an s3 action in an AWS IoT rule:
{
"rule": {
156
AWS IoT Developer Guide
SNS Action
SNS Action
A sns action sends the data from the MQTT message that triggered the rule as an SNS push notification.
When creating a rule with an sns action, you must specify the following information:
messageFormat
The message format. Accepted values are "JSON" and "RAW". The default value of the attribute is
"RAW". SNS uses this setting to determine if the payload should be parsed and relevant platform-
specific parts of the payload should be extracted.
roleArn
The SNS topic or individual device to which the push notification will be sent.
Note
Make sure the policy associated with the rule has the sns:Publish permission.
The following JSON example shows how to define an sns action in an AWS IoT rule:
{
"rule": {
"sql": "SELECT * FROM 'some/topic'",
"ruleDisabled": false,
"actions": [{
"sns": {
"targetArn": "arn:aws:sns:us-east-1:123456789012:my_sns_topic",
"roleArn": "arn:aws:iam::123456789012:role/aws_iot_sns"
}
}]
}
}
SQS Action
A sqs action sends data from the MQTT message that triggered the rule to an SQS queue. When creating a
rule with an sqs action, you must specify the following information:
queueUrl
157
AWS IoT Developer Guide
AWS IoT SQL Reference
useBase64
Set to true if you want the MQTT message data to be Base64-encoded before writing to the SQS
queue; otherwise, set to false.
roleArn
Note
Make sure the role associated with the rule has a policy granting the sqs:SendMessage permission.
The following JSON example shows how to create an AWS IoT rule with an sqs action:
{
"rule": {
"sql": "SELECT * FROM 'some/topic'",
"ruleDisabled": false,
"actions": [{
"sqs": {
"queueUrl": "https://sqs.us-east-1.amazonaws.com/123456789012/
my_sqs_queue",
"roleArn": "arn:aws:iam::123456789012:role/aws_iot_sqs",
"useBase64": false
}
}]
}
}
SELECT
Required. Extracts information from the incoming payload and performs transformations.
FROM
Required. The MQTT topic filter from which the rule will receive messages.
WHERE
Optional. Adds conditional logic that determines if a rule is evaluated and its actions are executed.
An example MQTT message (also called an incoming payload) looks like this:
{
"color":"red",
"temperature":100
158
AWS IoT Developer Guide
Data Types
If this message is published on the 'a/b' topic, the rule is triggered and the SQL statement is evaluated.
The SQL statement extracts the value of the rgb property if the "temperature" property is greater than
50. The WHERE clause specifies the condition temperature > 50. The AS keyword renames the "color"
property to "rgb". The result (also called an outgoing payload) looks like this:
{
"rgb":"red"
}
This data is then forwarded to the rule's action, which sends the data for more processing. For more
information about rule actions, see AWS IoT Rule Actions (p. 148).
Data Types
The AWS IoT rules engine supports all JSON data types.
Type Meaning
{"foo":null, "bar":undefined}
159
AWS IoT Developer Guide
Data Types
Type Meaning
is serialized to JSON as:
{"foo":null}
Conversions
The following table lists the results when a value of one type is converted to another type (when a value of
the incorrect type is given to a function). For example, if the absolute value function "abs" (which expects
an Int or Decimal) is given a String, it attempts to convert the String to a Decimal, following these rules.
In this case, 'abs("-5.123")' is treated as 'abs(-5.123)'.
Note
There are no attempted conversions to Array, Object, Null, or Undefined.
To Decimal
Array Undefined.
Object Undefined.
Null Null.
Undefined Undefined.
To Int
160
AWS IoT Developer Guide
Data Types
Array Undefined.
Object Undefined.
Null Null.
Undefined Undefined.
To Boolean
Array Undefined.
Object Undefined.
Null Undefined.
Undefined Undefined.
To String
161
AWS IoT Developer Guide
Operators
Null Undefined.
Undefined Undefined.
Operators
The following operators can be used in SELECT, FROM, and WHERE clauses.
AND operator
Returns a Boolean result. Performs a logical AND operation. Returns true if left and right operands are true;
returns false otherwise. Boolean operands or case-insensitive "true" or "false" string operands are required.
AND Operator
Boolean Boolean Boolean. True if both operands are true; otherwise, false.
String/Boolean String/Boolean If all strings are "true" or "false" (case-insensitive), they are
converted to Boolean and processed normally as boolean
AND boolean.
OR operator
Returns a Boolean result. Performs a logical OR operation. Returns true if either the left or the right
operands are true; returns false otherwise. Boolean operands or case-insensitive "true" or "false" string
operands are required.
OR Operator
String/Boolean String/Boolean If all strings are "true" or "false" (case-insensitive), they are
converted to Booleans and processed normally as boolean
OR boolean.
162
AWS IoT Developer Guide
Operators
NOT operator
Returns a Boolean result. Performs a logical NOT operation. Returns true if the operand is false; returns
true otherwise. A boolean operand or case-insensitive "true" or "false" string operand is required.
NOT Operator
Operand Output
> operator
Returns a Boolean result. Returns true if the left operand is greater than the right operand. Both operands
are converted to a Decimal, and then compared.
> Operator
Left Operand Right Operand Output
Int/Decimal Int/Decimal Boolean. True if the left operand is greater than the right
operand; otherwise, false.
>= operator
Returns a Boolean result. Returns true if the left operand is greater than or equal to the right operand. Both
operands are converted to a Decimal, and then compared.
>= Operator
Left Operand Right Operand Output
Int/Decimal Int/Decimal Boolean. True if the left operand is greater than or equal to the
right operand; otherwise, false.
163
AWS IoT Developer Guide
Operators
< operator
Returns a Boolean result. Returns true if the left operand is less than the right operand. Both operands are
converted to a Decimal, and then compared.
< Operator
Left Operand Right Operand Output
Int/Decimal Int/Decimal Boolean. True if the left operand is less than the right
operand; otherwise, false.
<= operator
Returns a Boolean result. Returns true if the left operand is less than or equal to the right operand. Both
operands are converted to a Decimal, and then compared.
>= Operator
Left Operand Right Operand Output
Int/Decimal Int/Decimal Boolean. True if the left operand is less than or equal to the
right operand; otherwise, false.
<> operator
Returns a Boolean result. Returns true if both left and right operands are not equal; returns false otherwise.
<> Operator
Left Operand Right Operand Output
Int Int True if left operand is not equal to right operand; otherwise,
false.
Decimal Decimal True if left operand is not equal to right operand; otherwise
false.Int is converted to Decimal before being compared.
String String True if left operand is not equal to right operand; otherwise,
false.
164
AWS IoT Developer Guide
Operators
Array Array True if the items in each operand are not equal and not in the
same order; otherwise, false
Object Object True if the keys and values of each operand are not equal;
otherwise, false. The order of keys/values is unimportant.
= operator
Returns a Boolean result. Returns true if both left and right operands are equal; returns false otherwise.
= Operator
Int Int True if left operand is equal to right operand; otherwise, false.
String String True if left operand is equal to right operand; otherwise, false.
Array Array True if the items in each operand are equal and in the same
order; otherwise, false.
Object Object True if the keys and values of each operand are equal;
otherwise, false. The order of keys/values is unimportant.
+ operator
The "+" is an overloaded operator. It can be used for string concatenation or addition.
+ Operator
String Any Value Converts the right operand to a string and concatenates it to
the end of the left operand.
165
AWS IoT Developer Guide
Operators
Any Value String Converts the left operand to a string and concatenates the
right operand to the end of the converted left operand.
- operator
Subtracts the right operand from the left operand.
- Operator
Int Int Int value. Subtracts right operand from left operand.
Int/Decimal Int/Decimal Decimal value. Subtracts right operand from left operand.
* operator
Multiplies the left operand by the right operand.
* Operator
Int Int Int value. Multiplies the left operand by the right operand.
Int/Decimal Int/Decimal Decimal value. Multiplies the left operand by the right
operand.
/ operator
Divides the left operand by the right operand.
166
AWS IoT Developer Guide
Functions
/ Operator
Int Int Int value. Divides the left operand by the right operand.
Int/Decimal Int/Decimal Decimal value. Divides the left operand by the right operand.
% operator
Returns the remainder from dividing the left operand by the right operand.
% Operator
Int Int Int value. Returns the remainder from dividing the left
operand by the right operand.
Functions
You can use the following built-in functions in the SELECT or WHERE clauses of your SQL expressions.
abs(Decimal)
Returns the absolute value of a number. Supported by SQL version 2015-10-8 and later.
Boolean Undefined.
Array Undefined.
167
AWS IoT Developer Guide
Functions
Object Undefined.
Null Undefined.
Undefined Undefined.
accountid()
Returns the ID of the account that owns this rule as a String. Supported by SQL version 2015-10-8 and
later.
Example:
accountid() = "123456789012"
acos(Decimal)
Returns the inverse cosine of a number in radians. Decimal arguments are rounded to double precision
before function application. Supported by SQL version 2015-10-8 and later.
Boolean Undefined.
Array Undefined.
Object Undefined.
Null Undefined.
Undefined Undefined.
asin(Decimal)
Returns the inverse sine of a number in radians. Decimal arguments are rounded to double precision
before function application. Supported by SQL version 2015-10-8 and later.
168
AWS IoT Developer Guide
Functions
Boolean Undefined.
Array Undefined.
Object Undefined.
Null Undefined.
Undefined Undefined.
atan(Decimal)
Returns the inverse tangent of a number in radians. Decimal arguments are rounded to double precision
before function application. Supported by SQL version 2015-10-8 and later.
Boolean Undefined.
Array Undefined.
Object Undefined.
Null Undefined.
Undefined Undefined.
atan2(Decimal, Decimal)
Returns the angle, in radians, between the positive x-axis and the (x, y) point defined in the two arguments.
The angle is positive for counter-clockwise angles (upper half-plane, y > 0), and negative for clockwise
angles (lower half-plane, y < 0). Decimal arguments are rounded to double precision before function
application. Supported by SQL version 2015-10-8 and later.
169
AWS IoT Developer Guide
Functions
bitand(Int, Int)
Performs a bitwise AND on the bit representations of the two Int(-converted) arguments. Supported by
SQL version 2015-10-8 and later.
Example: bitand(13, 5) = 5
bitor(Int, Int)
Performs a bitwise OR of the bit representations of the two arguments. Supported by SQL version
2015-10-8 and later.
Example: bitor(8, 5) = 13
170
AWS IoT Developer Guide
Functions
bitxor(Int, Int)
Performs a bitwise XOR on the bit representations of the two Int(-converted) arguments. Supported by
SQL version 2015-10-8 and later.
Example:bitor(13, 5) = 8
bitnot(Int)
Performs a bitwise NOT on the bit representations of the Int(-converted) argument. Supported by SQL
version 2015-10-8 and later.
Example: bitnot(13) = 2
cast()
Converts a value from one data type to another. Cast behaves mostly like the standard conversions, with
the addition of the ability to cast numbers to/from Booleans. If we cannot determine how to cast one type
to another, the result is Undefined. Supported by SQL version 2015-10-8 and later. Format: cast(value as
type).
Example:
The following keywords may appear after "as" when calling cast:
Keyword Result
171
AWS IoT Developer Guide
Functions
Keyword Result
Casting rules:
Cast to Decimal
Array Undefined.
Object Undefined.
Null Undefined.
Undefined Undefined.
Cast to Int
172
AWS IoT Developer Guide
Functions
Array Undefined.
Object Undefined.
Null Undefined.
Undefined Undefined.
Cast to Boolean
Argument Type Result
Array Undefined.
Object Undefined.
Null Undefined.
Undefined Undefined.
Cast to String
Argument Type Result
Null Undefined.
Undefined Undefined.
173
AWS IoT Developer Guide
Functions
ceil(Decimal)
Rounds the given Decimal up to the nearest Int. Supported by SQL version 2015-10-8 and later.
Examples:
ceil(1.2) = 2
ceil(11.2) = -1
chr(String)
Returns the ASCII character that corresponds to the given Int argument. Supported by SQL version
2015-10-8 and later.
Examples:
chr(65) = "A".
chr(49) = "1".
Boolean Undefined.
Array Undefined.
Object Undefined.
Null Undefined.
174
AWS IoT Developer Guide
Functions
clientid()
Returns the ID of the MQTT client sending the message, or Undefined if the message wasn't sent over
MQTT. Supported by SQL version 2015-10-8 and later.
Example:
clientid() = "123456789012"
concat()
Concatenates arrays or strings. This function accepts any number of arguments and returns a String or an
Array. Supported by SQL version 2015-10-8 and later.
Examples:
concat() = Undefined.
concat(1) = "1".
concat("he","is","man") = "heisman"
0 Undefined.
cos(Decimal)
Returns the cosine of a number in radians. Decimal arguments are rounded to double precision before
function application. Supported by SQL version 2015-10-8 and later.
Example:
cos(0) = 1.
175
AWS IoT Developer Guide
Functions
Boolean Undefined.
Array Undefined.
Object Undefined.
Null Undefined.
Undefined Undefined.
cosh(Decimal)
Returns the hyperbolic cosine of a number in radians. Decimal arguments are rounded to double precision
before function application. Supported by SQL version 2015-10-8 and later.
Boolean Undefined.
Array Undefined.
Object Undefined.
Null Undefined.
Undefined Undefined.
encode(value, encodingScheme)
Use the encode function to encode the payload, which potentially might be non-JSON data, into its string
representation based on the encoding scheme. Supported by SQL version 2016-03-23 and later.
176
AWS IoT Developer Guide
Functions
value
Any of the valid expressions, as defined in AWS IoT SQL Reference (p. 158). In addition, you can
specify * to encode the entire payload, regardless of whether it's in JSON format. If you supply an
expression, the result of the evaluation will first be converted to a string before it is encoded.
encodingScheme
A literal string representing the encoding scheme you want to use. Currently, only 'base64' is
supported.
endswith(String, String)
Returns a Boolean indicating whether the first String argument ends with the second String argument. If
either argument is Null or Undefined, the result is Undefined. Supported by SQL version 2015-10-8 and
later.
exp(Decimal)
Returns e raised to the Decimal argument. Decimal arguments are rounded to double precision before
function application. Supported by SQL version 2015-10-8 and later.
Example: exp(1) = e.
get
Extracts a value from a collection-like type (Array, String, Object). No conversion will be applied to the first
argument. Conversion applies as documented in the table to the second argument. Supported by SQL
version 2015-10-8 and later.
Examples:
177
AWS IoT Developer Guide
Functions
get("abc", 1) = "b"
get_thing_shadow(thingName, roleARN)
Returns the shadow of the specified thing. Supported by SQL version 2016-03-23 and later.
thingName
String: The name of the thing whose shadow you want to retrieve.
roleArn
Example:
WHERE get_thing_shadow("MyThing","arn:aws:iam::123456789012:role/
AllowsThingShadowAccess") .state.reported.alarm = 'ON'
Hashing Functions
We provide the following hashing functions:
md2
md5
sha1
sha224
sha256
sha384
sha512
178
AWS IoT Developer Guide
Functions
All hash functions expect one string argument. The result is the hashed value of that string. Standard string
conversions apply to non-string arguments. All hash functions are supported by SQL version 2015-10-8 and
later.
Examples:
md2("hello") = "a9046c73e00331af68917d3804f70655"
md5("hello") = "5d41402abc4b2a76b9719d911017c592"
hsin(Decimal)
Returns the hyperbolic sine of a number. Decimal values are rounded to double precision before function
application. The result is a Decimal value of double precision. Supported by SQL version 2015-10-8 and
later.
Boolean Undefined.
Array Undefined.
Object Undefined.
Null Undefined.
Undefined Undefined.
htan(Decimal)
Returns the hyperbolic tangent of a number in radians. Decimal values are rounded to double precision
before function application. Supported by SQL version 2015-10-8 and later.
Boolean Undefined.
179
AWS IoT Developer Guide
Functions
Array Undefined.
Object Undefined.
Null Undefined.
Undefined Undefined.
indexof(String, String)
Returns the first index (0-based) of the second argument as a substring in the first argument. Both
arguments are expected as strings. Arguments that are not strings are subjected to standard string
conversion rules. This function does not apply to arrays, only to strings. Supported by SQL version
2015-10-8 and later.
Examples:
indexof("abcd", "bc") = 1
isNull()
Returns whether the argument is the Null value. Supported by SQL version 2015-10-8 and later.
Examples:
isNull(5) = false.
isNull(Null) = true.
Int false
Decimal false
Boolean false
String false
Array false
Object false
Null true
Undefined false
isUndefined()
Returns whether the argument is Undefined. Supported by SQL version 2015-10-8 and later.
180
AWS IoT Developer Guide
Functions
Examples:
isUndefined(5) = false.
isNull(floor([1,2,3]))) = true.
Int false
Decimal false
Boolean false
String false
Array false
Object false
Null false
Undefined true
length(String)
Returns the number of characters in the provided string. Standard conversion rules apply to non-String
arguments. Supported by SQL version 2015-10-8 and later.
Examples:
length("hi") = 2
length(false) = 5
ln(Decimal)
Returns the natural logarithm of the argument. Decimal arguments are rounded to double precision before
function application. Supported by SQL version 2015-10-8 and later.
Example: ln(e) = 1.
Boolean Undefined.
Array Undefined.
181
AWS IoT Developer Guide
Functions
Object Undefined.
Null Undefined.
Undefined Undefined.
log(Decimal)
Returns the base 10 logarithm of the argument. Decimal arguments are rounded to double precision before
function application. Supported by SQL version 2015-10-8 and later.
Boolean Undefined.
Array Undefined.
Object Undefined.
Null Undefined.
Undefined Undefined.
lower(String)
Returns the lowercase version of the given String. Non-string arguments are converted to Strings using
the standard conversion rules. Supported by SQL version 2015-10-8 and later.
Examples:
lower("HELLO") = "hello".
lower(["HELLO"]) = "[\"hello\"]".
lpad(String, Int)
Returns the String argument, padded on the left side with the number of spaces specified by the second
argument. The Int argument must be between 0 and 1000. If the provided value is outside of this valid
range, the argument will be set to the nearest valid value (0 or 1000). Supported by SQL version 2015-10-8
and later.
Examples:
182
AWS IoT Developer Guide
Functions
ltrim(String)
Removes all leading whitespace (tabs and spaces) from the provided String. Supported by SQL version
2015-10-8 and later.
Example:
Null Undefined.
Undefined Undefined.
183
AWS IoT Developer Guide
Functions
machinelearning_predict(modelId)
Use the machinelearning_predict function to make predictions using the data from an MQTT message
based on an Amazon Machine Learning (Amazon ML) model. Supported by SQL version 2015-10-8 and
later. The arguments for the machinelearning_predict function are:
modelId
The ID of the model against which to run the prediction. The real-time endpoint of the model must be
enabled.
roleArn
The IAM role that has a policy with machinelearning:Predict and machinelearning:GetMLModel
permissions and allows access to the model against which the prediction is run.
record
The data to be passed into the Amazon ML Predict API. This should be represented as a single layer
JSON object. If the record is a multi-level JSON object, the record will be flattened by serializing its
values. For example, the following JSON:
would become:
predictedLabel
The algorithm used by Amazon ML to make predictions. The value must be SGD.
predictedScores
mod(Decimal, Decimal)
Returns the remainder of the division of the first argument by the second argument. Supported by SQL
version 2015-10-8 and later. You can also use "%" as an infix operator for the same modulo functionality.
Supported by SQL version 2015-10-8 and later.
Example: mod(8, 3) = 2.
184
AWS IoT Developer Guide
Functions
nanvl(AnyValue, AnyValue)
Returns the first argument if it is a valid Decimal; otherwise, the second argument is returned. Supported by
SQL version 2015-10-8 and later.
Example: Nanvl(8, 3) = 8.
newuuid()
Returns a random 16-byte UUID. Supported by SQL version 2015-10-8 and later.
numbytes(String)
Returns the number of bytes in the UTF-8 encoding of the provided string. Standard conversion rules apply
to non-String arguments. Supported by SQL version 2015-10-8 and later.
Examples:
numbytes("hi") = 2
numbytes("") = 3
principal()
Returns the X.509 certificate fingerprint or thing name, depending on which endpoint, MQTT or HTTP,
received the request. Supported by SQL version 2015-10-8 and later.
Example:
principal() = "ba67293af50bf2506f5f93469686da660c7c844e7b3950bfb16813e0d31e9373"
185
AWS IoT Developer Guide
Functions
power(Decimal, Decimal)
Returns the first argument raised to the second argument. Decimal arguments are rounded to double
precision before function application. Supported by SQL version 2015-10-8 and later. Supported by SQL
version 2015-10-8 and later.
rand()
Returns a pseudorandom, uniformly distributed double between 0.0 and 1.0. Supported by SQL version
2015-10-8 and later.
Example:
rand() = 0.8231909191640703
regexp_matches(String, String)
Returns whether the first argument contains a match for the second argument (regex).
Example:
First argument:
186
AWS IoT Developer Guide
Functions
Null Undefined.
Undefined Undefined.
Second argument:
Must be a valid regex expression. Non-string types are converted to String using the standard conversion
rules. Depending on the type, the resultant string may or may not be a valid regular expression. If the
(converted) argument is not valid regex, the result is Undefined.
Third argument:
Must be a valid regex replacement string. (Can reference capture groups.) Non-string types will be
converted to String using the standard conversion rules. If the (converted) argument is not a valid regex
replacement string, the result is Undefined.
Example:
First argument:
Null Undefined.
Undefined Undefined.
Second argument:
Must be a valid regex expression. Non-string types are converted to Strings using the standard conversion
rules. Depending on the type, the resultant string may or may not be a valid regular expression. If the
(converted) argument is not a valid regex expression, the result is Undefined.
Third argument:
187
AWS IoT Developer Guide
Functions
Must be a valid regex replacement string. (Can reference capture groups.) Non-string types will be
converted to Strings using the standard conversion rules. If the (converted) argument is not a valid regex
replacement string, the result is Undefined.
regexp_substr(String, String)
Finds the first match of the 2nd parameter (regex) in the first parameter. Reference capture groups with "$".
Supported by SQL version 2015-10-8 and later.
Example:
First argument:
Null Undefined.
Undefined Undefined.
Second argument:
Must be a valid regex expression. Non-string types are converted to Strings using the standard conversion
rules. Depending on the type, the resultant string may or may not be a valid regular expression. If the
(converted) argument is not a valid regex expression, the result is Undefined.
Third argument:
Must be a valid regex replacement string. (Can reference capture groups.) Non-string types will be
converted to String using the standard conversion rules. If the argument is not a valid regex replacement
string, the result is Undefined.
rpad(String, Int)
Returns the string argument, padded on the right side with the number of spaces specified in the second
argument. The Int argument must be between 0 and 1000. If the provided value is outside of this valid
range, the argument will be set to the nearest valid value (0 or 1000). Supported by SQL version 2015-10-8
and later.
Examples:
188
AWS IoT Developer Guide
Functions
189
AWS IoT Developer Guide
Functions
round(Decimal)
Rounds the given Decimal to the nearest Int. If the Decimal is equidistant from two Int values (for
example, 0.5), the Decimal is rounded up. Supported by SQL version 2015-10-8 and later.
Example: Round(1.2) = 1.
190
AWS IoT Developer Guide
Functions
Round(1.5) = 2.
Round(1.7) = 2.
Round(-1.1) = -1.
Round(-1.5) = -2.
rtrim(String)
Removes all trailing whitespace (tabs and spaces) from the provided String. Supported by SQL version
2015-10-8 and later.
Examples:
Null Undefined.
Undefined Undefined
sign(Decimal)
Returns the sign of the given number. When the sign of the argument is positive, 1 is returned. When the
sign of the argument is negative, -1 is returned. If the argument is 0, 0 is returned. Supported by SQL
version 2015-10-8 and later.
Examples:
sign(-7) = -1.
sign(0) = 0.
191
AWS IoT Developer Guide
Functions
sign(13) = 1.
String Int, the sign of the Decimal value. The string is converted
to a Decimal value, and the sign of the Decimal value is
returned. If the String cannot be converted to a Decimal,
the result is Undefined. Supported by SQL version
2015-10-8 and later.
sin(Decimal)
Returns the sine of a number in radians. Decimal arguments are rounded to double precision before
function application. Supported by SQL version 2015-10-8 and later.
Boolean Undefined.
Array Undefined.
Object Undefined.
Null Undefined.
Undefined Undefined.
If the arguments provided are not (String, Int), or (String, Int, Int>), the standard conversions will be
applied to the arguments to attempt to convert them into the correct types. If the types cannot be converted,
the result of the function is Undefined. Supported by SQL version 2015-10-8 and later.
192
AWS IoT Developer Guide
Functions
Examples:
substring("012345", 0) = "012345".
substring("012345", 2) = "2345".
substring(123, 2) = "3".
substring("012345", 1, 3) = "12".
substring("012345", 3, 1) = "".
sqrt(Decimal)
Returns the square root of a number. Decimal arguments are rounded to double precision before function
application. Supported by SQL version 2015-10-8 and later.
Boolean Undefined.
Array Undefined.
Object Undefined.
Null Undefined.
Undefined Undefined.
startswith(String, String)
Returns Boolean, whether the first string argument starts with the second string argument. If either
argument is Null or Undefined, the result is Undefined. Supported by SQL version 2015-10-8 and later.
Example:
startswith("ranger","ran") = true
193
AWS IoT Developer Guide
Functions
timestamp()
Returns the current timestamp in milliseconds from 00:00:00 Coordinated Universal Time (UTC), Thursday,
1 January 1970, as observed by the AWS IoT rules engine. Supported by SQL version 2015-10-8 and later.
topic(Decimal)
Returns the topic to which the message that triggered the rule was sent. If no parameter is specified, the
entire topic is returned. The Decimal parameter is used to specify a specific, one-based topic segment. For
the topic foo/bar/baz, topic(1) will return foo, topic(2) will return bar, and so on. Supported by SQL version
2015-10-8 and later.
Examples:
topic() = "things/myThings/thingOne"
topic(1) = "things"
tan(Decimal)
Returns the tangent of a number in radians. Decimal values are rounded to double precision before
function application. Supported by SQL version 2015-10-8 and later.
Boolean Undefined.
Array Undefined.
Object Undefined.
Null Undefined.
Undefined Undefined.
194
AWS IoT Developer Guide
Functions
traceid()
Returns the trace ID (UUID) of the MQTT message, or Undefined if the message wasn't sent over MQTT.
Supported by SQL version 2015-10-8 and later.
Example:
traceid() = "12345678-1234-1234-1234-123456789012"
trunc(Decimal, Int)
Truncates the first argument to the number of Decimal places specified by the second argument. If the
second argument is less than zero, it will be set to zero. If the second argument is greater than 34, it will be
set to 34. Trailing zeroes are stripped from the result. Supported by SQL version 2015-10-8 and later.
Examples:
trunc(2.3, 0) = 2.
trunc(2.3123, 2 = 2.31.
trunc(2.888, 2) = 2.88.
(2.00, 5) = 2.
trim(String)
Removes all leading and trailing whitespace from the provided String. Supported by SQL version
2015-10-8 and later.
Example:
Int The String representation of the Int with all leading and
trailing whitespace removed.
195
AWS IoT Developer Guide
SELECT Clause
Null Undefined.
Undefined Undefined.
upper(String)
Returns the uppercase version of the given String. Non-String arguments are converted to String using
the standard conversion rules. Supported by SQL version 2015-10-8 and later.
Examples:
upper("hello") = "HELLO"
upper(["hello"]) = "[\"HELLO\"]"
SELECT Clause
The AWS IoT SELECT clause is essentially the same as the ANSI SQL SELECT clause, with some minor
differences.
You can use the SELECT clause to extract information from incoming MQTT messages. SELECT * can be
used to retrieve the entire incoming message payload. For example:
If the payload is a JSON object, you can reference keys in the object. Your outgoing payload will contain
the key-value pair. For example:
You can select multiple items by separating them with a comma. For example:
196
AWS IoT Developer Guide
SELECT Clause
You can select multiple items including '*' to add items to the incoming payload. For example:
You can use the "VALUE" keyword to produce outgoing payloads that are not JSON objects. You may only
select one item. For example:
You can use '.' syntax to drill into nested JSON objects in the incoming payload. For example:
You can use functions (see Functions (p. 167)) to transform the incoming payload. Parentheses can be
used for grouping. For example:
1. The SQL statement and templates must not refer to other JSON names, other than *.
2. The SELECT statement must have only a single selected item, * (i.e. SELECT * FROM 'a/b').
The following SELECT can not be used with binary payloads because it referrs to device_type in the
WHERE clause.
The following SELECT can not be used with binary payloads because it violates rule #2.
197
AWS IoT Developer Guide
FROM Clause
The following SELECT can be used with binary payloads because it doesn't violate either rule #1 or #2.
The following AWS IoT rule can not be used with payloads because it violates rule #1.
{
"sql": "SELECT * FROM 'a/b'"
"actions": [{
"republish": {
"topic":"device/${device_id}"
}
}]
}
FROM Clause
The FROM clause subscribes your rule to a topic or topic filter. A topic filter allows you to subscribe to a
group of similar topics.
Example:
The rule is subscribed to 'a/b', so the incoming payload is passed to the rule, and the outgoing payload
(passed to the rule actions) is: {t: 50}. The rule is not subscribed to 'a/c', so the rule is not triggered for
the message published on 'a/c'.
You can use the # wildcard character to match any subpath in a topic filter:
Example:
The rule is subscribed to any topic beginning with 'a', so it is executed three times, sending outgoing
payloads of {t: 50} (for a/b), {t: 60} (for a/c), and {t: 70} (for a/e/f) to its actions. It is not
subscribed to 'b/x', so the rule will not be triggered for the {temperature: 80} message.
You can use the '+' character to match any one particular path element:
Example:
198
AWS IoT Developer Guide
WHERE Clause
The rule is subscribed to all topics with two path elements where the first element is 'a'. The rule is
executed for the messages sent to 'a/b' and 'a/c', but not 'a/e/f' or 'b/x'.
You can use functions and operators in the WHERE clause. In the WHERE clause, you cannot reference
any aliases created with the AS keyword in the SELECT. (The WHERE clause is evaluated first, to
determine if the SELECT clause is evaluated.)
WHERE Clause
The WHERE clause determines if a rule is evaluated for a message sent to an MQTT topic to which the
rule is subscribed. If the WHERE clause evaluates to true, the rule is evaluated; otherwise, the rule is not
evaluated.
Example:
SQL: SELECT color AS my_color FROM 'a/b' WHERE temperature > 50 AND color <> 'red'.
In this case, the rule would not be evaluated; there would be no outgoing payload; and rules actions would
not be triggered.
You can use functions and operators in the WHERE clause. However, you cannot reference any aliases
created with the AS keyword in the SELECT. (The WHERE clause is evaluated first, to determine if
SELECT is evaluated.)
Literals
You can directly specify literal objects in the SELECT and WHERE clauses of your rule SQL, which can be
useful for passing information.
Note
Literals are only available when using SQL Version 2016-03-23 or newer.
JSON object syntax is used (key-value pairs, comma-separated, where keys are strings and values are
JSON values, wrapped in curly brackets {}). For example:
You can also directly specify arrays in the SELECT and WHERE clauses of your rule SQL, which allows
you to group information. JSON syntax is used (wrap comma-separated items in square brackets [] to
create an array literal). For example:
199
AWS IoT Developer Guide
Case Statements
Case Statements
Case statements can be used for branching execution, like a switch statement, or if/else statements.
Syntax:
The expression v is evaluated and matched for equality against each t[i] expression. If a match is found,
the corresponding r[i] expression becomes the result of the case statement. If there is more than one
possible match, the first match is selected. If there are no matches, the else statement's re is used as
the result. If there is no match and no else statement, the result of the case statement is Undefined. For
example:
SQL statement: SELECT CASE color WHEN 'green' THEN 'go' WHEN 'yellow' THEN 'caution' WHEN
'red' THEN 'stop' ELSE 'you are not at a stop light' END as instructions FROM 'a/b'
Case statements require at least one WHEN clause. An ELSE clause is not required.
Note
If v is Undefined, the result of the case statement is Undefined.
JSON Extensions
You can use the following extensions to ANSI SQL syntax to make it easier to work with nested JSON
objects.
"." Operator
This operator accesses members in embedded JSON objects and functions identically to ANSI SQL and
JavaScript. For example:
* Operator
This functions in the same way as the * wildcard in ANSI SQL. It's used in the SELECT clause only and
creates a new JSON object containing the message data. If the message payload is not in JSON format, *
returns the entire message payload as raw bytes. For example:
{
"deviceid" : "iot123",
"temp" : 54.98,
"humidity" : 32.43,
200
AWS IoT Developer Guide
Substitution Templates
"coords" : {
"latitude" : 47.615694,
"longitude" : -122.3359976
}
}
{
"temp": 54.98,
"hashed_id": "e37f81fb397e595c4aeb5645b8cbbbd1"
}
Substitution Templates
You can use a substitution template to augment the JSON data returned when a rule is triggered and AWS
IoT performs an action. The syntax for a substitution template is ${expression}, where expression can
be any expression supported by AWS IoT in SELECT or WHERE clauses. For more information about
supported expressions, see AWS IoT SQL Reference (p. 158).
{
"sql": "SELECT *, topic() AS topic FROM 'my/iot/topic'",
"ruleDisabled": false,
"actions": [{
"republish": {
"topic": "${topic()}/republish",
"roleArn": "arn:aws:iam::123456789012:role/my-iot-role"
}
}]
}
{
"deviceid" : "iot123",
"temp" : 54.98,
"humidity" : 32.43,
"coords" : {
"latitude" : 47.615694,
"longitude" : -122.3359976
}
{
"coords":{
"longitude":-122.3359976,
"latitude":47.615694
},
"humidity":32.43,
"temp":54.98,
"deviceid":"iot123",
201
AWS IoT Developer Guide
Substitution Templates
"topic":"my/iot/topic"
}
202
AWS IoT Developer Guide
Device Shadows Data Flow
A thing shadow (sometimes referred to as a device shadow) is a JSON document that is used to store and
retrieve current state information for a thing (device, app, and so on). The Thing Shadows service maintains
a thing shadow for each thing you connect to AWS IoT. You can use thing shadows to get and set the state
of a thing over MQTT or HTTP, regardless of whether the thing is connected to the Internet. Each thing
shadow is uniquely identified by its name.
Contents
Device Shadows Data Flow (p. 203)
Device Shadows Documents (p. 211)
Using Device Shadows (p. 214)
Device Shadow RESTful API (p. 222)
Device Shadow MQTT Topics (p. 225)
Device Shadow Document Syntax (p. 231)
Device Shadow Error Messages (p. 233)
To illustrate how devices and applications communicate with the Thing Shadows service, this section walks
you through the use of the AWS IoT MQTT client and the AWS CLI to simulate communication between an
internet-connected light bulb, an application, and the Thing Shadows service.
The Thing Shadows service uses MQTT topics to facilitate communication between applications and
devices. To see how this works, use the AWS IoT MQTT client to subscribe to the following MQTT topics
with QoS 1:
$aws/things/myLightBulb/shadow/update/accepted
The Thing Shadows service sends messages to this topic when an update is successfully made to the
thing shadow.
$aws/things/myLightBulb/shadow/update/rejected
The Thing Shadows service sends messages to this topic when an update to the thing shadow is
rejected.
203
AWS IoT Developer Guide
Device Shadows Data Flow
$aws/things/myLightBulb/shadow/update/delta
The Thing Shadows service sends messages to this topic when a difference is detected between the
reported and desired sections of the thing shadow. For more information, see /update/delta (p. 227).
$aws/things/myLightBulb/shadow/get/accepted
The Thing Shadows service sends messages to this topic when a request for the thing shadow is made
successfully.
$aws/things/myLightBulb/shadow/get/rejected
The Thing Shadows service sends messages to this topic when a request for the thing shadow is
rejected.
$aws/things/myLightBulb/shadow/delete/accepted
The Thing Shadows service sends messages to this topic when the thing shadow is deleted.
$aws/things/myLightBulb/shadow/delete/rejected
The Thing Shadows service sends messages to this topic when a request to delete the thing shadow is
rejected.
$aws/things/myLightBulb/shadow/update/documents
The Thing Shadows service publishes a state document to this topic whenever an update to the thing
shadow is successfully performed.
To learn more about all of the MQTT topics used by the Thing Shadows service, see Device Shadow
MQTT Topics (p. 225).
Note
We recommend you subscribe to the .../rejected topics to see any errors sent by the Thing
Shadows service.
When the light bulb comes online, it sends its current state to the Thing Shadows service by sending an
MQTT message to the $aws/things/myLightBulb/shadow/update topic.
To simulate this, use the AWS IoT MQTT client to publish the following message to the $aws/things/
myLightbulb/shadow/update topic:
{
"state": {
"reported": {
"color": "red"
}
}
}
The Thing Shadows service responds by sending the following message to the $aws/things/myLightBulb/
shadow/update/accepted topic:
{
"messageNumber": 4,
"payload": {
"state": {
"reported": {
"color": "red"
}
204
AWS IoT Developer Guide
Device Shadows Data Flow
},
"metadata": {
"reported": {
"color": {
"timestamp": 1469564492
}
}
},
"version": 1,
"timestamp": 1469564492
},
"qos": 0,
"timestamp": 1469564492848,
"topic": "$aws/things/myLightBulb/shadow/update/accepted"
}
This message indicates the Thing Shadows service received the UPDATE request and updated the thing
shadow. If the thing shadow doesn't exist, it is created. Otherwise, the thing shadow is updated with the
data in the message. If you don't see a message published to $aws/things/myLightBulb/shadow/update/
accepted, check the subscription to $aws/things/myLightBulb/shadow/update/rejected to see any error
messages.
In addition, the Thing Shadows service publishes the following message to the $aws/things/myLightBulb/
shadow/update/documents topic.
{
"previous":null,
"current":{
"state":{
"reported":{
"color":"red"
}
},
"metadata":{
"reported":{
"color":{
"timestamp":1483467764
}
}
},
"version":1
},
"timestamp":1483467764
}
Messages are published to the /update/documents topic whenever an update to the thing shadow is
successfully performed. For more information of the contents of messages published to this topic, see
Device Shadow MQTT Topics (p. 225).
An application that interacts with the light bulb comes online and requests the light bulb's current state. The
application sends an empty message to the $aws/things/myLightBulb/shadow/get topic. To simulate this,
use the AWS IoT MQTT client to publish an empty message ("") to the $aws/things/myLightBulb/shadow/
get topic.
The Thing Shadows service responds by publishing the requested thing shadow to the $aws/things/
myLightBulb/shadow/get/accepted topic:
{
"messageNumber": 1,
"payload": {
"state": {
205
AWS IoT Developer Guide
Device Shadows Data Flow
"reported": {
"color": "red"
}
},
"metadata": {
"reported": {
"color": {
"timestamp": 1469564492
}
}
},
"version": 1,
"timestamp": 1469564571
},
"qos": 0,
"timestamp": 1469564571533,
"topic": "$aws/things/myLightBulb/shadow/get/accepted"
}
The application displays this information to the user, and the user requests a change to the light bulb's color
(from red to green). To do this, the application publishes a message on the $aws/things/myLightBulb/
shadow/update topic:
{
"state": {
"desired": {
"color": "green"
}
}
}
To simulate this, use the AWS IoT MQTT client to publish the preceding message to the $aws/things/
myLightBulb/shadow/update topic.
{
"messageNumber": 5,
"payload": {
"state": {
"desired": {
"color": "green"
}
},
"metadata": {
"desired": {
"color": {
"timestamp": 1469564658
}
}
},
"version": 2,
"timestamp": 1469564658
},
"qos": 0,
"timestamp": 1469564658286,
"topic": "$aws/things/myLightBulb/shadow/update/accepted"
}
206
AWS IoT Developer Guide
Device Shadows Data Flow
{
"messageNumber": 1,
"payload": {
"version": 2,
"timestamp": 1469564658,
"state": {
"color": "green"
},
"metadata": {
"color": {
"timestamp": 1469564658
}
}
},
"qos": 0,
"timestamp": 1469564658309,
"topic": "$aws/things/myLightBulb/shadow/update/delta"
}
The Thing Shadow service publishes a message to this topic when it accepts a thing shadow update and
the resulting thing shadow contains different values for desired and reported states.
{
"previous":{
"state":{
"reported":{
"color":"red"
}
},
"metadata":{
"reported":{
"color":{
"timestamp":1483467764
}
}
},
"version":1
},
"current":{
"state":{
"desired":{
"color":"green"
},
"reported":{
"color":"red"
}
},
"metadata":{
"desired":{
"color":{
"timestamp":1483468612
}
},
"reported":{
"color":{
"timestamp":1483467764
}
}
},
207
AWS IoT Developer Guide
Device Shadows Data Flow
"version":2
},
"timestamp":1483468612
}
{
"state":{
"reported":{
"color":"green"
},
"desired":null}
}
}
{
"messageNumber": 6,
"payload": {
"state": {
"reported": {
"color": "green"
},
"desired": null
},
"metadata": {
"reported": {
"color": {
"timestamp": 1469564801
}
},
"desired": {
"timestamp": 1469564801
}
},
"version": 3,
"timestamp": 1469564801
},
"qos": 0,
"timestamp": 1469564801673,
"topic": "$aws/things/myLightBulb/shadow/update/accepted"
}
{
"previous":{
"state":{
"reported":{
"color":"red"
}
},
"metadata":{
"reported":{
"color":{
208
AWS IoT Developer Guide
Device Shadows Data Flow
"timestamp":1483470355
}
}
},
"version":3
},
"current":{
"state":{
"reported":{
"color":"green"
}
},
"metadata":{
"reported":{
"color":{
"timestamp":1483470364
}
}
},
"version":4
},
"timestamp":1483470364
}
The app requests the current state from the Thing Shadows service and displays the most recent state
data. To simulate this, run the following command:
Note
On Windows, omit the && cat "output.txt", which displays the contents of output.txt to the
console. You can open the file in Notepad or any text editor to see the contents of the thing
shadow.
{
"state":{
"reported":{
"color":"green"
}
},
"metadata":{
"reported":{
"color":{
"timestamp":1469564801
}
}
},
"version":3,
"timestamp":1469564864}
{
"version" : 1,
"timestamp" : 1488565234
}
209
AWS IoT Developer Guide
Detecting a Thing is Connected
{
"rule": {
"ruleDisabled": false,
"sql": "SELECT * FROM 'my/things/myLightBulb/update'",
"description": "Turn my/things/ into $aws/things/",
"actions": [{
"republish": {
"topic": "$$aws/things/myLightBulb/shadow/update",
"roleArn": "arn:aws:iam::123456789012:role/aws_iot_republish"
}
}]
}
}
When a device connects, it registers an LWT that sets the connected setting to false:
{
"state": {
"reported":
{
"connected":"false"
}
}
}
{
"state": {
"reported":
{
"connected":"true"
}
}
}
When the device disconnects gracefully, it publishes a message on its update topic and sets its connected
state to false:
{
"state": {
"reported":{
"connected":"false"
210
AWS IoT Developer Guide
Device Shadows Documents
}
}
}
If the device disconnects due to an error, its LWT message is posted automatically to the update topic.
Contents
Document Properties (p. 211)
Versioning of a Thing Shadow (p. 212)
Client Token (p. 212)
Example Document (p. 212)
Empty Sections (p. 212)
Arrays (p. 213)
Document Properties
A thing shadow document has the following properties:
state
desired
The desired state of the thing. Applications can write to this portion of the document to update the
state of a thing without having to directly connect to a thing.
reported
The reported state of the thing. Things write to this portion of the document to report their new
state. Applications read this portion of the document to determine the state of a thing.
metadata
Information about the data stored in the state section of the document. This includes timestamps, in
Epoch time, for each attribute in the state section, which enables you to determine when they were
updated.
timestamp
Indicates when the message was transmitted by AWS IoT. By using the timestamp in the message and
the timestamps for individual attributes in the desired or reported section, a thing can determine how
old an updated item is, even if it doesn't feature an internal clock.
clientToken
A string unique to the device that enables you to associate responses with requests in an MQTT
environment.
version
The document version. Every time the document is updated, this version number is incremented. Used
to ensure the version of the document being updated is the most recent.
For more information, see Device Shadow Document Syntax (p. 231).
211
AWS IoT Developer Guide
Versioning of a Thing Shadow
A client can receive an error if it attempts to overwrite a shadow using an older version number. The
client is informed it must resync before it can update a thing shadow.
A client can decide not to act on a received message if the message has a lower version than the version
stored by the client.
In some cases, a client might bypass version matching by not submitting a version.
Client Token
You can use a client token with MQTT-based messaging to verify the same client token is contained in a
request and request response. This ensures the response and request are associated.
Example Document
Here is an example thing shadow document:
{
"state" : {
"desired" : {
"color" : "RED",
"sequence" : [ "RED", "GREEN", "BLUE" ]
},
"reported" : {
"color" : "GREEN"
}
},
"metadata" : {
"desired" : {
"color" : {
"timestamp" : 12345
},
"sequence" : {
"timestamp" : 12345
}
},
"reported" : {
"color" : {
"timestamp" : 12345
}
}
},
"version" : 10,
"clientToken" : "UniqueClientToken",
"timestamp": 123456789
}
Empty Sections
A thing shadow document contains a desired section only if it has a desired state. For example, the
following is a valid state document with no desired section:
212
AWS IoT Developer Guide
Arrays
{
"reported" : { "temp": 55 }
}
{
"desired" : { "color" : "RED" }
}
If an update causes the desired or reported sections to become null, the section is removed from
the document. To remove the desired section from a document (in response, for example, to a device
updating its state), set the desired section to null:
{
"state": {
"reported": {
"color": "red"
},
"desired": null
}
}
It is also possible a thing shadow document will not contain desired or reported sections. In that case, the
shadow document is empty. For example, this is a valid document:
{
}
Arrays
Thing shadows support arrays, but treat them as normal values in that an update to an array replaces the
whole array. It is not possible to update part of an array.
Initial state:
{
"desired" : { "colors" : ["RED", "GREEN", "BLUE" ] }
}
Update:
{
"desired" : { "colors" : ["RED"] }
}
Final state:
{
"desired" : { "colors" : ["RED"] }
}
Arrays can't have null values. For example, the following array is not valid and will be rejected.
213
AWS IoT Developer Guide
Using Device Shadows
"desired" : {
"colors" : [ null, "RED", "GREEN" ]
}
}
UPDATE
Creates a thing shadow if it doesn't exist, or updates the content of a thing shadow with the data
provided in the request. The data is stored with timestamp information to indicate when it was last
updated. Messages are sent to all subscribers with the difference between desired or reported state
(delta). Things or apps that receive a message can perform an action based on the difference between
desired or reported states. For example, a device can update its state to the desired state, or an app
can update its UI to show the change in the device's state.
GET
Retrieves the latest state stored in the thing shadow (for example, during start-up of a device to retrieve
configuration and the last state of operation). This method returns the full JSON document, including
metadata.
DELETE
Deletes a thing shadow, including all of its content. This removes the JSON document from the data
store. You can't restore a thing shadow you deleted, but you can create a new thing shadow with the
same name.
Protocol Support
These methods are supported through both MQTT and a RESTful API over HTTPS. Because MQTT
is a publish/subscribe communication model, AWS IoT implements a set of reserved topics. Things or
applications subscribe to these topics before publishing on a request topic in order to implement a request
response behavior. For more information, see Device Shadow MQTT Topics (p. 225) and Device Shadow
RESTful API (p. 222).
Initial state:
{
"state": {
"reported" : {
"color" : { "r" :255, "g": 255, "b": 0 }
}
}
}
214
AWS IoT Developer Guide
Retrieving a Thing Shadow Document
"state": {
"desired" : {
"color" : { "r" : 10 },
"engine" : "ON"
}
}
}
The device receives the desired state on the /update/delta topic that is triggered by the previous /update
message and then executes the desired changes. When finished, the device should confirm its updated
state through the reported section in the thing shadow JSON document.
Final state:
{
"state": {
"reported" : {
"color" : { "r" : 10, "g" : 255, "b": 0 },
"engine" : "ON"
}
}
}
Example document:
{
"state": {
"desired": {
"lights": {
"color": "RED"
},
"engine": "ON"
},
"reported": {
"lights": {
"color": "GREEN"
},
"engine": "ON"
}
},
"metadata": {
"desired": {
"lights": {
"color": {
"timestamp": 123456
},
"engine": {
"timestamp": 123456
}
}
},
"reported": {
"lights": {
"color": {
"timestamp": 789012
}
215
AWS IoT Developer Guide
Retrieving a Thing Shadow Document
},
"engine": {
"timestamp": 789012
}
},
"version": 10,
"timestamp": 123456789
}
}
Response:
{
"state": {
"desired": {
"lights": {
"color": "RED"
},
"engine": "ON"
},
"reported": {
"lights": {
"color": "GREEN"
},
"engine": "ON"
},
"delta": {
"lights": {
"color": "RED"
}
}
},
"metadata": {
"desired": {
"lights": {
"color": {
"timestamp": 123456
},
}
"engine": {
"timestamp": 123456
}
},
"reported": {
"lights": {
"color": {
"timestamp": 789012
}
},
"engine": {
"timestamp": 789012
}
},
"delta": {
"lights": {
"color": {
"timestamp": 123456
}
}
}
},
"version": 10,
"timestamp": 123456789
}
216
AWS IoT Developer Guide
Retrieving a Thing Shadow Document
Optimistic Locking
You can use the state document version to ensure you are updating the most recent version of a thing
shadow document. When you supply a version with an update request, the service rejects the request
with an HTTP 409 conflict response code if the current version of the state document does not match the
version supplied.
For example:
Initial document:
{
"state" : {
"desired" : { "colors" : ["RED", "GREEN", "BLUE" ] }
},
"version" : 10
}
{
"state": {
"desired": {
"colors": [
"BLUE"
]
}
},
"version": 9
}
Result:
409 Conflict
{
"state": {
"desired": {
"colors": [
"BLUE"
]
}
},
"version": 10
}
Final state:
{
"state": {
"desired": {
"colors": [
"BLUE"
]
}
},
"version": 11
217
AWS IoT Developer Guide
Deleting Data
Deleting Data
You can delete data from a thing shadow by publishing to the /update (p. 225) topic, setting the fields to
be deleted to null. Any field with a value of null is removed from the document.
Initial state:
{
"state": {
"desired" : {
"lights": { "color": "RED" },
"engine" : "ON"
},
"reported" : {
"lights" : { "color": "GREEN" },
"engine" : "OFF"
}
}
}
{
"state": {
"desired": null,
"reported": {
"engine": null
}
}
}
Final state:
{
"state": {
"reported" : {
"lights" : { "color" : "GREEN" }
}
}
}
You can delete all data from a thing shadow by setting its state to null. For example, sending the following
message will delete all of the state data, but the thing shadow will remain.
{
"state": null
}
The thing shadow still exists even if its state is null. The version of the thing shadow will be incremented
when the next update occurs.
218
AWS IoT Developer Guide
Delta State
Note
Deleting a thing shadow does not delete the thing, and deleting a thing does not delete the thing
shadow.
Initial state:
{
"state": {
"desired" : {
"lights": { "color": "RED" },
"engine" : "ON"
},
"reported" : {
"lights" : { "color": "GREEN" },
"engine" : "OFF"
}
}
}
Final state:
Delta State
Delta state is a virtual type of state that contains the difference between the desired and reported states.
Fields in the desired section that are not in the reported section are included in the delta. Fields that are
in the reported section and not in the desired section are not included in the delta. The delta contains
metadata, and its values are equal to the metadata in the desired field. For example:
{
"state": {
"desired": {
"color": "RED",
"state": "STOP"
},
"reported": {
"color": "GREEN",
"engine": "ON"
},
"delta": {
"color": "RED",
"state": "STOP"
}
},
"metadata": {
"desired": {
"color": {
"timestamp": 12345
},
"state": {
"timestamp": 12345
},
"reported": {
"color": {
"timestamp": 12345
},
"engine": {
"timestamp": 12345
219
AWS IoT Developer Guide
Observing State Changes
}
},
"delta": {
"color": {
"timestamp": 12345
},
"state": {
"timestamp": 12345
}
}
},
"version": 17,
"timestamp": 123456789
}
}
When nested objects differ, the delta contains the path all the way to the root.
{
"state": {
"desired": {
"lights": {
"color": {
"r": 255,
"g": 255,
"b": 255
}
}
},
"reported": {
"lights": {
"color": {
"r": 255,
"g": 0,
"b": 255
}
}
},
"delta": {
"lights": {
"color": {
"g": 255
}
}
}
},
"version": 18,
"timestamp": 123456789
}
The Thing Shadows service calculates the delta by iterating through each field in the desired state and
comparing it to the reported state.
Arrays are treated like values. If an array in the desired section doesn't match the array in the reported
section, then the entire desired array is copied into the delta.
$aws/things/thing-name/shadow/update/accepted
$aws/things/thing-name/shadow/update/delta
220
AWS IoT Developer Guide
Message Order
The message sent to the update/delta topic is intended for the thing whose state is being updated. This
message contains only the difference between the desired and reported sections of the thing shadow
document. Upon receiving this message, the thing decides whether to make the requested change. If the
thing's state is changed, it publishes its new current state to the $aws/things/thing-name/shadow/update
topic.
Devices and applications can subscribe to either of these topics to be notified when the state of the
document has changed.
Message Order
There is no guarantee that messages from the AWS IoT service will arrive at the device in any specific
order.
{
"state" : {
"reported" : { "color" : "blue" }
},
"version" : 10,
"timestamp": 123456777
}
Update 1:
{
"state": { "desired" : { "color" : "RED" } },
"version": 10,
"timestamp": 123456777
}
Update 2:
{
"state": { "desired" : { "color" : "GREEN" } },
"version": 11 ,
"timestamp": 123456778
}
{
"state": {
"reported": { "color" : "GREEN" }
},
"version": 12,
221
AWS IoT Developer Guide
Trim Device Shadow Messages
"timestamp": 123456779
}
{
"state": {
"color": "RED"
},
"version": 11,
"timestamp": 123456778
}
{
"state": { "color" : "GREEN" },
"version": 12,
"timestamp": 123456779
}
The device might receive these messages out of order. Because the state in these messages is cumulative,
a device can safely discard any messages that contain a version number older than the one it is tracking. If
the device receives the delta for version 12 before version 11, it can safely discard the version 11 message.
The rule is specified in JSON and should look like the following:
{
"sql": "SELECT state, version FROM '$aws/things/+/shadow/update/delta'",
"ruleDisabled": false,
"actions": [{
"republish": {
"topic": "${topic(2)}/delta",
"roleArn": "arn:aws:iam::123456789012:role/my-iot-role"
}
}]
}
The SELECT statement determines which fields from the message will be republished to the specified
topic. A "+" wild card is used to match all thing shadow names. The rule specifies that all matching
messages should be republished to the specified topic. In this case, the "topic()" function is used to
specify the topic on which to republish. topic(2) evaluates to the thing name in the original topic. For more
information about creating rules, see Rules.
https://endpoint/things/thingName/shadow
The endpoint is specific to your AWS account. To retrieve your endpoint, use the describe-endpoint
command. The format of the endpoint is as follows:
222
AWS IoT Developer Guide
GetThingShadow
identifier.iot.region.amazonaws.com
API Actions
GetThingShadow (p. 223)
UpdateThingShadow (p. 223)
DeleteThingShadow (p. 224)
GetThingShadow
Gets the thing shadow for the specified thing.
The response state document includes the delta between the desired and reported states.
Request
The request includes the standard HTTP headers plus the following URI:
Response
Upon success, the response includes the standard HTTP headers plus the following code and body:
HTTP 200
BODY: response state document
For more information, see Example Response State Document (p. 231).
Authorization
Retrieving a thing shadow requires a policy that allows the caller to perform the iot:GetThingShadow
action. The Thing Shadows service accepts two forms of authentication: Signature Version 4 with IAM
credentials or TLS mutual authentication with a client certificate.
The following is an example policy that allows a caller to retrieve a thing shadow:
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": "iot:GetThingShadow",
"Resource": ["arn:aws:iot:region:account:thing/thing"]
}]
}
UpdateThingShadow
Updates the thing shadow for the specified thing.
Updates affect only the fields specified in the request state document. Any field with a value of null is
removed from the thing shadow.
Request
The request includes the standard HTTP headers plus the following URI and body:
223
AWS IoT Developer Guide
DeleteThingShadow
For more information, see Example Request State Document (p. 231).
Response
Upon success, the response includes the standard HTTP headers plus the following code and body:
HTTP 200
BODY: response state document
For more information, see Example Response State Document (p. 231).
Authorization
Updating a thing shadow requires a policy that allows the caller to perform the iot:UpdateThingShadow
action. The Thing Shadows service accepts two forms of authentication: Signature Version 4 with IAM
credentials or TLS mutual authentication with a client certificate.
The following is an example policy that allows a caller to update a thing shadow:
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": "iot:UpdateThingShadow",
"Resource": ["arn:aws:iot:region:account:thing/thing"]
}]
}
DeleteThingShadow
Deletes the thing shadow for the specified thing.
Request
The request includes the standard HTTP headers plus the following URI:
Response
Upon success, the response includes the standard HTTP headers plus the following code and body:
HTTP 200
BODY: Empty response state document
Authorization
Deleting a thing shadow requires a policy that allows the caller to perform the iot:DeleteThingShadow
action. The Thing Shadows service accepts two forms of authentication: Signature Version 4 with IAM
credentials or TLS mutual authentication with a client certificate.
The following is an example policy that allows a caller to delete a thing shadow:
224
AWS IoT Developer Guide
MQTT Pub/Sub Topics
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": "iot:DeleteThingShadow",
"Resource": ["arn:aws:iot:region:account:thing/thing"]
}]
}
The following are the MQTT topics used for interacting with thing shadows.
Topics
/update (p. 225)
/update/accepted (p. 226)
/update/documents (p. 226)
/update/rejected (p. 227)
/update/delta (p. 227)
/get (p. 228)
/get/accepted (p. 228)
/get/rejected (p. 229)
/delete (p. 229)
/delete/accepted (p. 230)
/delete/rejected (p. 230)
/update
Publish a request state document to this topic to update the thing shadow:
$aws/things/thingName/shadow/update
A client attempting to update the state of a thing would send a JSON request state document like this:
{
"state" : {
"desired" : {
"color" : "red",
"power" : "on"
}
}
}
A thing updating its thing shadow would send a JSON request state document like this:
225
AWS IoT Developer Guide
/update/accepted
{
"state" : {
"reported" : {
"color" : "red",
"power" : "on"
}
}
}
AWS IoT responds by publishing to either /update/accepted (p. 226) or /update/rejected (p. 227).
Example Policy
The following is an example of the required policy:
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": ["iot:Publish"],
"Resource": ["arn:aws:iot:region:account:topic/$aws/things/thingName/shadow/
update"]
}]
}
/update/accepted
AWS IoT publishes a response state document to this topic when it accepts a change for the thing shadow:
$aws/things/thingName/shadow/update/accepted
Example Policy
The following is an example of the required policy:
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"iot:Subscribe",
"iot:Receive"
],
"Resource": ["arn:aws:iot:region:account:topicfilter/$aws/things/thingName/shadow/
update/accepted"]
}]
}
/update/documents
AWS IoT publishes a state document to this topic whenever an update to the shadow is successfully
performed:
226
AWS IoT Developer Guide
/update/rejected
$aws/things/thingName/shadow/update/documents
The JSON document will contain two primary nodes: previous and current. The previous node will
contain the contents of the full shadow document before the update was performed while current will
contain the full shadow document after the update is successfully applied. When the device shadow is
updated (created) for the first time, the previous node will contain null.
Example Policy
The following is an example of the required policy:
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"iot:Subscribe",
"iot:Receive"
],
"Resource": ["arn:aws:iot:region:account:topicfilter/$aws/things/thingName/shadow/
update/documents"]
}]
}
/update/rejected
AWS IoT publishes an error response document to this topic when it rejects a change for the thing shadow:
$aws/things/thingName/shadow/update/rejected
Example Policy
The following is an example of the required policy:
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"iot:Subscribe",
"iot:Receive"
],
"Resource": ["arn:aws:iot:region:account:topicfilter/$aws/things/thingName/shadow/
update/rejected"]
}]
}
/update/delta
AWS IoT publishes a response state document to this topic when it accepts a change for the thing shadow
and the request state document contains different values for desired and reported states:
$aws/things/thingName/shadow/update/delta
227
AWS IoT Developer Guide
/get
Publishing Details
A message published on update/delta includes only the desired attributes that differ between the
desired and reported sections. It contains all of these attributes, regardless of whether these attributes
were contained in the current update message or were already stored in AWS IoT. Attributes that do not
differ between the desired and reported sections are not included.
If an attribute is in the reported section but has no equivalent in the desired section, it is not included.
If an attribute is in the desired section but has no equivalent in the reported section, it is included.
If an attribute is deleted from the reported section but still exists in the desired section, it is included.
Example Policy
The following is an example of the required policy:
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"iot:Subscribe",
"iot:Receive"
],
"Resource": ["arn:aws:iot:region:account:topicfilter/$aws/things/thingName/shadow/
update/delta"]
}]
}
/get
Publish an empty message to this topic to get the thing shadow:
$aws/things/thingName/shadow/get
AWS IoT responds by publishing to either /get/accepted (p. 228) or /get/rejected (p. 229).
Example Policy
The following is an example of the required policy:
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"iot:Publish"
],
"Resource": ["arn:aws:iot:region:account:topic/$aws/things/thingName/shadow/get"]
}]
}
/get/accepted
AWS IoT publishes a response state document to this topic when returning the thing shadow:
228
AWS IoT Developer Guide
/get/rejected
$aws/things/thingName/shadow/get/accepted
Example Policy
The following is an example of the required policy:
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"iot:Subscribe",
"iot:Receive"
],
"Resource": ["arn:aws:iot:region:account:topicfilter/$aws/things/thingName/shadow/
get/accepted"]
}]
}
/get/rejected
AWS IoT publishes an error response document to this topic when it can't return the thing shadow:
$aws/things/thingName/shadow/get/rejected
Example Policy
The following is an example of the required policy:
{
"Version": "2012-10-17",
"Statement": [{
"Action": [
"iot:Subscribe",
"iot:Receive"
],
"Resource": ["arn:aws:iot:region:account:topicfilter/$aws/things/thingName/shadow/
get/rejected"]
}]
}
/delete
To delete a thing shadow, publish an empty message to the delete topic:
$aws/things/thingName/shadow/delete
AWS IoT responds by publishing to either /delete/accepted (p. 230) or /delete/rejected (p. 230).
229
AWS IoT Developer Guide
/delete/accepted
Example Policy
The following is an example of the required policy:
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"iot:Subscribe",
"iot:Receive"
],
"Resource": ["arn:aws:iot:region:account:topic filter/$aws/things/thingName/shadow/
delete"]
}]
}
/delete/accepted
AWS IoT publishes a message to this topic when a thing shadow is deleted:
$aws/things/thingName/shadow/delete/accepted
Example Policy
The following is an example of the required policy:
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"iot:Subscribe",
"iot:Receive"
],
"Resource": ["arn:aws:iot:region:account:topicfilter/$aws/things/thingName/shadow/
delete/accepted"]
}]
}
/delete/rejected
AWS IoT publishes an error response document to this topic when it can't delete the thing shadow:
$aws/things/thingName/shadow/delete/rejected
Example Policy
The following is an example of the required policy:
{
"Version": "2012-10-17",
230
AWS IoT Developer Guide
Document Syntax
"Statement": [{
"Effect": "Allow",
"Action": [
"iot:Subscribe",
"iot:Receive"
],
"Resource": ["arn:aws:iot:region:account:topicfilter/$aws/things/thingName/shadow/
delete/rejected"]
}]
}
Examples
Request State Documents (p. 231)
Response State Documents (p. 231)
Error Response Documents (p. 233)
{
"state": {
"desired": {
"attribute1": integer2,
"attribute2": "string2",
...
"attributeN": boolean2
},
"reported": {
"attribute1": integer1,
"attribute2": "string1",
...
"attributeN": boolean1
}
}
"clientToken": "token",
"version": version
}
231
AWS IoT Developer Guide
Response State Documents
{
"state": {
"desired": {
"attribute1": integer2,
"attribute2": "string2",
...
"attributeN": boolean2
},
"reported": {
"attribute1": integer1,
"attribute2": "string1",
...
"attributeN": boolean1
},
"delta": {
"attribute3": integerX,
"attribute5": "stringY"
}
},
"metadata": {
"desired": {
"attribute1": {
"timestamp": timestamp
},
"attribute2": {
"timestamp": timestamp
},
...
"attributeN": {
"timestamp": timestamp
}
},
"reported": {
"attribute1": {
"timestamp": timestamp
},
"attribute2": {
"timestamp": timestamp
},
...
"attributeN": {
"timestamp": timestamp
}
}
},
"timestamp": timestamp,
"clientToken": "token",
"version": version
}
state
reported Only present if a thing reported any data in the reported section and contains only fields
that were in the request state document.
desired Only present if a thing reported any data in the desired section and contains only fields
that were in the request state document.
metadata Contains the timestamps for each attribute in the desired and reported sections so that
you can determine when the state was updated.
timestamp The Epoch date and time the response was generated by AWS IoT.
clientToken Present only if a client token was used when publishing valid JSON to the /update topic.
version The current version of the document for the thing shadow shared in AWS IoT. It is increased
by one over the previous version of the document.
232
AWS IoT Developer Guide
Error Response Documents
{
"code": error-code,
"message": "error-message",
"timestamp": timestamp,
"clientToken": "token"
}
For more information, see Device Shadow Error Messages (p. 233).
413 (Payload Too Large) The payload exceeds the maximum size allowed
429 (Too Many Requests) The Thing Shadow service will generate this error message
when there are more than 10 in-flight requests.
233
AWS IoT Developer Guide
Error Messages
234
AWS IoT Developer Guide
AWS Mobile SDK for Android
Contents
AWS Mobile SDK for Android (p. 235)
Arduino Yn SDK (p. 235)
AWS IoT Device SDK for Embedded C (p. 236)
AWS Mobile SDK for iOS (p. 236)
AWS IoT Device SDK for Java (p. 236)
AWS IoT Device SDK for JavaScript (p. 236)
AWS IoT Device SDK for Python (p. 236)
The AWS IoT Device SDKs help you to easily and quickly connect your devices to AWS IoT. The AWS IoT
Device SDKs include open-source libraries, developer guides with samples, and porting guides so that you
can build innovative IoT products or solutions on your choice of hardware platforms.
Arduino Yn SDK
The AWS IoT Arduino Yn SDK allows developers to connect their Arduino Yn-compatible boards to AWS
IoT. By connecting a device to AWS IoT, users can securely work with the message broker, rules, and
thing shadows provided by AWS IoT and with other AWS services like AWS Lambda, Amazon Kinesis, and
Amazon S3. For more information, see the following:
235
AWS IoT Developer Guide
AWS IoT Device SDK for Embedded C
236
AWS IoT Developer Guide
AWS IoT Device SDK for Python
devices to AWS IoT, users can securely work with the message broker, rules, and thing shadows provided
by AWS IoT and with other AWS services like AWS Lambda, Amazon Kinesis, and Amazon S3, and more.
237
AWS IoT Developer Guide
Monitoring is an important part of maintaining the reliability, availability, and performance of AWS IoT and
your AWS solutions. You should collect monitoring data from all parts of your AWS solution so that you
can more easily debug a multi-point failure if one occurs. Before you start monitoring AWS IoT, you should
create a monitoring plan that includes answers to the following questions:
The next step is to establish a baseline for normal AWS IoT performance in your environment, by
measuring performance at various times and under different load conditions. As you monitor AWS IoT,
store historical monitoring data so that you can compare it with current performance data, identify normal
performance patterns and performance anomalies, and devise methods to address issues.
For example, if you're using Amazon EC2, you can monitor CPU utilization, disk I/O, and network
utilization for your instances. When performance falls outside your established baseline, you might need to
reconfigure or optimize the instance to reduce CPU utilization, improve disk I/O, or reduce network traffic.
PublishIn.Success
PublishOut.Success
Subscribe.Success
Ping.Success
Connect.Success
GetThingShadow.Accepted
UpdateThingShadow.Accepted
DeleteThingShadow.Accepted
RulesExecuted
Topics
Monitoring Tools (p. 239)
238
AWS IoT Developer Guide
Monitoring Tools
Monitoring Tools
AWS provides various tools that you can use to monitor AWS IoT. You can configure some of these tools
to do the monitoring for you, while some of the tools require manual intervention. We recommend that you
automate monitoring tasks as much as possible.
Amazon CloudWatch Alarms Watch a single metric over a time period that you specify, and perform
one or more actions based on the value of the metric relative to a given threshold over a number of time
periods. The action is a notification sent to an Amazon Simple Notification Service (Amazon SNS) topic
or Auto Scaling policy. CloudWatch alarms do not invoke actions simply because they are in a particular
state; the state must have changed and been maintained for a specified number of periods. For more
information, see Monitoring with Amazon CloudWatch (p. 240).
Amazon CloudWatch Logs Monitor, store, and access your log files from AWS CloudTrail or other
sources. For more information, see Monitoring Log Files in the Amazon CloudWatch User Guide.
Amazon CloudWatch Events Match events and route them to one or more target functions or streams
to make changes, capture state information, and take corrective action. For more information, see Using
Events in the Amazon CloudWatch User Guide.
AWS CloudTrail Log Monitoring Share log files between accounts, monitor CloudTrail log files in
real time by sending them to CloudWatch Logs, write log processing applications in Java, and validate
that your log files have not changed after delivery by CloudTrail. For more information, see Working with
CloudTrail Log Files in the AWS CloudTrail User Guide.
239
AWS IoT Developer Guide
Monitoring with Amazon CloudWatch
Topics
AWS IoT Metrics and Dimensions (p. 240)
How Do I Use AWS IoT Metrics? (p. 244)
Creating CloudWatch Alarms to Monitor AWS IoT (p. 244)
IoT Metrics
Metric Description
240
AWS IoT Developer Guide
Metrics and Dimensions
Rule Metrics
Metric Description
Metric Description
Metric Description
241
AWS IoT Developer Guide
Metrics and Dimensions
Metric Description
242
AWS IoT Developer Guide
Metrics and Dimensions
Metric Description
Note
The message broker metrics are displayed in the AWS IoT console under Protocol Metrics.
Metric Description
243
AWS IoT Developer Guide
Using AWS IoT Metrics
Note
The thing shadow metrics are displayed in the AWS IoT console under Protocol Metrics.
Dimension Description
ActionType The action type specified by the rule that triggered by the
request.
Protocol The protocol used to make the request. Valid values are:
MQTT or HTTP
How can I be notified if my things do not connect successfully each day? (p. 244)
How can I be notified if my things are not publishing data each day? (p. 245)
How can I be notified if my thing's shadow updates are being rejected each day? (p. 246)
244
AWS IoT Developer Guide
Creating CloudWatch Alarms
245
AWS IoT Developer Guide
Logging AWS IoT API Calls with AWS CloudTrail
--unit Count \
--evaluation-periods 1 \
--alarm-actions arn:aws:sns:us-east-1:1234567890:things-not-publishing-data
To learn more about CloudTrail, including how to configure and enable it, see the AWS CloudTrail User
Guide.
246
AWS IoT Developer Guide
AWS IoT Information in CloudTrail
All AWS IoT actions are logged by CloudTrail and are documented in the AWS IoT API Reference. For
example, calls to the CreateThing, ListThings, and ListTopicRules sections generate entries in the
CloudTrail log files.
Every log entry contains information about who generated the request. The user identity information in the
log entry helps you determine the following:
Whether the request was made with root or IAM user credentials.
Whether the request was made with temporary security credentials for a role or federated user.
Whether the request was made by another AWS service.
You can store your log files in your Amazon S3 bucket for as long as you want, but you can also define
Amazon S3 lifecycle rules to archive or delete log files automatically. By default, your log files are encrypted
with Amazon S3 server-side encryption (SSE).
If you want to be notified upon log file delivery, you can configure CloudTrail to publish Amazon SNS
notifications when new log files are delivered. For more information, see Configuring Amazon SNS
Notifications for CloudTrail.
You can also aggregate AWS IoT log files from multiple AWS regions and multiple AWS accounts into a
single Amazon S3 bucket.
For more information, see Receiving CloudTrail Log Files from Multiple Regions and Receiving CloudTrail
Log Files from Multiple Accounts.
The following example shows a CloudTrail log entry that demonstrates the AttachPrincipalPolicy action.
{
"timestamp":"1460159496",
"AdditionalEventData":"",
"Annotation":"",
"ApiVersion":"",
"ErrorCode":"",
"ErrorMessage":"",
"EventID":"8bff4fed-c229-4d2d-8264-4ab28a487505",
"EventName":"AttachPrincipalPolicy",
"EventTime":"2016-04-08T23:51:36Z",
"EventType":"AwsApiCall",
"ReadOnly":"",
"RecipientAccountList":"",
"RequestID":"d4875df2-fde4-11e5-b829-23bf9b56cbcd",
"RequestParamters":{
247
AWS IoT Developer Guide
Understanding AWS IoT Log File Entries
"principal":"arn:aws:iot:us-
east-1:123456789012:cert/528ce36e8047f6a75ee51ab7beddb4eb268ad41d2ea881a10b67e8e76924d894",
"policyName":"ExamplePolicyForIoT"
},
"Resources":"",
"ResponseElements":"",
"SourceIpAddress":"52.90.213.26",
"UserAgent":"aws-internal/3",
"UserIdentity":{
"type":"AssumedRole",
"principalId":"AKIAI44QH8DHBEXAMPLE",
"arn":"arn:aws:sts::12345678912:assumed-role/iotmonitor-us-east-1-beta-
InstanceRole-1C5T1YCYMHPYT/i-35d0a4b6",
"accountId":"222222222222",
"accessKeyId":"access-key-id",
"sessionContext":{
"attributes":{
"mfaAuthenticated":"false",
"creationDate":"Fri Apr 08 23:51:10 UTC 2016"
},
"sessionIssuer":{
"type":"Role",
"principalId":"AKIAI44QH8DHBEXAMPLE",
"arn":"arn:aws:iam::123456789012:role/executionServiceEC2Role/iotmonitor-
us-east-1-beta-InstanceRole-1C5T1YCYMHPYT",
"accountId":"222222222222",
"userName":"iotmonitor-us-east-1-InstanceRole-1C5T1YCYMHPYT"
}
},
"invokedBy":{
"serviceAccountId":"111111111111"
}
},
"VpcEndpointId":""
}
248
AWS IoT Developer Guide
Diagnosing Connectivity Issues
The following information might help you troubleshoot common issues in AWS IoT.
Tasks
Diagnosing Connectivity Issues (p. 249)
Setting Up CloudWatch Logs (p. 250)
Diagnosing Rules Issues (p. 254)
Diagnosing Problems with Thing Shadows (p. 255)
Add the AWS IoT CA certificate to your clients trust store. You can download the CA certificate from
here.
How can I validate a correctly configured certificate?
Use the OpenSSL s_client command to test a connection to the AWS IoT endpoint:
Authorization
I received a PUBNACK or SUBNACK response from the broker. What do I do?
Make sure there is a policy attached to the certificate you are using to call AWS IoT. All publish/
subscribe operations are denied by default.
249
AWS IoT Developer Guide
Setting Up CloudWatch Logs
Role policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:PutMetricFilter",
"logs:PutRetentionPolicy"
],
"Resource": [
"*"
]
}
]
}
Trust policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "iot.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
250
AWS IoT Developer Guide
CloudWatch Log Entry Format
Event
An identifier generated randomly for an incoming request that can be used to filter all of the
corresponding logs to one incoming message.
PrincipalId
A certificate fingerprint or a thing name, depending on which endpoint (MQTT or HTTP) received the
request from a device.
Depending upon the message context the following fields may also be included in log messages:
Topic Name
The MQTT topic name, which is added to an entry when an MQTT publish or subscribe message is
received.
ClientId
The thing name, which is added to an entry when a request is sent to an HTTP endpoint to update or
delete thing state.
RuleId
The rule identifier, which contains the ID of a rule when the rule is triggered.
Log Level
The log level specifies which types of logs will be generated.
251
AWS IoT Developer Guide
Logging Events and Error Codes
ERROR
Anything that can potentially cause inconsistencies in the system, but might not necessarily cause the
operation to fail.
401 Unauthorized
Message Broker
252
AWS IoT Developer Guide
Logging Events and Error Codes
Package Exceeds Maximum Payload Size This client attempted to publish a payload that
exceeds the message broker's upper limit.
401 Unauthorized
403 Forbidden
253
AWS IoT Developer Guide
Diagnosing Rules Issues
401 Unauthorized.
403 Forbidden.
409 Conflict.
The most common issue is authorization. In this case, the logs will tell you your role is not authorized to
perform AssumeRole on the resource.
254
AWS IoT Developer Guide
Diagnosing Problems with Thing Shadows
External services are controlled by the end user. Before rule execution, make sure external services are set
up with enough throughput and capacity units.
A thing shadow document is rejected with "Invalid If you are unfamiliar with JSON, modify the
JSON document." examples provided in this guide for your own
use. For more information, see Thing Shadow
Document Syntax.
I submitted correct JSON, but none or only parts of Be sure you are following the JSON formatting
it are stored in the thing shadow document. guidelines. Only JSON fields in the desired and
reported sections will be stored. JSON content
(even if formally correct) outside of those sections
will be ignored.
I received an error that the thing shadow exceeds The thing shadow supports 8 KB of data only.
the allowed size. Try shortening field names inside of your JSON
document or simply create more thing shadows.
A device can have an unlimited number of thing
shadows. The only requirement is that the thing
name is unique in your account.
When I receive a thing shadow, it is larger than 8 Upon receipt, the AWS IoT service adds metadata
KB. How can this happen? to the thing shadow. The service includes this
data in its response, but it does not count toward
the limit of 8 KB. Only the data for desired and
reported state inside the state document sent to
the thing shadow counts toward the limit.
My request has been rejected due to incorrect Perform a GET operation to sync to the latest state
version. What should I do? document version. When using MQTT, subscribe to
the ./update/accepted topic so you will be notified
about state changes and receive the latest version
of the JSON document.
The timestamp is off by several seconds. The timestamp for individual fields and the whole
JSON document is updated when the document is
received by the AWS IoT service or when the state
document is published onto the ./update/accepted
and ./update/delta message. Messages can be
delayed over the network, which can cause the
timestamp to be off by a few seconds.
My device can publish and subscribe on the Be sure you have created policies in IAM to allow
corresponding thing shadow topics, but when I access to these topics and for the corresponding
attempt to update the thing shadow document over action (UPDATE/GET/DELETE) for the credentials
the HTTP REST API, I get HTTP 403. you are using. IAM policies and certificate policies
are independent.
255
AWS IoT Developer Guide
Diagnosing Problems with Thing Shadows
256