Image for post
Image for post

Power up serverless framework with javascript configurations

Pavel Vlasov
Jan 30, 2018 · 2 min read

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.

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store