【问题标题】:Declaring resources in multiple files in Serverless framework在无服务器框架中的多个文件中声明资源
【发布时间】:2018-05-29 17:21:02
【问题描述】:

有没有办法将无服务器框架中的资源定义拆分为多个文件?比如:

resources:
  - ${resources/base.yml}
  - ${resources/foo.yml}

我一直在尝试多种组合,但我不断收到关于未找到参考的错误。

【问题讨论】:

    标签: serverless-framework


    【解决方案1】:

    请注意,resources 属性必须是包含 Resources 属性的对象,不是像您在代码 sn-p 中想要的那样的资源数组。

    因此,要使用外部文件引用,您可以执行类似...

    resources
        Resources:
            UsersTable: ${file(../resources/base.yml):UsersTable}
            FooTable: ${file(../resources/foo.yml):FooTable}
    

    参考:Reference variables in other files

    【讨论】:

      【解决方案2】:

      尽管 dashmug 的答案是正确的,但我发现我试图使其工作的方式也非常接近有效的解决方案。如this github comment 中所述,可以在资源部分引用其他文件:

      resources:
         - ${file(resources/first-cf-resources.yml)}
         - ${file(resources/second-cf-resources.yml)}
      

      前提是每个文件都定义了自己的“资源”键,例如:

      ---
      Resources:
        MyCFResource:
          Type:.....
      

      我没有做到的是采用混合方法,例如:

      resources:
        - ${file(resources/first-cf-resources.yml)}
        - ${file(resources/second-cf-resources.yml)}
        SomeResource:
          Type: ...
      

      所以我只有一个 resources/base.yml 来代替它。

      【讨论】:

      • PSA:如果您以这种方式包含一个空的 .yml 文件,这将非常失败(其中空意味着具有“Resources:”标头而没有其他内容。
      • “可怕”是对的。我花了几个小时才弄清楚这一点 - 无缘无故地不断出现instance of Fn::GetAtt references undefined resource 错误,也没有显示任何错误 - 一个只有“资源”的文件(我已经注释掉了一些东西)会默默地破坏你的设置。谢谢@Techrocket9
      【解决方案3】:

      我无法发表评论,但我想扩展 Jesuspc 的回答。

      有一种方法可以在 serverless.yml 中实现这种“混合”方法:

      resources:
        - ${file(resources/first-cf-resources.yml)}
        - ${file(resources/second-cf-resources.yml)}
        - Resources:
            SomeResource:
              Type: ...
      

      在这种情况下,文件first-cf-resources.ymlsecond-cf-resources.yml 必须具有下一个结构:

      Resources:
        SomeResourceA:
          ...
        AnotherResourceB:
          ...
      

      【讨论】:

      • @DeusProx first-cf-resources.yml 也应该从Resources:开始
      • 我总是做“resources: Resources: ...”并像“- ${file(./resourcefile.yml):resources}”一样引用它们,但它仅在仅使用引用时才有效。
      猜你喜欢
      • 2016-11-19
      • 2018-03-05
      • 2020-07-06
      • 2019-10-04
      • 1970-01-01
      • 2019-03-25
      • 2020-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多