9
votes

I'm attempting to create an AWS::ApiGateway::RestApi resource using CloudFormation but when running

aws cloudformation deploy --template-file lorem.json --stack-name lorem

This ends up failing and upon looking in the CloudFormation console I see that the error is Invalid REST API identifier specified.

enter image description here

Here is my lorem.json file:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "lorem.io Stack",
  "Resources": {
    "API": {
      "Type" : "AWS::ApiGateway::RestApi",
      "Properties" : {
        "FailOnWarnings": true,
        "BodyS3Location": {
          "Bucket": "cloudformation.lorem.io",
          "Key": "open-api.json"
        }
      }
    }
  }
}

Here I'm specifying BodyS3Location which points at an S3 object that contains the following:

{
  "swagger": "2.0",
  "info": {
    "title": "Lorem.IO API",
    "version": "1.0.0"
  },
  "definitions": {
    "Generator": {
      "type": "object",
      "properties": {
        "title": {
          "type": "string"
        }
      }
    }
  },
  "produces": [
    "application/json"
  ],
  "paths": {
    "/generators": {
      "get": {
        "responses": {
          "200": {
            "schema": {
              "type": "array",
              "items": {
                "$ref": "#/definitions/Generator"
              }
            }
          }
        }
      }
    }
  }
}

Because I'm providing this file according to the documentation I shouldn't have to provide the name of the RestApi so I don't think that's the problem. Any idea on how I would go about debugging exactly what it's unhappy about?

Update #1

I've stripped out a lot of my configuration so that the only property that I'm now specifying is name and I still get the same error (Invalid REST API identifier specified):

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "lorem.io Stack",
  "Resources": {
    "API": {
      "Type" : "AWS::ApiGateway::RestApi",
      "Properties" : {
        "FailOnWarnings": true,
        "Name": "Hello World"
      }
    }
  }
}

According to the documentation Name is the only required attribute - Is this a bug w/ CloudFormation or am I missing something?

2

2 Answers

10
votes

Both the original template and the "Update 1" minimal example you provided created successfully in my local tests, and I don't see any obvious issues with either of them.

I noticed that the event-log screenshot above said UPDATE_FAILED instead of CREATE_FAILED, and think the issue is somewhere in the attempt to "update" the existing resource.

Is it possible that the original RestAPI resource was manually modified/deleted after the CloudFormation stack's initial creation? If so, be warned that this violates the "Manage All Stack Resources Through AWS CloudFormation" best practice, and could be the source of the error:

Do not make changes to stack resources outside of AWS CloudFormation. Doing so can create a mismatch between your stack's template and the current state of your stack resources, which can cause errors if you update or delete the stack.

To recover, you could change the logical name of the resource (e.g., from API to API2) and update the stack again. This will cause a new RestAPI resource to be created separately from the old one.

4
votes

I spent a good while trying to replicate using the lorem.json and BodyS3Location content you provided, but was unable to reproduce the error. Is there some more template content or background information missing here?

I think the error may lie with your Swagger template, not CloudFormation. I was able to reproduce the error in the AWS console by deliberately adding an invalid method to the Swagger template (foobar instead of get), then creating the stack. The stack was created successfully, but trying to view the API in the console displayed your error.

I even looked at the history of this post and tried to replicate using the original Swagger template, but the stack would not create. This leads me to believe there's some history with your stack, and maybe the API resource was created initially with, as the error suggests, an "Invalid REST API identifier specified".