Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS SAM: The REST API doesn't contain any methods

I'm trying to use AWS SAM to deploy a simple API. When the API is simple (that is, does not specify explicitly an API Gateway). the deployment succeeds.

However, the following deployment fails:

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

Parameters: 
  Stage:
    Type: String
    AllowedValues: 
      - dev
      - sat
      - demo
      - staging
      - prod
    Description: Enter dev, sat, demo, staging or prod

Resources:

  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: !Ref Stage
      EndpointConfiguration: PRIVATE
      DefinitionBody:
        swagger: '2.0'
        x-amazon-apigateway-policy:
          Version: '2012-10-17'
          Statement:
            - Effect: Allow
              Principal: "*"
              Action: execute-api:Invoke
              Resource:
                - !Sub arn:aws:execute-api:*:*:*/${Stage}/*

  ThumbnailFunction:
    Type: 'AWS::Serverless::Function'
    Properties:
      Runtime: nodejs8.10
      Handler: get-config.handler
      CodeUri: ./functions
      Events:
        ThumbnailApi:
          Type: Api
          Properties:
            RestApiId: !Ref MyApi
            Path: /thumbnail
            Method: GET

The error message is the following:

The REST API doesn't contain any methods (Service: AmazonApiGateway;
Status Code: 400; Error Code: BadRequestException

Looking on Google, I do find mentions of this error when manually specifying a deployment (here, or here). In my case, the deployment is implicit, hence I assume my issue is different.

The code I'm using is based on a SAM example (here). I'm scratching my head to understand what is wrong with my stack.

Any pointers to a solution?

like image 959
Pierre Avatar asked Jan 27 '23 15:01

Pierre


1 Answers

Just as the error message says, you have not defined any methods in your Swagger. I think your confusion is here:

In my case, the deployment is implicit, hence I assume my issue is different.

SAM does create implicit APIs of type AWS::Serverless::Api from the union of Api events defined on AWS::Serverless::Function resources - but only if they do not refer (via the RestApiId property) to an AWS::Serverless::Api resource that you defined explicitly in the template. And in your case, it does.

Also, you mention that your template is based on the "api_swagger_cors" example SAM template here, but in fact there is a key difference between yours and that example, namely: in the example, a Swagger YAML file is being pulled in from an S3 bucket; whereas in yours, your Swagger is defined inline, but it doesn't define any methods.

For more info:

  • See this answer on implicit v explicit APIs (I also wrote that).
  • See this page for the structure of a Swagger.
like image 81
Alex Harvey Avatar answered Feb 01 '23 13:02

Alex Harvey