API Gateway HTTP Proxy integration with serverless-offline (NOT Lambda Proxy)

瘦欲@ 提交于 2019-12-31 02:24:07

问题


I am trying to use serverless-offline to develop / simulate my API Gateway locally. My API gateway makes liberal use of the HTTP proxy integrations. The production Resource looks like this:

I have created a serverless-offline configuration based on a few documents and discussion which say that it is possible to define an HTTP Proxy integration using Cloud Formation configuration:

  • httpProxyWithApiGateway.md - Setting an HTTP Proxy on API Gateway by using Serverless framework.
  • Setting an HTTP Proxy on API Gateway (official Serverless docs: API Gateway)

I have adapted the above two configuration examples for my purposes, see below.

Have any tips, for what I might be doing wrong here?

plugins:
  - serverless-offline

service: company-apig
provider:
  name: aws
  stage: dev
  runtime: python2.7

resources:
  Resources:

    # Parent APIG RestApi
    ApiGatewayRestApi:
      Type: AWS::ApiGateway::RestApi
      Properties:
        Name: company-apig
        Description: 'The main entry point of the APIG'

    # Resource /endpoint
    EndpointResource:
      Type: AWS::ApiGateway::Resource
      Properties:
        ParentId:
          Fn::GetAtt:
            - ApiGatewayRestApi
            - RootResourceId
        PathPart: 'endpoint'
        RestApiId:
          Ref: ApiGatewayRestApi

    # Resource /endpoint/{proxy+}
    EndpointProxyPath:
      Type: AWS::ApiGateway::Resource
      Properties:
        ParentId:
          Ref: EndpointResource
        PathPart: '{proxy+}'
        RestApiId:
          Ref: ApiGatewayRestApi

    # Method ANY /endpoint/{proxy+}
    EndpointProxyAnyMethod:
      Type: AWS::ApiGateway::Method
      Properties:
        AuthorizationType: NONE
        HttpMethod: ANY
        Integration:
          IntegrationHttpMethod: ANY
          Type: HTTP_PROXY
          Uri: http://endpoint.company.cool/{proxy}
          PassthroughBehavior: WHEN_NO_MATCH
        MethodResponses:
          - StatusCode: 200
        ResourceId:
          Ref: EndpointProxyPath
        RestApiId:
          Ref: ApiGatewayRestApi

For the above configuration, I get this output. Apparently, the configuration registers no routes at all.

{
  "statusCode":404,
  "error":"Serverless-offline: route not found.",
  "currentRoute":"get - /endpoint/ping",
  "existingRoutes":[]
}

Related: I am also attempting to solve the same problem using aws-sam, at the following post - API Gateway HTTP Proxy integration with aws-sam (NOT Lambda Proxy)


回答1:


By default serverless-offline doesn't parse your resources for endpoints, enable it via custom config.

custom:
  serverless-offline:
    resourceRoutes: true

Ends up serving:

Serverless: Routes defined in resources:
Serverless: ANY /endpoint/{proxy*} -> http://endpoint.company.cool/{proxy}

Serverless: Offline listening on http://localhost:3000

Documentation



来源:https://stackoverflow.com/questions/55129644/api-gateway-http-proxy-integration-with-serverless-offline-not-lambda-proxy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!