本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
步骤 1:创建 Amazon IoT 策略
在此过程中,创建一个允许您的设备访问 Amazon IoT 本教程中使用的资源的 Amazon IoT 策略。
创建 Amazon IoT 策略
-
查看支持AmazonAmazon IoT SiteWise 的地区。如有必要,切换到其中一个受支持的区域。
-
导航到 Amazon IoT 控制台
。如果出现 “Connect 设备” 按钮,请选择该按钮。 -
在左侧导航窗格中,选择安全,然后选择策略。
-
选择创建。
-
输入 Amazon IoT 策略的名称(例如,
SiteWiseTutorialDevicePolicy
)。 -
在政策文档下,选择 JSON,以 JSON 形式输入以下政策。将
region
和account-id
替换为您的区域和账户 ID,例如us-east-1
和123456789012
。{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "iot:Connect", "Resource": "arn:aws:iot:
region
:account-id
:client/SiteWiseTutorialDevice*" }, { "Effect": "Allow", "Action": "iot:Publish", "Resource": [ "arn:aws:iot:region
:account-id
:topic/$aws/things/${iot:Connection.Thing.ThingName}/shadow/update", "arn:aws:iot:region
:account-id
:topic/$aws/things/${iot:Connection.Thing.ThingName}/shadow/delete", "arn:aws:iot:region
:account-id
:topic/$aws/things/${iot:Connection.Thing.ThingName}/shadow/get" ] }, { "Effect": "Allow", "Action": "iot:Receive", "Resource": [ "arn:aws:iot:region
:account-id
:topic/$aws/things/${iot:Connection.Thing.ThingName}/shadow/update/accepted", "arn:aws:iot:region
:account-id
:topic/$aws/things/${iot:Connection.Thing.ThingName}/shadow/delete/accepted", "arn:aws:iot:region
:account-id
:topic/$aws/things/${iot:Connection.Thing.ThingName}/shadow/get/accepted", "arn:aws:iot:region
:account-id
:topic/$aws/things/${iot:Connection.Thing.ThingName}/shadow/update/rejected", "arn:aws:iot:region
:account-id
:topic/$aws/things/${iot:Connection.Thing.ThingName}/shadow/delete/rejected" ] }, { "Effect": "Allow", "Action": "iot:Subscribe", "Resource": [ "arn:aws:iot:region
:account-id
:topicfilter/$aws/things/${iot:Connection.Thing.ThingName}/shadow/update/accepted", "arn:aws:iot:region
:account-id
:topicfilter/$aws/things/${iot:Connection.Thing.ThingName}/shadow/delete/accepted", "arn:aws:iot:region
:account-id
:topicfilter/$aws/things/${iot:Connection.Thing.ThingName}/shadow/get/accepted", "arn:aws:iot:region
:account-id
:topicfilter/$aws/things/${iot:Connection.Thing.ThingName}/shadow/update/rejected", "arn:aws:iot:region
:account-id
:topicfilter/$aws/things/${iot:Connection.Thing.ThingName}/shadow/delete/rejected" ] }, { "Effect": "Allow", "Action": [ "iot:GetThingShadow", "iot:UpdateThingShadow", "iot:DeleteThingShadow" ], "Resource": "arn:aws:iot:region
:account-id
:thing/SiteWiseTutorialDevice*" } ] }此策略使您的 Amazon IoT 设备能够使用 MQTT 消息建立连接并与设备影子通信。有关 MQTT 消息的更多信息,请参阅什么是 MQ
TT? 。要与设备影子进行交互,您的 Amazon IoT 事物会发布和接收有关以开头的主题的 MQTT 消息。 $aws/things/
此策略包含一个称为 “事物” 策略变量thing-name
/shadow/${iot:Connection.Thing.ThingName}
。此变量在每个主题中替换关联事物的名称。该iot:Connect
语句限制了哪些设备可以建立连接,从而确保事物策略变量只能替换以开头的名称SiteWiseTutorialDevice
。有关更多信息,请参阅Amazon IoT 开发人员指南中的事物策略变量。
注意
此策略适用于名称以
SiteWiseTutorialDevice
开头的事物。要为您的事物使用不同的名称,您必须相应地更新策略。 -
选择创建。