简体   繁体   English

AWS SAM - 本地 api 调用以返回事件的版本 2

[英]AWS SAM - local api call to return version 2 of the event

I am using AWS SAM to test my Api gateway and lambdas locally.我正在使用 AWS SAM 在本地测试我的 Api 网关和 lambda。 When executing sam local start-api and calling a lambda, I'd like the event to be of version 2.0 format instead of version 1.当执行sam local start-api并调用 lambda 时,我希望event2.0 版格式而不是 1 版。

I am using CDK HttpApi construct from @aws-cdk/aws-apigatewayv2 hence there is now inconsistency between my local testing and what's deployed.我正在使用来自@aws-cdk/aws-apigatewayv2 CDK HttpApi构造,因此现在我的本地测试与部署的内容之间存在不一致。

I am new to Sam config, my template.yml file is:我是 Sam 配置的新手,我的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'"

I've tried various setups (Api, HttpApi) using those AWS Docs for SAM but always manage to get only version 1 event.我已经尝试使用那些适用于 SAM 的 AWS Docs 的各种设置(Api、HttpApi),但总是设法只获得版本 1 事件。

Can you point me to what I am doing wrong or how to specify the version?你能指出我做错了什么或如何指定版本吗?

I found the resolution in this post .在这篇文章中找到了解决方案。 It is necessary to specify PayloadFormatVersion: "2.0" (value as string) in the configuration of HttpApi.需要在 HttpApi 的配置中指定PayloadFormatVersion: "2.0" (value as string)。

Example yml:示例 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Lambda 的 AWS SAM API 端点在使用“sam start local-api”在本地运行时抛出错误 - AWS SAM API endpoint using a Lambda is throwing error when it runs locally with "sam start local-api" AWS SAM 本地启动 API 无法解析 Fn::ImportValue - AWS SAM local start-api cannot resolve Fn::ImportValue 无法将 AWS SAM 本地 API 连接到在 docker 实例中运行的 dynamodb-local - Unable to connect AWS SAM local API to dynamodb-local running in docker instance VS Code 和 AWS SAM 本地调用问题 - VS Code & AWS SAM local invoke issue 卓:如何使用“sam local”写入本地文件系统? - AWS: How to write to the local file system with 'sam local'? SAM 中 AWS API 网关的授权设置 - Authorization setting in SAM for AWS API Gateway AWS SAM - 将 Lambda 授权方添加到现有 API - AWS SAM - Add Lambda Authorizer To Existing API AWS SAM:我们可以在 aws sam 模板中使用已经存在的 api 吗? - AWS SAM: can we use a already existing api in aws sam template? 是否可以使用 SAM cli 发布 aws lambda 的新版本 - Is it possible to publish a new version for aws lambda using SAM cli SAM AWS Lambda Python:网关 api - 处理二进制形式上传 - SAM AWS Lambda Python: gateway api - handle binary form upload
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM