Serverless Framework Glob-Merge for YAML
This is a Serverless Framework plugin that lets you merge serverless.yml
files using Serverless Framework variables from YAML. You write a variable that contains a path glob (i.e., **/serverless.yml
) to merge YAML files and an optional subpath to select only the data you want from the merged YAML.
This plugin is primarily intended for splitting serverless.yml
into smaller chunks without needing to create separate Serverless Framework projects or custom ${file(./script.js)}
scripts.
Requirements
- Serverless Framework >= 1.52
Installation
- Install the package by typing
npm i --save-dev serverless-glob-merge-yaml
. - Add
serverless-glob-merge-yaml
to yourserverless.yml
plugins section. - Change your variable syntax to allow globs in variable references. (See the example below.)
serverless.yml
Example serverless.yml
service: name: test plugins: - serverless-glob-merge-yaml provider: name: aws runtime: nodejs12.x # Change the variable syntax to allow *, +, and | in the variables. variableSyntax: "\\${([ *+|~:a-zA-Z0-9._@\\'\",\\-\\/\\(\\)]+?)}" iamRoleStatements: # Look for serverless.yml in src/ and subdirectories and get the # iamRoleStatements from the merged YAML. ${provider.iamRoleStatements} environment: # Provide defaults use stage overrides. The YAML is merged in # lexicographical order from env/ ${stage,'dev'}).yml} # The above works like this: Assume you have a files named # env/0default.yml # env/1dev.yml # env/1prod.yml # # In the dev stage, YAML will merge in this order: # 1) env/0default.yml # 2) env/1dev.yml # # In the prod stage, YAML will merge in this order: # 1) env/0default.yml # 2) env/1prod.yml # # See: https://github.com/isaacs/node-glob#glob-primer functions: # Merge all functions.yml in src/ ${src/**/functions.yml} resources: # Merge all resources.yml in src/ ${src/**/resources.yml}
Variable Syntax
The syntax of the variable expression is broken down like this: ${glob-merge-yaml:PATH_GLOB:SUB_PATH}
glob-merge-yaml
- This is the plugin's variable prefix.PATH_GLOB
- Replace this with a path glob. The glob syntax is based on theglob
packageSUB_PATH
- This is an object path that can be used to traverse the merged data structure. You can omit the subpath to return the merged data structure.
Recursion
The YAML files merged with glob-merge-yaml
can contain additional glob-merge-yaml
statements, so be careful not to create circular references. :)
Contributing
Pull requests are welcome! Especially test scenarios for different situations and configurations.