如何在CloudFormation模板中为无服务器Api函数定义模型

问题描述 投票:0回答:1

请参阅下面的模板相关部分。我不知道如何为Api设置模型。如果我将模型部分从MyApi中剔除,则'sam deploy'说:“相关的API未定义任何模型”。那么,如何为Api和功能请求模型添加模型?

次级问题:

可以在外部json / yaml文件中定义模型吗?

如何定义响应模型?

我可以在单独的模板文件中引入模型吗?

谢谢。

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
aws-lambda amazon-cloudformation aws-api-gateway aws-serverless
1个回答
0
投票

为了回答我自己的问题,使用无服务器,不需要AWS :: ApiGateway :: Models,而是使用Api定义它们。

 MyApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: test
      Models:
        PostPointModel:
          type: object
          required:
            - name
          properties:
            name:
              type: string
            description:
              type: string
© www.soinside.com 2019 - 2024. All rights reserved.