如何将多个 serverless.yml 文件合并为单个 serverless.yml 文件?

How to combine multiple serverless.yml files to single serverless.yml file?

我读了这个文档:https://serverless.com/framework/docs/providers/google/guide/services/

users/
  serverless.yml # Contains 4 functions that do Users CRUD operations and the Users database
posts/
  serverless.yml # Contains 4 functions that do Posts CRUD operations and the Posts database
comments/
  serverless.yml # Contains 4 functions that do Comments CRUD operations and the Comments database

如何将这些 serverless.yml 文件合并为单个 serverless.yml 文件?除了部署每个服务,我也可以 运行 serverless deploy 一次部署所有服务。

最简单的方法是使用插件(比如这个:https://github.com/economysizegeek/serverless-dir-config-plugin

如果你想要更多的控制权,你也可以自己动手。例如,您可以将功能特定的配置放在每个目录中,然后使用 cat 之类的工具将它们与项目的通用配置(例如 cat serverless.common.yml users/serverless.yml posts/serverless.yml comments/serverless.yml > serverless.yml)连接起来。虽然如果你想将键合并在一起,你可能需要编写更复杂的东西 &c.

我在单个 serverless.yml 文件中定义函数,并在主 serverless.yml 文件中的函数下包含文件引用,这对我有用,我还将单个 yml 文件命名为 posts-sls.yml、用户-sls.yml等

# foo-functions.yml
getFoo:
  handler: handler.foo
deleteFoo:
  handler: handler.foo

# serverless.yml
---
functions:
  - ${file(../foo-functions.yml)}
  - ${file(../bar-functions.yml)}

此处引用:

https://github.com/serverless/serverless/issues/4218 https://serverless.com/framework/docs/providers/aws/guide/functions/