Building a larger project with serverless framework forces you to break your application into many services. But, yaml and json configurations are not always flexible enough to cover all the needs. Fortunately, 1.26 release will change it all with support of serverless.js files.
There’re bunch of commonly used chunks of configuration copied from one file to another. Now you can easily extend base file or use default settings:
I used lodash merge to extend base-serverless.js.
Serverless has a mechanism to include settings from external files. But it doesn’t allow you to generate it dynamically or include custom logic. With serverless.js you can use external modules, that’s slightly more flexible:
Use cli arguments and environment variables
It’s a tricky one. Because js configuration will be executed in the same process with serverless, it allows you to get access to the cli arguments and environment variables (or even define you own!!!). You’ll need you choice of cli arguments parser (I used commander):
I used commander to parse command line and inject properties into configuration. You can implement quite more complex workflows. For example, you are able to inject different properties depending on cli arguments. Another common use case is to configure different domains for different stages.
Thanks for reading! All the examples are available at https://github.com/pavelvlasov/serverless-js-config-example (checkout base-config, composition and props-and-env folders).
Would love to hear your suggestions or use cases in comments.