Deploy - specify which serverless.yml file to deploy

Greetings,

What I need:
Is there a way to specify which serverless.yml file I want to deploy?
sls deploy uses serverless.yml to deploy but I have multiple .yml files (serverless-1.yml, serverless-2.yml so on) Why? I’ll explain it at the end.

What I currently do:

  1. Change the name from serverless-1.yml to serverless.yml then sls deploy
  2. Change the name from serverless.yml to serverless-1.yml then
  3. Change the name from serverless-2.yml to serverless.yml then sls deploy

What I need
To specify which .yml file I want to deploy so I don’t have to change the names to serverless.yml

sls deploy --file serverless-1.yml
sls deploy --file serverless-2.yml

Why do I have multiple YAML files under the same directory?
Well I’ve hit the CF 200 resources limit a long time ago, I’ve tried plugins such as split stacks, nested stacks and additional stacks none of them worked, so I’m using microservices now I have to divide my service into small services (30 or 35 functions per serverless.yml)

I realized that putting the serverless.yml file under a directory made that directory the microservice thus I could not invoke or use anything outside that directory (utils functions and node_modules) I don’t want to put a node_modules inside each microservice and I dont want to copy and paste my utils functions in each microservice (is obvious why), so I ended up having my microservices to be the same the only thing that changes is the yml file (inside the functions I declare) and the index.js file

2 Likes

No.

This has been raised before at Issues · serverless/serverless · GitHub but I don’t believe it’s ever been implemented.

The usual advice is to split your application into microservices. You can’t get around having a separate node_modules for each microservice but that’s actually a good thing as it keeps your Lambda size down which helps at run time. Finally, if you have common utility functions I would recommend putting those into their own NPM package which you can use in your services.

2 Likes

Thanks a lot buggy. It’s really hard to put my common functions in a npm package because it changes a lot and it connects to our databases.

This may be a bit of a hack, but how about creating a deploy script for each one with that does something along the lines of:

------- deploy.sh ------

if [ -z "$1" ]; then
  echo "missing serverless id"
fi

id=$1
shift

if [ -e "serverless.yml" ]; then
  rm -f "serverless.yml"
fi

ln -s "serverless-$id.yml" serverless.yml
sls deploy $*

Then deploy by executing that script with the id and whichever parameters you wish to pass in like so:

./deploy.sh 1
./deploy.sh 2 -s prod -v
./deploy.sh 3 -s anotherstage -v

etc…

This all assumes that you’re using a bash shell such as bash on *nix or git-bash on windows, etc…

Hope this helps.

1 Like

Where I’ve needed to do something similar I’ve put serverless.tml files in a sub directory and ran my deploy scripts from there, but I bfieber’s idea of wrapping sls in scripts seems sound.

Hello @bfieber, nice solution just one observation, I don’t want to delete the serverless.yml (rm -f) I need to change its name then after deploy return its old name.

@robertdavidrowland I tried that and the zip file only had the sub directory contents thus no node_modules and others files just the ones inside that subdirectory

The rm -f is only removing the symbolic link(or on windows, the copy) left from the previous run if it exists.

Instead of trying to do the rename dance and hope nothing gets lost, just using a symlink keeps the originals around and makes the one you’re currently needing look like serverless.yml

Not sure why you’re not getting your node_modules. I tested it in my environment as and it worked perfectly.
Are you using a bash script friendly shell?
If not, you can re-write it as a windows script file.

@bfieber oh Nice one I’ll try it, I have windows OS but I use git bash so your sh will work, I’ll try again the subdirectories to see if it packages only the files where the .yml is

It removed my serverless.yml file it didn’t even created the SL

This is an old thread but ran across it and wanted to post for anyone finding it still. Simplest was to combine the copying mentioned above in the package.json. In the root of my folder I have two serverless files, serverless-public.yml and serverless-admin.yml and then the two scripts just pull one and copy it:

   "start-dev-public": "rm -f serverless.yml && cp serverless-public.yml serverless.yml && sls offline start --stage=dev --port=4000",
   "start-dev-admin": "rm -f serverless.yml && cp serverless-admin.yml serverless.yml && sls offline start --stage=dev --port=4999",

This is now possible with the --config option.

Please Note that the -c option seems broken at this time.

Hey,

I can’t find an example of this anywhere. Can you please provide some example on how to select a different yml?

Thanks

EDIT: Right… it’s as easy as you would think… just serverless -c
Not sure why I thought you would have to pass more stuff
Thanks

When I specify serverless --config ./path/to/serverless.yml it works fine, but I have a plugin there and it errors that the plugin is not found. Can I set a node_modules as well?

do you have the docs?