AWS SAM 应用程序 API 多个方法声明

Gow*_*wri 3 yaml aws-sam

我创建了AWS sam应用程序。它有一个REST API 客户。目前我只能添加一种 http 方法类型 GET 或 POST。

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

Globals:
Function:
Timeout: 59

Resources:
  HealthFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: src/
      Handler: index.handler
      Runtime: nodejs14.x
      Architectures:
        - x86_64
      Events:
        Health:
          Type: Api 
          Properties:
            Path: /health
            Method: get
        Customers:
          Type: Api 
          Properties:
            Path: /customers
            Method: get

Outputs:
  HealthApi:
    Description: "API Gateway endpoint URL for Prod for Health function"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/health"
  CustomersApi:
    Description: "API Gateway endpoint URL for Prod for Health function"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/customers"
Run Code Online (Sandbox Code Playgroud)

如何声明客户 API http 方法 GET 和 POST?

Lot*_*tus 7

您可以为同一路径定义多个事件。

CustomersGetEvent:
  Type: Api
  Properties:
    Path: /customers
    Method: GET
CustomersPostEvent:
  Type: Api
  Properties:
    Path: /customers
    Method: POST
Run Code Online (Sandbox Code Playgroud)

作为替代方案,您也可以使用Method: ANY.