【问题标题】:Creating Non-Proxy API Gateway for Lambda Event using SAM Template使用 SAM 模板为 Lambda 事件创建非代理 API 网关
【发布时间】:2021-11-17 11:34:41
【问题描述】:

我按照 AWS 教程使用 SAM 模板设置 Lambda + API Gateway。 但是在 lambda 模板下定义的事件会创建一个代理集成。 我遵循本教程是因为我想为我的一个项目设置类似的设置。 我需要针对该特定用例的非代理集成。因为我必须将 xml 格式返回给客户端,而这只能通过修改集成响应来完成。 但在代理 API 集成响应中无法修改。 我搜索了很多,但找不到答案。 现在 template.yaml 看起来像这样

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  callforward

  Sample SAM Template for callforward

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 3

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.8
      Events:
        HelloWorld:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /hello
            Method: get

Outputs:
  # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
  # Find out more about other implicit resources you can reference within SAM
  # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
  HelloWorldApi:
    Description: "API Gateway endpoint URL for Prod stage for Hello World function"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
  HelloWorldFunction:
    Description: "Hello World Lambda Function ARN"
    Value: !GetAtt HelloWorldFunction.Arn
  HelloWorldFunctionIamRole:
    Description: "Implicit IAM Role created for Hello World function"
    Value: !GetAtt HelloWorldFunctionRole.Arn

【问题讨论】:

    标签: amazon-web-services aws-lambda amazon-cloudformation aws-api-gateway


    【解决方案1】:

    您寻求的是 API Gateway 的“映射模板”功能。 不幸的是,在 AWS SAM 中没有直接的方法。

    但是有一种方法可以通过利用 AWS SAM 中的开放 API 支持来实现这一点,该 AWS SAM 具有 API 网关扩展的子集。 (x-amazon-API gateway-integration.requestTemplates 对象)

    RestApi:
    Type: AWS::Serverless::Api
    Properties:
        DefinitionBody:       
            <add openAPI defintaion>
    

    API Gateway Extension

    OpenAPI definitions of sample API integrated with a Lambda function

    【讨论】:

      【解决方案2】:

      更新: 所以如果有人偶然发现这个愚蠢的问题,这是给你的。 带有 Lambda 的代理 API 网关的全部意义在于只返回 lambda 返回的任何内容。

      我能够使用代理 API 本身来满足我的要求。 以这个帖子为起点,https://forums.aws.amazon.com/thread.jspa?messageID=649836

      我被要求将这个东西转移到一个新的 AWS 账户,看到我之前写的代码,畏缩并重写它。 在写这篇文章的时候,我对我为什么首先问这个愚蠢的问题感到畏缩。

      【讨论】:

        猜你喜欢
        • 2020-12-21
        • 2017-11-06
        • 1970-01-01
        • 1970-01-01
        • 2018-04-15
        • 1970-01-01
        • 2019-07-29
        • 2019-06-20
        • 2019-08-11
        相关资源
        最近更新 更多