Dave Konopka

Forgot about Amazon API Gateway models

In my post on building a serverless URL shortener I neglected to mention API Gateway models at all.

A model defines a JSON schema for data passed in or out of API methods. Models are assigned to method requests/responses. They play a role in data transformations that happen within mapping templates. Models are not used for validating input data. That seems like a logical use for them so perhaps they will serve this role in the future.

It’s easy to forget about API Gateway models. You can stage a functioning API without creating any models contrary to misleading web panel error messages (more on this below). Also, Swagger API exports don’t seem to include any sign of models you do create. That’s why if you import the Swagger definition included with the serverless URL shortener repo you’ll get a misleading popup error message in the AWS web panel:

You need to define a Model to select before adding a status code output. You can define models on the Models page.

I’ve been able to stage API’s without a model despite this error message. You can get rid of the error though by creating a model. Within the API, click Resources -> Model. Add a model named “Token”.

API Gateway model panel

Add the following schema which defines an object with url and token properties.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "http://jsonschema.net",
  "type": "object",
  "properties": {
    "url": {
      "id": "http://jsonschema.net/url",
      "type": "string"
    },
    "token": {
      "id": "http://jsonschema.net/token",
      "type": "string"
    }
  },
  "required": [
    "url",
    "token"
  ]
}