API网关如何与aws-sam进行HTTP代理集成(而不是Lambda代理)

7
我正在尝试使用aws-sam在本地开发/模拟我的API网关。我的API网关广泛使用HTTP代理集成。生产资源如下所示:

Screenshot of an HTTP Proxy integration on an API Gateway Resource Method

我找到的所有aws-sam示例 以及相关文档和问答,都使用Lambda集成/强烈依赖于Lambda函数作为代理资源,而不是HTTP代理集成。

是否有一种方法可以为aws-sam应用程序定义HTTP代理资源? (与Lambda代理资源相反?)

相关:

2个回答

5

是的,SAM只是一个CloudFormation转换。因此,您仍然可以像往常一样创建传统的CloudFormation对象。您也可以尝试将其附加到Serverless :: API上,但我对此不太有信心。

Resource:
  Api:
    Type: 'AWS::ApiGateway::RestApi'
    Properties:
      Description: A test API
      Name: MyRestAPI

    Type: 'AWS::ApiGateway::Resource'
    Properties:
      ParentId: !GetAtt Api.RootResourceId
      RestApiId: !Ref Api
      PathPart: '{proxy+}'

或者可能(未经测试):

Resource:
  Api:
    Type: 'AWS::Serverless::Api'
    Properties:
      Description: A test API
      Name: MyRestAPI

    Type: 'AWS::ApiGateway::Resource'
    Properties:
      ParentId: !GetAtt Api.RootResourceId
      RestApiId: !Ref Api
      PathPart: '{proxy+}'


2

我测试了J.A的第二个建议,它有效!谢谢。

  MyGalleryApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: prod

  GetImagesResource:
    Type: AWS::ApiGateway::Resource
    Properties:
      RestApiId: !Ref MyGalleryApi
      ParentId: !GetAtt MyGalleryApi.RootResourceId
      PathPart: getImages

  GetImagesMethod:
    Type: AWS::ApiGateway::Method
    Properties:
      RestApiId: !Ref MyGalleryApi
      ResourceId: !Ref GetImagesResource
      HttpMethod: GET
      AuthorizationType: AWS_IAM
      Integration:
        Type: HTTP_PROXY
        IntegrationHttpMethod: GET
        Uri: https://proxy.com/path/getImages

您可以通过在函数定义中使用RestApiId,将HTTP_PROXY和LAMBA_PROXY组合在一个模板中:
  NonceFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: src/handlers/nonce.handler
      Role: !GetAtt MyGalleryLambdaExecutionRole.Arn
      Architectures:
        - x86_64
      MemorySize: 256
      Description: Nonce function to start authentication
      Policies:
        - DynamoDBCrudPolicy:
            TableName: !Ref UsersTable
      Environment:
        Variables:
          USERTABLE_NAME: !Ref UsersTable
      Events:
        ApiEvent:
          Type: Api
          Properties:
            Path: /nonce
            Method: GET
            RestApiId: !Ref MyGalleryApi
            RequestParameters:
              - method.request.path.address:
                  Required: True

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接