Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS SAM app API multiple method declarations

Tags:

yaml

aws-sam

I created AWS sam application. It has a REST API customers. Presently I'm able to add only one http method type either GET or 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"

How can I declare customers API http methods both GET and POST?

like image 705
Gowri Avatar asked Feb 14 '26 04:02

Gowri


1 Answers

You can define multiple events for the same path.

CustomersGetEvent:
  Type: Api
  Properties:
    Path: /customers
    Method: GET
CustomersPostEvent:
  Type: Api
  Properties:
    Path: /customers
    Method: POST

As an alternative you can also use Method: ANY.

like image 70
Lotus Avatar answered Feb 16 '26 18:02

Lotus