amazon-web-servicesaws-lambdaaws-cloudformationaws-sam

RequestParameters specification


I am trying to create a Restful API Gateway (AWS::ApiGateway::RestApi) integrated with a lambda function. I'd also like to call it asynchronously (that is when it called like /foobar it triggers the lambda function and returns immediately, while the lambda function still works on the background).

I have create necessary resources but couldn't manage the "asynchronous" part.

My method looks like this:

ApiGatewayPostMethod:
    Type: AWS::ApiGateway::Method
    Properties:
      AuthorizationType: NONE
      HttpMethod: POST
      OperationName: bailiffs
      ResourceId: !Ref ApiGatewayResource
      RestApiId: !Ref ApiGatewayRestApi
      Integration:
        Type: AWS
        IntegrationHttpMethod: POST
        RequestParameters:
          method.request.header.InvocationType: 'Event'
        RequestTemplates:
          "application/json": "{\"statusCode\": 200}"
        Uri: !Sub "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${WorkerFunction.Arn}/invocations"  
        IntegrationResponses:
            - StatusCode: 200      
      MethodResponses:
        - StatusCode: 200

When I run the aws cloudformation deploy, it fails with the following error:

Resource handler returned message: "Invalid mapping expression specified: Validation
Result: warnings : [], errors : [Invalid mapping expression specified: Event, Invalid mapping expression specified: method.request.header.InvocationType]
(Service: ApiGateway, Status Code: 400, Request ID: 5559ff9f-ba8e-4400-ba89-6978b66ca933)"
(RequestToken: 3b398fd2-2e7a-b78a-6a2e-2257e8882267, HandlerErrorCode: InvalidRequest)

I understand there is something wrong with the following part:

RequestParameters:
  method.request.header.InvocationType: 'Event'

Does anyone have any idea about thе correct key-values?


Solution

  • After many trials I finally managed to come up with something. It should look like following:

    ApiGatewayPostMethod:
        Type: AWS::ApiGateway::Method
        Properties:
          AuthorizationType: NONE
          HttpMethod: POST
          OperationName: bailiffs
          ResourceId: !Ref ApiGatewayResource
          RestApiId: !Ref ApiGatewayRestApi
          # !!!!!! This line is added
          RequestParameters:
            method.request.header.X-Amz-Invocation-Type: true
          Integration:
            Type: AWS
            PassthroughBehavior: WHEN_NO_TEMPLATES
            IntegrationHttpMethod: POST
            RequestParameters:
              # !!!!! This line is updated
              integration.request.header.X-Amz-Invocation-Type: "'Event'"
            RequestTemplates:
              "application/json": "{\"statusCode\": 200}"
            Uri: !Sub "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${WorkerFunction.Arn}/invocations"  
            IntegrationResponses:
                - StatusCode: 200      
          MethodResponses:
            - StatusCode: 200
    

    I'd still be glad to see experts' approach to this.