Search code examples
amazon-web-servicesaws-lambdaaws-api-gatewayaws-sam

How to configure asynchronous lambda invocation via API Gateway using SAM?


I have configured a lambda proxy integration with API Gateway using SAM specification and am invoking the lambda asynchronously by passing X-Amz-Invocation-Type: "'Event'" header

  ApiGateway:
      Type: AWS::Serverless::Api
      Properties:
        StageName: prod
        DefinitionBody:
          swagger: "2.0"
          info:
            title:
              Ref: AWS::StackName
            description: API endpoint for invoking LambdaFunction
            version: 1.0.0
          paths:
            /items:
              post:
                consumes: ["application/json"]
                produces: ["application/json"]
                responses: {}
                x-amazon-apigateway-integration:
                  type: "aws_proxy"
                  uri:
                    Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LambdaFunction.Arn}/invocations
                  passthroughBehavior: "when_no_match"
                  httpMethod: "POST"
                  requestParameters:
                      integration.request.header.X-Amz-Invocation-Type: "'Event'"

The problem is that lambda is returning empty response(invoked async) so API Gateway is throwing following error

Wed Nov 14 08:03:14 UTC 2018 : Execution failed due to configuration error: Malformed Lambda proxy response Wed Nov 14 08:03:14 UTC 2018 : Method completed with status: 502

Is this behaviour expected? Do I have to define responses explicitly? I do not want to throw 200 always because I also want to send bad request and unauthorized errors as well.What's the solution to avoid this problem?


Solution

  • AWS Api Gateway doesn't allow this.

    This has to be unchecked to be very specific when using async integration. enter image description here

    Plus you don’t need to actually pass the X-Amz-Invocation-Type header in the request.

    Async invocation, unfortunately, does not work with the lambda-proxy (aws_proxy) integration type.