0

我正在使用 AWS SAM 在本地测试我的 Api 网关和 lambda。执行sam local start-api和调用 lambda 时,我希望使用event2.0格式而不是 1 版。

我正在使用 CDKHttpApi构造,@aws-cdk/aws-apigatewayv2因此现在我的本地测试和部署的内容之间存在不一致。

我是 Sam config 的新手,我的template.yml文件是:

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Resources:
  MyFunction:
  Type: 'AWS::Serverless::Function'
  Properties:
    Handler: index.handler
    Runtime: nodejs14.x
    CodeUri: .output/healthz
    Timeout: 10
    Events:
      ApiEvent:
        Type: Api
        Properties:
          Path: /health
          Method: GET
Globals:
 HttpApi:
    CorsConfiguration:
    AllowOrigin: "'http://localhost:3000'"
    AllowMethods: "'POST, OPTIONS, GET, PUT'"
    AllowHeaders: "'Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token'"

我已经尝试使用这些适用于 SAM 的 AWS Docs 进行各种设置(Api、HttpApi),但总是设法只获得版本 1 事件。

你能指出我做错了什么或如何指定版本吗?

4

1 回答 1

0

在这篇文章中找到了解决方案。需要在HttpApi的配置中指定PayloadFormatVersion: "2.0"(value as string)。

示例 yml:

Resources:
  MyFunction:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: index.handler
      Runtime: nodejs14.x
      CodeUri: .output/healthz
      Timeout: 10
      Events:
        ApiEvent:
          Type: HttpApi
          Properties:
            PayloadFormatVersion: "2.0"
            Path: /health
            Method: GET
            Auth:
              Authorizer: NONE
于 2021-09-28T07:42:24.447 回答