Deploy - specify which serverless.yml file to deploy


#1

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

No.

This has been raised before at https://github.com/serverless/serverless/issues 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.


#3

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.


#4

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.


#5

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.


#6

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.


#7

@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


#8

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.


#9

@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


#10

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