合并 serverless.yml 中的属性

Combine properties in serverless.yml

我在 serverless.common.yml 中有一个 custom 部分,它对所有服务都是通用的。某些服务在其 serverless.yml 应用程序中使用了前面提到的文件中定义的一些附加属性。

我的想法是执行类似的操作:

custom:
  - ${file(../serverless.common.yml):custom}
  - myServiceCustomProperty: 1

但这不起作用。关于如何实现该行为的任何想法?

谢谢

- ${file(../serverless.common.yml):custom} 将从 common 中转储数组,这样就不会合并。使用这种方法,您需要分别从 common/custom 部分添加每个 属性:

custom:
  - foo: ${file(../serverless.common.yml):custom.foo}
  - myServiceCustomProperty: 1

custom: ${file(../serverless.common.yml):custom}

或者,如果您的设置很复杂,您可以编写一个允许 JS 处理的 serverless.js 文件。我个人使用它以编程方式合并十几个 yml 文件 - 例如当我 运行 sls offline 我想要 add/remove 一堆东西时,这是一个非常强大的方法。