You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

适用于所有Thing Shadow更新的单一AWS IoT规则

Fixing AWS IoT Rule to Trigger Lambda on Any Thing Shadow Update Accepted

Got it, let's break down why your wildcard approach isn't working and get you set up correctly to handle all Thing Shadow update successes with a single Lambda function.

The Problem with Your Current Wildcard

Your rule uses # which is a multi-level wildcard in AWS IoT MQTT topics, but it's not the right fit here. The # wildcard matches all remaining levels of a topic starting from its position. While $aws/things/# would technically match your target topics, it would also match every other topic under $aws/things/ (like shadow get requests, delete events, etc.)—and more importantly, the way you structured it ($aws/things/#/shadow/update/accepted) is invalid syntax because # must be the final segment in a topic filter. That's why your rule wasn't triggering as expected.

The Correct Wildcard Syntax

To specifically target all Thing Shadow update accepted events (and nothing else), use the + wildcard instead. The + matches exactly one topic level, which is perfect for replacing the specific thing name:

SELECT * FROM '$aws/things/+/shadow/update/accepted'

This will reliably match any topic like:

  • $aws/things/testThing/shadow/update/accepted
  • $aws/things/device001/shadow/update/accepted
  • $aws/things/sensor-abc/shadow/update/accepted

Additional Steps to Ensure It Works

  • Verify Lambda Permissions
    Make sure your AWS IoT rule has permission to invoke the Lambda function. When you create the rule action to trigger Lambda, AWS usually auto-creates the necessary IAM role, but double-check that the role includes the lambda:InvokeFunction permission for your target Lambda. If your Lambda needs to interact with IoT Shadows (like reading/writing back), ensure it has permissions like iot:GetThingShadow or iot:UpdateThingShadow attached to its execution role.

  • Extract the Thing Name in Lambda
    If you need to know which specific Thing triggered the Lambda, modify the SQL to include the thing name directly in the payload:

    SELECT topic(2) AS thingName, * FROM '$aws/things/+/shadow/update/accepted'
    

    The topic(2) function extracts the 3rd segment of the topic (counting starts at 0: segment 0 = $aws, segment 1 = things, segment 2 = the thing name). Your Lambda will then receive a thingName field in the event payload, making per-device logic straightforward.

  • Test the Rule
    Use the AWS IoT Console's Test client to publish a sample message to a test Thing's shadow update accepted topic. Check Lambda logs to confirm it's invoked and receiving the expected payload.

内容的提问来源于stack exchange,提问作者tharun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:34:07