1
votes

I'm trying to create a AWS Lambda with the serverless framework. The Lambda is triggered through an AWS IoT Topic Rule. In case the execution of the Rule fails I want to have an error action executed. The entire configuration should take place within the serverless.yml.

As far as I can tell from the documentation there is no option to describe an errorAction for an iot event:

functions:
  foobar:
    events:
      - iot:
          errorAction: ?

It is possible though to define a Cloud Formation resource with an ErrorAction inside the serverless.yml:

resources:
  Resources:
     FoobarIotTopicRule1:
       Type: AWS::IoT::TopicRule
       Properties:
           ErrorAction:
             Republish:
               RoleArn: arn:aws:iam::1234567890:role/service-role/iot_execution_role
               Topic: FAILURE

But then I don't know how to link the resource to act as a trigger of the Lambda function.

functions:
  foobar:
    handler: index.handler
    events:
      - iot:
          name: iot_magic_rule
          sql: "SELECT * FROM 'my/dedicated/topic'"
          enabled: true
          sqlVersion: '2016-03-23'

resources:
  Resources:
     FoobarIotTopicRule1:
       Type: AWS::IoT::TopicRule
       Properties:
         RuleName: iot_magic_rule
         TopicRulePayload:
           AwsIotSqlVersion: '2016-03-23'
           RuleDisabled: false
           Sql: "SELECT * FROM 'my/dedicated/topic'"
           ErrorAction:
             Republish:
               RoleArn: arn:aws:iam::1234567890:role/service-role/iot_execution_role
               Topic: FAILURE

With the above configuration, trying to deploy on AWS fails as Cloud Formation tries to create the AWS IoT Topic Rule twice. Once for the definition in events and once as the defined resource FoobarIoTTopicRule1.

EDIT1

Defining the Lambda action inside the IoTTopicRule resource, creates the rule as intended, with Lambda action and error event. Unfortunately the rule does not show up as a trigger within the Lambda.

1

1 Answers

0
votes

To be able to define an AWS IoT Topic Rule with an ErrorAction that will also show up as a trigger event on AWS Lambda, the configuration should look somewhat like this:

functions:
  foobar:
    handler: index.handler

resources:
  Resources:
     FoobarIotTopicRule1:
       Type: AWS::IoT::TopicRule
       Properties:
         RuleName: iot_magic_rule
         TopicRulePayload:
           AwsIotSqlVersion: '2016-03-23'
           RuleDisabled: false
           Sql: "SELECT * FROM 'my/dedicated/topic'"
           Actions:
             - Lambda:
                 FunctionArn: { "Fn::GetAtt": ['FoobarLambdaFunction', 'Arn']}
           ErrorAction:
             Republish:
               RoleArn: arn:aws:iam::1234567890:role/service-role/iot_execution_role
               Topic: FAILURE
     FoobarLambdaPermissionIotTopicRule1:
      Type: AWS::Lambda::Permission
      Properties: 
        FunctionName: { "Fn::GetAtt": [ "FoobarLambdaFunction", "Arn" ] }
        Action: lambda:InvokeFunction
        Principal: { "Fn::Join": ["", [ "iot.", { "Ref": "AWS::URLSuffix" } ]]}
        SourceArn: 
          Fn::Join: 
            - ""
            - - "arn:"
              - "Ref": "AWS::Partition"
              - ":iot:"
              - "Ref": "AWS::Region"
              - ":"
              - "Ref": "AWS::AccountId"
              - ":rule/"
              - "Ref": "FoobarIotTopicRule1"