Justin
Justin

Reputation: 4843

Lambda destination not being triggered on Lambda error

I have a Cloudformation stack which connects two Lambdas together via a Lambda destination config and an SQS queue.

The idea is that ErrorsFunction is fired when there is an error in HelloAddFunction.

This stack deploys fine, and HelloAddFunction works fine when I invoke it with some integer values.

I can see an error in HelloAddFunction when I invoke it with non- integer values, but no corresponding error seems to be received by ErrorsFunction.

The binding of ErrorsFunction to ErrorsQueue seems to be working - if I push a message onto ErrorsQueue via the console, it's received by ErrorsFunction.

So it feels like the Lambda destination config is somehow not working.

What am I missing here ?

TIA

AWSTemplateFormatVersion: '2010-09-09'
Outputs: {}
Parameters:
  AppName:
    Type: String
  MemorySizeSmall:
    Default: 512
    Type: Number
  RuntimeVersion:
    Default: '3.8'
    Type: String
  TimeoutShort:
    Default: 5
    Type: Number
Resources:
  HelloAddEventConfig:
    Properties:
      DestinationConfig:
        OnFailure:
          Destination:
            Fn::GetAtt:
            - ErrorsQueue
            - Arn
      FunctionName:
        Ref: HelloAddFunction
      MaximumRetryAttempts: 0
      Qualifier: $LATEST
    Type: AWS::Lambda::EventInvokeConfig
  HelloAddFunction:
    Properties:
      Code:      
        ZipFile: |
          def handler(event, context):
            x, y = int(event["x"]), int(event["y"])
            return x+y     
      Handler: index.handler
      MemorySize:
        Ref: MemorySizeSmall
      Role:
        Fn::GetAtt:
        - HelloAddRole
        - Arn
      Runtime:
        Fn::Sub: python${RuntimeVersion}
      Timeout:
        Ref: TimeoutShort
    Type: AWS::Lambda::Function
  HelloAddRole:
    Properties:
      AssumeRolePolicyDocument:
        Statement:
        - Action: sts:AssumeRole
          Effect: Allow
          Principal:
            Service: lambda.amazonaws.com
        Version: '2012-10-17'
      Policies:
      - PolicyDocument:
          Statement:
          - Action: logs:*
            Effect: Allow
            Resource: '*'
          - Action: sqs:*
            Effect: Allow
            Resource: '*'
          Version: '2012-10-17'
        PolicyName:
          Fn::Sub: hello-add-role-policy-${AWS::StackName}
    Type: AWS::IAM::Role
  ErrorsFunction:
    Properties:
      Code:
        ZipFile: |
          def handler(event, context):
            print (event)
      Handler: index.handler
      MemorySize:
        Ref: MemorySizeSmall
      Role:
        Fn::GetAtt:
        - ErrorsRole
        - Arn
      Runtime:
        Fn::Sub: python${RuntimeVersion}
      Timeout:
        Ref: TimeoutShort
    Type: AWS::Lambda::Function
  ErrorsQueue:
    Properties: {}
    Type: AWS::SQS::Queue
  ErrorsQueueBinding:
    Properties:
      EventSourceArn:
        Fn::GetAtt:
        - ErrorsQueue
        - Arn
      FunctionName:
        Ref: ErrorsFunction
    Type: AWS::Lambda::EventSourceMapping
  ErrorsRole:
    Properties:
      AssumeRolePolicyDocument:
        Statement:
        - Action: sts:AssumeRole
          Effect: Allow
          Principal:
            Service: lambda.amazonaws.com
        Version: '2012-10-17'
      Policies:
      - PolicyDocument:
          Statement:
          - Action: logs:*
            Effect: Allow
            Resource: '*'
          - Action: sqs:*
            Effect: Allow
            Resource: '*'
          Version: '2012-10-17'
        PolicyName:
          Fn::Sub: errors-role-policy-${AWS::StackName}
    Type: AWS::IAM::Role

Upvotes: 3

Views: 1080

Answers (1)

Marcin
Marcin

Reputation: 238081

Your template is fine, but lambda destinations are for asynchronous invocations of your function only. So you have to make such an invocation, which can be done using AWS CLI (--invocation-type Event). AWS Console is synchronous only.

aws lambda invoke --function-name <HelloAddFunctionnaem> --invocation-type Event --payload '{"x": 3, "y": "SSS"}' /dev/stdout

Upvotes: 3

Related Questions