【问题标题】:Cannot reference an API in Outputs section from AWS::ApiGateway::Model resource无法从 AWS::ApiGateway::Model 资源的输出部分引用 API
【发布时间】:2019-01-24 21:57:56
【问题描述】:

我能够使用 Cloudformation/无服务器应用程序模型定义和部署我的 API Gateway + Lambda 堆栈,并希望将模型添加到我的 API。

我在 YAML 中创建了模型,但它似乎无法引用我文件的 Outputs 部分中定义的 API。

我看到的错误是

创建变更集失败:Waiter ChangeSetCreateComplete 失败: 服务员遇到终端故障状态状态:FAILED。

原因: 模板格式错误:未解决的资源依赖项 [MyApi] 在 模板的资源块

是否可以引用Output 对象,或者在这种情况下它不被视为资源?即我是否需要明确定义AWS::Serverless::Api 而不是使用Output

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  PutItemFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: put_item
      Handler: handlers.put_item
      Runtime: python3.6
      Events:
        PutResource:
          Type: Api
          Properties:
            Path: /
            Method: put    
  ModelResource:
    Type: AWS::ApiGateway::Model
    Properties:
      RestApiId:
        Ref: MyApi
      ContentType: "application/json"
      Name: MyModel
      Schema:
        "$schema": "http://json-schema.org/draft-04/schema#"
        type: object
        properties:
          id:
            type: string    
Outputs:
  MyApi:
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/s/"

【问题讨论】:

    标签: yaml aws-api-gateway amazon-cloudformation serverless-framework aws-serverless


    【解决方案1】:

    首先,模板的“输出”部分用于显示模板执行后的信息。在创建资源时,您无法获得对该部分中任何内容的引用。

    无服务器函数中Events 属性的Api 类型的文档位于: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-api.html

    有一个名为RequestModel 的属性可以添加到您的PathMethod 属性下方以引用您的ModelResource 模型。因此,您的活动部分可能如下所示:

          Events:
            PutResource:
              Type: Api
              Properties:
                Path: /
                Method: put  
                RequestModel: MyModel  
    
    

    ...并取出RestApiId 属性。

    我还没有测试过这个,所以我不知道它是否会工作,但试一试。而且,由于我是在你问这个问题 2 年后才回答这个问题的,所以你可能已经想通了。

    【讨论】:

      猜你喜欢
      • 2022-07-28
      • 1970-01-01
      • 2019-06-25
      • 2019-08-31
      • 2021-10-28
      • 2017-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多