feat: Add support for error action on IoT topic rule#13423
Conversation
- Add errorAction property to IoT event schema - Handle errorAction.lambda.functionArn in compileIoTEvents - Add unit test for errorAction functionality Fixes serverless#11642
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughAdded optional Changes
Sequence Diagram(s)sequenceDiagram
participant Dev as Developer / serverless.yml
participant SLS as Serverless Compile Plugin
participant CF as CloudFormation Template
participant IoT as AWS::IoT::TopicRule
participant Lambda as AWS::Lambda::Permission / Function
Dev->>SLS: define iot event with errorAction.lambda.functionArn
SLS->>CF: compile TopicRulePayload (include ErrorAction when present)
SLS->>CF: add AWS::Lambda::Permission resource for errorAction Lambda
CF->>IoT: create TopicRule with ErrorAction -> Lambda.FunctionArn
CF->>Lambda: attach permission allowing iot.amazonaws.com to invoke function
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/serverless/lib/plugins/aws/package/compile/events/iot.js`:
- Around line 43-62: The errorAction schema currently allows an empty object
because it doesn't require any action types; update the errorAction schema (the
errorAction object and its properties) to require at least one action type by
adding required: ['lambda'] under the errorAction definition (so that when
errorAction is present it must include the lambda property), and keep
additionalProperties: false; alternatively, if you intend to support multiple
action types in future, replace the properties + required approach with a oneOf
pattern similar to the redrivePolicy/sns pattern so at least one action variant
is enforced.
- Around line 136-143: The template currently always creates
TopicRulePayload.ErrorAction (event.iot.errorAction) even when no lambda is
configured and it never creates a corresponding AWS::Lambda::Permission for an
error-action lambda; update the logic in the compile step that builds
topicRuleResource (symbols: event.iot.errorAction,
topicRuleResource.Properties.TopicRulePayload.ErrorAction) so that ErrorAction
is only set when event.iot.errorAction.lambda exists, and when you detect an
error-action lambda generate a matching permission resource (same pattern used
for the main action's AWS::Lambda::Permission resource) granting
iot.amazonaws.com invoke rights to that lambda ARN (or alternatively add a
schema warning if you cannot create a static/resolvable ARN); ensure the
permission resource uses a unique logical ID analogous to the existing
permission resource generation code.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 9d1c6359-7a86-47a3-8f5f-86d07adb1045
⛔ Files ignored due to path filters (1)
package-lock.jsonis excluded by!**/package-lock.json
📒 Files selected for processing (2)
packages/serverless/lib/plugins/aws/package/compile/events/iot.jspackages/serverless/test/unit/lib/plugins/aws/package/compile/events/iot.test.js
- Add required: ['lambda'] to errorAction schema to prevent empty objects - Only create ErrorAction when event.iot.errorAction.lambda exists - Generate AWS::Lambda::Permission for error-action lambda - Update tests to verify error action Lambda permission creation
Description
This PR adds support for error action configuration on AWS IoT topic rules. This allows users to specify a Lambda function that will be invoked when the IoT rule fails to execute.
Closes #11642
Changes
errorActionproperty to the IoT event schemaerrorAction.lambda.functionArnto specify the error handling Lambda functionUsage Example
Summary by CodeRabbit
New Features
Tests