1
votes

I am using Serverless framework.

I have written a rule for the MQTT topic. When a message is published on a MQTT topic by the device, I extract the required fields from the message payload and trigger a Lambda functions. This works perfectly file.

Here is the serverless configuration snippet for the same.

LambdafunctionName:
        handler: bin/handlers/LambdafunctionName
        package:
          exclude:
             - "**/**"
          include:
             - ./bin/handlers/LambdafunctionName
        events:
          - iot:
              name: "iotRuleName"
              sql: "select field1, field2 from '/my/mqtt/topicname'"
              description: "Rule description"

I want to convert this synchronous event processing to Asynchronous mode,.

From the AWS console, I have created another rule for IoT, wherein the message coming to MQTT broker is published to the SQS queue. Then I have configured a Lambda function which gets triggered when a message is published to the SQS queue.

This also works fine.

I want to configure this IoT Rule, SQS Queue and the Lambda function at the time of deployment using the serverless.yml file.

Are there any sample configuration files, which provide the example of this setup? IoT Topic -> IoT rule -> SQS Queue -> Lambda invocation.

So far the example I have seen only show the configuration for SQS and lambda functions only.

https://medium.com/consulner/aws-lambda-sqs-events-with-serverless-framework-7429471ac664

1

1 Answers

1
votes

In the resources section of your serverless.yml you will need to add the CloudFormation definition of the IoT rule you created.

Something like

resources:
  Resources:
    IoTRule:
      Type: AWS::Iot::TopicRule
      Properties:
        RuleName: String
        TopicRulePayload: 
          ....

See https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-iot-topicrule.html for information on the topic rule definition for CloudFormation