【问题标题】:How to define Models for Serverless Api functions in CloudFormation template如何在 CloudFormation 模板中为无服务器 API 函数定义模型
【发布时间】:2020-06-10 02:37:42
【问题描述】:

有关我的模板的相关部分,请参见下文。我不知道如何为 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】:

要回答我自己的问题,对于无服务器,不需要 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

【讨论】:

  • 我们如何在api事件中使用它(模型)?
猜你喜欢
  • 2020-07-19
  • 2020-07-17
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 2019-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多