Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for asynchronous lambda invocation with integration type AWS #4862

Closed
bsdkurt opened this Issue Mar 28, 2018 · 13 comments

Comments

Projects
None yet
10 participants
@bsdkurt
Copy link
Contributor

bsdkurt commented Mar 28, 2018

This is a Feature Proposal

Description

API Gateway supports asynchronous lambda invocation when integration type is AWS. Described here:
https://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-lambda.html
Currently it is not possible to configure this in serverless directly and requires a resource override to enable. This feature proposal is to add support for integration type AWS, asynchronous lambda invocation without using an resource override.

  • What is the use case that should be solved. The more detail you describe this in the easier it is to understand for us.

Asynchronous lambda invocation allows for a quick response back (e.g. 202 Accepted) while allowing the lambda to continue running. This would be desired if you wish to kick off a lambda that runs longer then the API GW 30 second timeout, or if the application needs a quick response without the need for direct success for failure of the longer running lambda.

  • If there is additional config how would it look

Add new integration type AWS_ASYNC that configures the X-Amz-Invocation-Type correctly:

  lambda:
    handler: index.handler
    events:
      - http:
          path: users
          method: post
          integration: AWS_ASYNC
  • Cloud Formation Information

AWS_ASYNC would set the integration type to AWS and add "integration.request.header.X-Amz-Invocation-Type": "'Event'" to Integration { RequestParameters: {} } similar to as follows:

          "ApiGatewayMethodUsersPost": {
            "Type": "AWS::ApiGateway::Method",
            "Properties": {
              "HttpMethod": "POST",
              "RequestParameters": {
              },
              "Integration": {
                "IntegrationHttpMethod": "POST",
                "Type": "AWS",
                "RequestParameters": {
                  "integration.request.header.X-Amz-Invocation-Type": "'Event'"
                }

Additional Data

  • Serverless Framework Version you're using:
    serverless/serverless as of 4/28/2018
  • Operating System:
    Ubuntu 16.04
  • Stack Trace:
    N/A
  • Provider Error messages:
    N/A
@bsdkurt

This comment has been minimized.

Copy link
Contributor Author

bsdkurt commented Mar 28, 2018

@jjkirby Developed the following resource override to use as a work-around until the feature proposal is complete:

resources:
   Resources:
     ApiGatewayMethodUsersPost:
       Type: AWS::ApiGateway::Method
       Properties:
         HttpMethod: POST
         Integration:
           IntegrationHttpMethod: POST
           Type: "AWS"
           RequestParameters:
              "integration.request.header.X-Amz-Invocation-Type": "'Event'"

@jjkirby If you find there is a need for additional resource overrides please add that information here. Thanks

@jjkirby

This comment has been minimized.

Copy link

jjkirby commented Mar 28, 2018

Or just add an element like async which does the header settings under the hood such as:

lambda:
    handler: index.handler
    events:
      - http:
          path: users
          method: post
          integration: AWS
          async: true
@HyperBrain

This comment has been minimized.

Copy link
Member

HyperBrain commented Mar 28, 2018

@jjkirby @bsdkurt As the asynchronous invocation is only valid with AWS type integrations, it could be encoded into a new integration type: AWS_ASYNC. Having a separate async property could make people believe that it is independent from the integration and should do something in any case.

@bsdkurt

This comment has been minimized.

Copy link
Contributor Author

bsdkurt commented Mar 28, 2018

I've updated the feature proposal with suggestions from above.

@defionscode

This comment has been minimized.

Copy link

defionscode commented Apr 27, 2018

My org is also in need of this feature. What help is needed?

@bsdkurt

This comment has been minimized.

Copy link
Contributor Author

bsdkurt commented Apr 27, 2018

Hi @defionscode. I wrote the feature proposal after helping jjkirby on slack with how to figure out what resource override was needed to get this to work. I am not currently using asynchronous lambda's and wont have time to implement this, but thought it would be helpful to the community to document the current work-around and propose how to get this integrated.

What is needed now is someone to develop the feature with the unit tests and documentation and then make a pull request for review.

@jjkirby

This comment has been minimized.

Copy link

jjkirby commented Apr 28, 2018

I am not a Node guy but I will be willing to help (where I can) since I started all of this

@sernaton

This comment has been minimized.

Copy link

sernaton commented May 28, 2018

This would be really nice - I went to the doc trying to figure out how to set up api gateway "event" invocationType and saw that unfortunately it is not possible yet. My support to this story.

@scott2b

This comment has been minimized.

Copy link

scott2b commented Sep 2, 2018

Is there something that needs to be done other than adding the resource override to serverless.yml to get the workaround suggested by @bsdkurt to work? When I do this, I still get a timeout for requests, indicating that the function is not being handled asynchronously.

@jjkirby

This comment has been minimized.

Copy link

jjkirby commented Sep 2, 2018

@scottb can you post your serverless.yml file?

@jjkirby

This comment has been minimized.

Copy link

jjkirby commented Sep 2, 2018

@scott2b Here are my snippets:

My plugins:

  • serverless-pseudo-parameters
  • serverless-reqvalidator-plugin
  • serverless-aws-documentation

One of the keys is double quote around single quote on request parameter Event and WHEN-NO _MATCH on pass through
API definition:

 events:
        - http:
           path: sfNotify
           method: POST
           cors: true
           private: true
           integration: aws
           reqValidatorName: onlyBody
           documentation:
              tags:
                - notify
              summary: Post a flight event notification generated by a third party
              description: ''
              requestModels:
                 "application/json": sfNotifyPostRequest
           request:
             passThrough: WHEN_NO_MATCH
             parameters:
               headers:
                'X-Amz-Invocation-Type': "'Event'"

Then my resources:

resources:
   Resources:
     GatewayResponse:
         Type: 'AWS::ApiGateway::GatewayResponse'
         Properties:
           ResponseParameters:
             gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
           ResponseType: INVALID_API_KEY
           RestApiId:
             Ref: 'ApiGatewayRestApi'
           StatusCode: '403'
     # Using CF Request Validator
     onlyBody:
       Type: "AWS::ApiGateway::RequestValidator"
       Properties:
         Name: 'only-body'
         RestApiId:
           Ref: ApiGatewayRestApi
         ValidateRequestBody: true
         ValidateRequestParameters: false
     ApiGatewayMethodSfnotifyPost:
       Type: AWS::ApiGateway::Method
       Properties:
         HttpMethod: POST
         Integration:
           IntegrationHttpMethod: POST
           Type: "AWS"
           RequestParameters:
              "integration.request.header.X-Amz-Invocation-Type": "'Event'"

@crebstock

This comment has been minimized.

Copy link

crebstock commented Oct 15, 2018

Is there anyway to override the resource while still keeping the very easy to use Api object in the events of the lambda functions that use that specific API gateway?

"Events": {
    "MyApiGateway": {
        "Type": "Api",
         "Properties": {
             "Method": "get",
                 "Path": "/mypath",
             }
     }
},
@johnlim

This comment has been minimized.

Copy link
Contributor

johnlim commented Jan 25, 2019

Hi guys, is there any update on this issue? Thanks. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.