适用于所有Thing Shadow更新的单一AWS IoT规则
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 thelambda:InvokeFunctionpermission for your target Lambda. If your Lambda needs to interact with IoT Shadows (like reading/writing back), ensure it has permissions likeiot:GetThingShadoworiot:UpdateThingShadowattached 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 athingNamefield 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

