Search code examples
amazon-web-servicesaws-api-gatewayapi-gatewayaws-sam

SAM Lambda event with an explicit API as the event source


I am trying to set up an event on my lambda function in my SAM template, but I want the event source to be an explicit API endpoint.

The documentation shows an event with an implicit API as an event source:

GetFunction:
  Type: AWS::Serverless::Function
  Properties:
    Handler: index.get
    Runtime: nodejs6.10
    CodeUri: s3://bucket/api_backend.zip
    Policies: AmazonDynamoDBReadOnlyAccess
    Environment:
      Variables:
        TABLE_NAME: !Ref Table
    Events:
      GetResource:
        Type: Api
        Properties:
          Path: /resource/{resourceId}
          Method: get

This would be the explicit API definition:

Resources:
  MyApi: 
    Type: AWS::Serverless::Api
    Properties:
      StageName: prod
      DefinitionUri: swagger.yml

How do I explicitly set the event source to be MyApi?


Solution

  • I needed to add the RestApiId under the event definition like so:

    Events:
        GetResource:
          Type: Api
          Properties:
            RestApiId: !Ref MyApi
            Path: /resource/{resourceId}
            Method: get