3

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

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

Yep exactly - just figured it out - was pinging Lambda with invocation-type RequestResponse and not Event - thx for the tip!
what's the best pattern for handling sync lambda errors similar to above, eg a lambda bound to an sqs queue? @marcin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.