Payload Flattening in Lambda Invocation via Topic Rule

0

We're encountering an issue with the payload received by a Lambda function invoked through a topic rule. It appears that when the first field of the JSON sent is an object, this object gets flattened. However, when we change the order of the fields so that a string field is at the top, everything functions correctly.

Testing directly via AWS CLI shows that the Lambda operates as expected, suggesting the issue lies with the topic rule.

Below are the details of our Lambda function and topic rule setup:

# Lambda invoked by the topic rule
Test1Lambda:
    Type: AWS::Serverless::Function
    Properties:
        Handler: test1.handler
        CodeUri: ../iot-handlers/
        Layers:
            - !Ref BackendLayer
        Role: !GetAtt ThingRole.Arn
Test1LambdaLogGroup:
    Type: AWS::Logs::LogGroup
    Properties:
        LogGroupName: !Sub /aws/lambda/${Test1Lambda}
        RetentionInDays: !Ref LogsRetentionTime

# Topic rule
Test1TopicRule:
    Type: AWS::IoT::TopicRule
    Properties:
        RuleName: !Ref Test1TopicRuleName
        TopicRulePayload:
            Sql: SELECT *, topic() as topic FROM '/test1/#'
            Actions:
                - Lambda:
                        FunctionArn: !GetAtt Test1Lambda.Arn
            Description: 'A topic rule'
            RuleDisabled: false

Additionally, our Lambda handler includes a console log as the first line:

export const handler = async (event: any): Promise<null> => {
      console.log(event)
      // ...
      return null;
};

We've conducted tests to illustrate the problem:

Object as first field. Enter image description here

String as first field Enter image description here

Could this issue stem from our code, or might it be an AWS bug?

Thanks in advance for your insights!

2 Answers
1

To answer your question, we require details that are non-public information. Please open a support case with AWS using the following Link.[https://console.aws.amazon.com/support/home#/case/create]

AWS
answered 3 months ago
  • I just opened a support ticket. Thanks

0
Accepted Answer

The problem was we were using the default SQL engine version (2015-10-08), which doesn't support nested object queries.

After updating our topic rule definition to use a newer version (2016-03-23), it started working as expected.

# Topic rule
Test1TopicRule:
    Type: AWS::IoT::TopicRule
    Properties:
        RuleName: !Ref Test1TopicRuleName
        TopicRulePayload:
            AwsIotSqlVersion: '2016-03-23'
            Sql: SELECT *, topic() as topic FROM '/test1/#'
            Actions:
                - Lambda:
                        FunctionArn: !GetAtt Test1Lambda.Arn
            Description: 'A topic rule'
            RuleDisabled: false
Juanma
answered 3 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions