3

我能够使用 Cloudformation / Serverless Application Model 定义和部署我的 API Gateway + Lambda 堆栈,并希望将模型添加到我的 API。

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

我看到的错误是

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

原因:模板格式错误:模板的Resources块中未解决的资源依赖[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/"
4

1 回答 1

1

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

Api无服务器函数中属性类型的文档Events位于: 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财产。

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

于 2021-07-05T19:51:50.053 回答