Skip to content

feat: Add support for error action on IoT topic rule#13423

Open
cyphercodes wants to merge 2 commits into
serverless:mainfrom
cyphercodes:feat/iot-error-action
Open

feat: Add support for error action on IoT topic rule#13423
cyphercodes wants to merge 2 commits into
serverless:mainfrom
cyphercodes:feat/iot-error-action

Conversation

@cyphercodes

@cyphercodes cyphercodes commented Mar 25, 2026

Copy link
Copy Markdown

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

  • Added errorAction property to the IoT event schema
  • Added support for errorAction.lambda.functionArn to specify the error handling Lambda function
  • Added unit test for the new functionality

Usage Example

functions:
  myFunction:
    handler: handler.main
    events:
      - iot:
          sql: "SELECT * FROM 'my/topic'"
          errorAction:
            lambda:
              functionArn: arn:aws:lambda:us-east-1:123456789012:function:errorHandler

Summary by CodeRabbit

  • New Features

    • Support for configuring IoT rule error actions: specify a Lambda function ARN to handle rule failures; compiled deployment includes the necessary permission for IoT to invoke that Lambda.
  • Tests

    • Added unit tests to validate error-action configuration and correct CloudFormation compilation (including the error-action invocation permission).

- Add errorAction property to IoT event schema
- Handle errorAction.lambda.functionArn in compileIoTEvents
- Add unit test for errorAction functionality

Fixes serverless#11642
@Mmarzex

Mmarzex commented Mar 25, 2026

Copy link
Copy Markdown
Contributor

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues
Code Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@coderabbitai

coderabbitai Bot commented Mar 25, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 445e0e24-920c-4f8a-88f3-58c9abac9d25

📥 Commits

Reviewing files that changed from the base of the PR and between 53c73fc and 902f707.

📒 Files selected for processing (2)
  • packages/serverless/lib/plugins/aws/package/compile/events/iot.js
  • packages/serverless/test/unit/lib/plugins/aws/package/compile/events/iot.test.js
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/serverless/test/unit/lib/plugins/aws/package/compile/events/iot.test.js
  • packages/serverless/lib/plugins/aws/package/compile/events/iot.js

📝 Walkthrough

Walkthrough

Added optional errorAction support to AWS IoT event schema and CloudFormation compilation: when event.iot.errorAction.lambda.functionArn is provided, the TopicRulePayload includes ErrorAction.Lambda.FunctionArn and an accompanying AWS::Lambda::Permission resource is generated.

Changes

Cohort / File(s) Summary
IoT Event Schema & Compilation
packages/serverless/lib/plugins/aws/package/compile/events/iot.js
Added errorAction schema (errorAction.lambda.functionArn) and updated TopicRulePayload compilation to include ErrorAction when configured. Also generates a separate AWS::Lambda::Permission resource for the error-action Lambda.
IoT Event Unit Tests
packages/serverless/test/unit/lib/plugins/aws/package/compile/events/iot.test.js
Added test asserting TopicRule payload contains ErrorAction: { Lambda: { FunctionArn } } and that a corresponding AWS::Lambda::Permission resource (with iot.amazonaws.com principal and correct FunctionName) is created.

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
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 I nibbled code by moonlit beam,
ErrorAction stitched into the stream.
If IoT rules take a tumble,
A Lambda saves the humble.
Hops of joy — CI's green gleam! 🥕✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the main change: adding support for error action on IoT topic rules, which aligns with the changeset.
Linked Issues check ✅ Passed The PR successfully implements all coding requirements from issue #11642: errorAction schema property, Lambda ARN support, CloudFormation template generation with ErrorAction, and Lambda::Permission creation for error-handling.
Out of Scope Changes check ✅ Passed All changes are directly within scope: schema extension for errorAction, CloudFormation compilation logic, Lambda permission generation, and corresponding unit tests. No unrelated modifications detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 8905d7a and 53c73fc.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (2)
  • packages/serverless/lib/plugins/aws/package/compile/events/iot.js
  • packages/serverless/test/unit/lib/plugins/aws/package/compile/events/iot.test.js

Comment thread packages/serverless/lib/plugins/aws/package/compile/events/iot.js
Comment thread packages/serverless/lib/plugins/aws/package/compile/events/iot.js Outdated
- 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for error action on iot topic rule

2 participants