【问题标题】:How to create an API Gateway HTTP Proxy without any lambda function如何在没有任何 lambda 函数的情况下创建 API Gateway HTTP 代理
【发布时间】:2018-07-23 22:10:25
【问题描述】:

我使用无服务器框架并希望部署 API Gateway HTTP 代理,但我没有与 API Gateway 连接的 Lambda 函数。

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

#    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 Gateway HTTP 代理?

谢谢

【问题讨论】:

  • 我能问一下你想要达到什么目的吗?
  • 我有一个正在运行的 AppSync 服务,但该服务需要身份验证(最低要求是 API 密钥)。我不乐意总是将 ApiKey 传递给我的应用程序,并希望创建一个 API Gateway 代理,将 API 密钥传递给 AppSync。这样我就不需要每年都更改 ApiKey(例如在移动应用中)。

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


【解决方案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

【讨论】:

    猜你喜欢
    • 2020-04-15
    • 1970-01-01
    • 2022-07-23
    • 1970-01-01
    • 2022-06-10
    • 2019-08-20
    • 2021-05-16
    • 1970-01-01
    • 2022-12-31
    相关资源
    最近更新 更多