Power up serverless framework with javascript configurations

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.


Inheritance

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.

Composition

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.

That’s it!

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.

Clap if it was helpful! If you’d like to chat or connect, here is my twitter, @pvl4sov, github and linkedin.

Pavel Vlasov

Written by

Teams advocate and #serverless enthusiast. Creator of Serverless IDE https://marketplace.visualstudio.com/items?itemName=ThreadHeap.serverless-ide-vscode

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade