[英]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 时,我希望event
是2.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.