question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Share common serverless configuration via nested serverless.yml files

See original GitHub issue

Use case description

A common serverless project structure is to have one repo with many individual services, each service in its own subfolder having its own serverless.yml. There can often be much of the configuration in the serverless.yml files that is common, leading to code duplication.

If we could define multiple “layers” of serverless.yml files, and have them merged together, entire subfolders of services could share common configuration and only override the parts that they need.

Some documentation exists suggesting to use syntax such as `${file(…/…/serverless.common.yml):fooBar} to refer to variables in a common serverless.yml. While this works, it becomes very tedious to manage and if you add a new common configuration you have to make sure to add a reference to it in every child service.

There is also a plugin serverless-plugin-parent which works better, but has the following limitations

  • It will only merge in 1 found parent serverless.yml file
  • Due to the fact that plugins are loaded AFTER the initial load and validation of the serverless.yml, many properties in the yml file cannot be shared (such as the provider) because the validation will fail if they are missing before the plugin even loads.

Proposed solution

I propose to support a project heirarchy where, upon loading of the serverless.yml file, the directory tree is walked upward, finding each parent folder’s serverless.yml and merging them in (with the items in each child node taking precedence). This would support projects with a structure like

/
     serverless.yml #top level, shared configuration for entire repo
     resources/
          serverless.yml  #shared configuration for all resources
          resourceFoo/
                 serverless.yml #specific configuration for Foo resource
     services/
          serverless.yml #shared configuration for all services
          serviceFoo/
                 serverless.yml #specific configuration for Foo service
           serviceBar/
                 serverless.yml #specific configuration for Bar service

It would be nice if a plugin could handle this but as I mentioned above, since the loading of serverless.yml and subsequent validation of it is before plugins are loaded, they can’t fully do this. Additionally, the use of plugins is specified in the serverless.yml file itself, so specifying of plugins would have to be repeated in each serverless.yml file.

Therefore, I think this should be a feature of the main serverless framework. To support backwards compatibility, it should probably be driven by a configuration parameter in the child serverless.yml file. Perhaps like

service:
   name: foo
   nestedService: true  #open to naming of this

I have a few ideas on where and how this could be implemented in the codebase and I am willing to contribute a PR.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
PatrykMilewskicommented, Nov 30, 2020

@kyle-hiller-nh @drock Consider using this plugin: https://github.com/KrysKruk/serverless-import-config-plugin

It also has some other cool features.

1reaction
medikoocommented, Nov 25, 2020

i.e. a tree structure of serverless files more than one level deep. Is this an intentional limitation or a bug?

If it doesn’t work, it’s a bug (please create a bug report for this). Still variables resolver is scheduled for a complete rewrite that should tackle all known bugs (follow https://github.com/serverless/serverless/issues/7207 and https://github.com/serverless/serverless/issues/8364)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Introducing multi-service deployments via Serverless ...
We are excited to announce Serverless Framework Compose: a new feature enabling you to deploy multiple services in one command, in parallel, ...
Read more >
Variables - Serverless Framework
Variables allow users to dynamically replace config values in serverless.yml config. They are especially useful when providing secrets for your service to use ......
Read more >
Split Stacks - Serverless Framework: Plugins
Set resources in the nested stack to depend on each others. This feature comes with a 2 new configurations, stackConcurrency and resourceConcurrency :...
Read more >
Serverless Dashboard - Outputs
Define outputs for shared services ... Define new outputs by adding a dictionary (key/value pairs) to the outputs: field in the serverless.yml file....
Read more >
AWS Lambda Guide - Serverless.yml Reference
List of existing Amazon SNS topics in the same region where notifications about stack events ... Enable CORS HTTP headers with default settings...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found