dougmoscrop / serverless-http

Use your existing middleware framework (e.g. Express, Koa) in AWS Lambda 🎉
Other
1.72k stars 166 forks source link

Feature request: Support for AWS API Gateway payload version 2 #146

Open danyfedorov opened 4 years ago

danyfedorov commented 4 years ago

Link with info about version 1 vs version 2.

https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html

mpvosseller commented 4 years ago

If useful to others: until payload version 2 is supported we can get this to work by specifying version 1 like this:

  ApiFunction:
    Type: AWS::Serverless::Function
    Properties:
      # ...
      Events:
        Http:
          Type: HttpApi
          Properties:
            PayloadFormatVersion: '1.0'
tehnrd commented 4 years ago

This is the file that needs changes, ya? https://github.com/dougmoscrop/serverless-http/blob/master/lib/provider/aws/create-request.js

If someone can confirm, I can take a stab at this.

tehnrd commented 4 years ago

I believe the example from @mpvosseller above is for SAM.

If using the serverless framework, the following is a workaround:

resources:
  Resources:
    Overrides:
      Type: AWS::ApiGatewayV2::ApiGatewayManagedOverrides
      Properties: 
        ApiId: 
          Ref: "HttpApi"
        Integration: 
          PayloadFormatVersion: '1.0'
Frans-L commented 4 years ago

@TehNrd

With your workaround, I got an error: The CloudFormation template is invalid: Template format error: Unresolved resource dependencies [HttpApi] in the Resources block of the template

Do you have any guess how this could be solved?

tehnrd commented 4 years ago

@Frans-L What does the rest of your serverless.yml file look like?

Frans-L commented 4 years ago

@TehNrd

Thanks for helping out. Here is the whole serverless.yml

service:
  name: test

app: something-test
org: something

custom:
  webpack:
    webpackConfig: ./webpack.config.js
    includeModules: true

plugins:
  - serverless-webpack
  - serverless-offline

provider:
  name: aws
  runtime: nodejs12.x
  region: eu-west-3
  apiGateway:
    minimumCompressionSize: 1024
  environment:
    AWS_NODEJS_CONNECTION_REUSE_ENABLED: 1

functions:
  app:
    handler: src/app.handler
    events:
      - http: ANY /
      - http: "ANY /{proxy+}"
tehnrd commented 4 years ago

@Frans-L You aren't using the new httpApi event. What you have there will create a normal REST API. need to change http to httpApi Once you do this you might also need to change the paths as I'm not sure those are valid for httpApi. I think you only need httpApi: '*'

And if you have an Custom Domain sitting in front of this it also needs to be a Regional domain name.

https://serverless.com/framework/docs/providers/aws/events/http-api/

tehnrd commented 4 years ago

Hmm, so my workaround above isn't working anymore and I am now receiving the following error on deployment.

An error occurred: Overrides - ApiGatewayManagedOverrides can only be associated to APIs created via quick create (Service: null; Status Code: 0; Error Code: null; Request ID: null) (Service: null; Status Code: 404; Error Code: BadRequestException; Request ID: null).

Not sure what changed, maybe something internal to the way serverless framework does deploy.

I started on a PR fix but stopped after finding the workaround, I'll get PR submitted soon.

tehnrd commented 4 years ago

Hmm, maybe I'm going crazy. Looks like Serverless Framework is setting 1.0 payload and has been for a while.

https://github.com/serverless/serverless/blob/master/lib/plugins/aws/package/compile/events/httpApi/index.js#L434

Not sure how mine got set to 2.0 for my deployment but would still be great it if both were supported so I'll continue on the PR.

tehnrd commented 4 years ago

PR Submitted! https://github.com/dougmoscrop/serverless-http/pull/150

Definitely open to feedback. :-)

niteshgrg commented 4 years ago

@TehNrd Could this be also due different payload version support. Also how can i check which setting mine api gateway is using

https://github.com/dougmoscrop/serverless-http/issues/151

tehnrd commented 4 years ago

@niteshgrg Not sure if that is related, but here is where you check payload version.

Screen Shot 2020-04-19 at 10 52 29 AM

There are also some CORS specific settings you may need to edit as well for your particular issue.

AshUK commented 3 years ago

this issue can be closed now ?

MarcoLeongDev commented 2 years ago

I have discovered another scenario for the error ApiGatewayManagedOverrides can only be associated to APIs created via quick create. This can happen whenTarget is missed AWS::ApiGatewayV2::Api, the result is ApiGatewayManagedOverrides no knowing what to do and throws the above error

ciekawy commented 1 year ago

curious why no serverless-http in `plugins' section?