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

1

5

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?

Hereto answered 9/3, 2019 at 3:48 Comment(0)
X
8

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.
Xanthic answered 9/3, 2019 at 9:18 Comment(2)
Thank you ; it is disappointing that Swagger seems to be the only way to define gateway policy in SAM, and at the same time that it requires basically defining function events twice.Hereto
Can some one point me to the SAM documentations, I'm facing some issues with SAM local invoke, I have mentioned my usecase in #78571341Parshall

© 2022 - 2024 — McMap. All rights reserved.