简体   繁体   中英

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. 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.

I am using CDK HttpApi construct from @aws-cdk/aws-apigatewayv2 hence there is now inconsistency between my local testing and what's deployed.

I am new to Sam config, my template.yml file is:

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.

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.

Example 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM