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:
Change the name from serverless-1.yml to serverless.yml then sls deploy
Change the name from serverless.yml to serverless-1.yml then
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
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
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.
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:
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
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:
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?