TKirahvi
TKirahvi

Reputation: 318

How to define Models for Serverless Api functions in CloudFormation template

See below for related part of my template. I don't know how to set Models for Api. If I leave the Models part out from MyApi, 'sam deploy' says: "the related API does not define any Models". So how do I add models for Api and as function request models?

Secondary questions:

Can the models be defined in external json/yaml files?

How can I define model for the response?

Can I introduce models in separate template file?

Thanks.

Resources:
  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: test
      Models:
        ???

  PostNewItem:
    Type: AWS::ApiGateway::Model
    Properties:
      RestApiId: !Ref MyApi
      Name: PostNewItem
      ContentType: application/json
      Schema:
        $schema: 'http://json-schema.org/draft-04/schema#'
        title: NewItemModel
        type: object
        properties:
          name:
            type: string
          description:
            type: string
          ....

  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      ...
      Events:
        AddItem:
          Type: Api
          Properties:
            Path: /item
            Method: post
            RestApiId:
              !Ref MyApi
            RequestModel:
              Model: !Ref PostNewItem
              Required: true

Upvotes: 1

Views: 1029

Answers (1)

TKirahvi
TKirahvi

Reputation: 318

To answer my own question, with Serverless, there is no need for AWS::ApiGateway::Models but instead you define them with Api.

 MyApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: test
      Models:
        PostPointModel:
          type: object
          required:
            - name
          properties:
            name:
              type: string
            description:
              type: string

Upvotes: 1

Related Questions