如何创建没有任何Lambda函数的API网关HTTP代理

时间:2018-07-22 10:15:22

标签: amazon-web-services serverless-framework serverless

我使用无服务器框架,想部署API网关HTTP代理,但是我没有与API网关连接的Lambda函数。

我在互联网上找到了这个,但是这个示例需要一个lambda函数连接到API网关

#    ProxyResource:
#      Type: AWS::ApiGateway::Resource
#      Properties:
#        ParentId:
#          Fn::GetAtt:
#            - ApiGatewayRestApi # our default Rest API logical ID
#            - RootResourceId
#        PathPart: serverless # the endpoint in your API that is set as proxy
#        RestApiId:
#          Ref: ApiGatewayRestApi
#    ProxyMethod:
#      Type: AWS::ApiGateway::Method
#      Properties:
#        ResourceId:
#          Ref: ProxyResource
#        RestApiId:
#          Ref: ApiGatewayRestApi
#        HttpMethod: GET # the method of your proxy. Is it GET or POST or ... ?
#        MethodResponses:
#          - StatusCode: 200
#        Integration:
#          IntegrationHttpMethod: POST
#          Type: HTTP
#          Uri: http://serverless.com # the URL you want to set a proxy to
#          IntegrationResponses:
#            - StatusCode: 200

如果我部署了该软件,则会收到错误消息:

The CloudFormation template is invalid: Template format error: Unresolved resource dependencies [ApiGatewayRestApi] in the Resources block of the template

是否可以仅部署API网关HTTP代理?

谢谢

1 个答案:

答案 0 :(得分:1)

我想出了在无服务器中没有任何lambda函数的情况下如何创建API网关的方法。我只需要将此添加到资源并将Ref:ApiGatewayRestApi更改为Ref:ProxyApi

resources:
  Resources:
    ProxyApi:
      Type: AWS::ApiGateway::RestApi
      Properties:
        Name: ApiGateway

要满足我在不使用任何ApiKey的情况下使用AppSync的要求,可以使用以下几行:

    ProxyApi:
      Type: AWS::ApiGateway::RestApi
      Properties:
        Name: AppSync Graph Proxy

    ProxyResource:
      Type: AWS::ApiGateway::Resource
      Properties:
        ParentId:
          Fn::GetAtt:
            - ProxyApi # our default Rest API logical ID
            - RootResourceId
        PathPart: graphql # the endpoint in your API that is set as proxy
        RestApiId:
          Ref: ProxyApi
    ProxyMethod:
      Type: AWS::ApiGateway::Method
      Properties:
        ResourceId:
          Ref: ProxyResource
        RestApiId:
          Ref: ProxyApi
        AuthorizationType: NONE
        HttpMethod: ANY # the method of your proxy. Is it GET or POST or ... ?
        MethodResponses:
          - StatusCode: 200
        Integration:
          IntegrationHttpMethod: POST
          Type: HTTP
          Uri: { Fn::GetAtt: [GraphQlApi, GraphQLUrl] } # the URL you want to set a proxy to
          IntegrationResponses:
            - StatusCode: 200
          RequestParameters:
            "integration.request.header.x-api-key": "stageVariables.API_KEY"
    ProxyDeployment:
      Type: AWS::ApiGateway::Deployment
      DependsOn: "ProxyMethod"
      Properties:
        RestApiId:
          Ref: ProxyApi

    ProxyStage:
      Type: AWS::ApiGateway::Stage
      Properties:
        StageName: ${opt:stage, self:provider.stage}
        RestApiId:
          Ref: ProxyApi
        DeploymentId:
          Ref: ProxyDeployment
        Variables:
          API_KEY: { Fn::GetAtt: [GraphQlApiKeyDefault, ApiKey] }

为此,您需要在无服务器配置中工作且已配置的serverless-appsync-plugin