Serverless.yml splitting (per function)

Hello there,

Migrating from serverless 0.5.6, I’m currently looking for a way to sepaprate the function part from serverless.yml inside each function, cause I have like 60 different functions and i don’t want to have an gigantic serverless.yml

Found this. Guide name did not attract my attention tho :’( maybe it should be more explicit !

Just FYI with 60 functions it might make more sense to split them up into separate services so you don’t have one huge serverless.yml file.

It’s planned in the future. Last month were kind of rush-y.

FYI from what I see now, my deploy time will go from ~2 hours with Serverless 0.5.6 to ~5 minutes with Serverless v1, so I like it :slight_smile:

Awesome, great to hear that you’re happy with it! Let us know if anything else comes up.

I have the issue here. I have several functions in the serverless.yml. I want to move each function’s config into function’s folder, and use ${file(./path-to-the-file)} to include them in the main serverless.yml.

The serverless.yml looks like this

functions:
  generateThumbs: ${file(api/photos/generate-thumbs/serverless.cfg.yaml).generateThumbs}

I got this error

  Type Error ---------------------------------------------

     Cannot assign to read only property 'events' of ${file(api/photos/generate-thumbs/serverless.cfg.yaml).generateThumbs}
1 Like

I want to develop a plugin that will automatically detect function.yml files under the root of the project. Is there a lifecycle event that is fired every time the serverless.yml is loaded ? Can’t find it :frowning:

Do you guys have any idea how I could achieve this …?

1 Like

@titozzz,
Hello,

I’m currently trying to do the same thing… “separate the function part from serverless.yml inside each function”.
Did you ever work out a solution using ${file(./path-to-the-file)} ?
I’m working on automating the setup of a serverless service and am interested to know if you’re working on a plugin?

Hi, Guys!

Just created a way to load functions in external files. In the serverless.yml file I put:

functions: ${file(./mapping/wrapper.js)}

The wrapper script code follows:

const YAML = require('js-yaml');
const path = './mapping/';
var fs = require('fs');

    module.exports = () => {
    var functionsData = '';
    var files = fs.readdirSync(path);
        
    files.forEach(function(element) {
        if (element.match('.yml')) {
            functionsData += fs.readFileSync(path + element);
        }
    }, this);

    return YAML.safeLoad(functionsData, 'utf8');

};

The functions files are in the same directory that the script. Bellow one function file:

miscGetURL:
  handler: src/misc/handler.get
  events:
    - http:
        path: /shortener/misc/{id}
        method: GET
        private: true
        request:
          parameters:
             id: true

I recommend to always put a new line at the end of function.yml, because the wrapper load all files content in a single variable.

Its important to exclude the js-yaml from deploy.

Regards!

3 Likes

I like the idea of this!! One question being a relative newbie to Serverless. Why is it important to exclude js-yaml? I’ve looked around and I’m picking you exclude it in the package.json file?

The other alternative from other comments would be to break up all the api’s into seperate sls services to keep them lightweight. I’m wondering actually if this is not the best option as breaking them up would isolate changes made in one service from other services and only changes would get deployed as the other services would be unchanged

Hi!

I excluded the js-yaml just because its used only in the wrapper.js.

I choosen a single project for the whole API because we have some vpc/env configuration and its easier to mantain in a single file. Also we want to centrilize the deploy process for production.

Breaking up the API in sls services is a good aproach too, but we found more easy to mantain the single file structure.

Regards!