Search code examples
lambdadeploymentaws-lambdaserverlessaws-serverless

How to deploy multiple functions using serverless cli?


From the doc, I can deploy a function like this

serverless deploy function -f functionName

https://www.serverless.com/framework/docs/providers/aws/cli-reference/deploy-function/

Can I deploy multiple functions with a command like this?

  serverless deploy function -f functionName1, functionName2

If can't, what should I do deploy multiple functions at once?

Thanks


Solution

  • The serverless-cli does not support deploying multiple functions in one go. I'm attached a bash function that you can add to your bashrc so it's always loaded automatically for you (instead of copy-ing and past-ing it when opening a new terminal)

    serverless-deploy-function() {
        # Validate at least one parameter is passed
        [ "$#" -ne 1 ] && echo "USAGE $0 <PARAMETER>" && return 2
    
        # create a list of the first parameter (split by ,)
        declare -a FUNCTIONS_TO_DEPLOY=($(echo $1 | tr "," " "))
    
        # remove the first parameter from $@
        shift;
        # loop on the list of functions to deploy
        for x in $FUNCTIONS_TO_DEPLOY
        do
            echo Running \"serverless deploy function -f $x $@\"
            # Pass one function at a time and keep the rest of the arguments the same
            serverless deploy function -f $x $@
        done
    }
    

    To use it, you'll need to do something like this:

    $ serverless-deploy-function function1,function2
    

    Keep in mind, this should also support extra parameters given that you add them after the function(s) you want to deploy:

    $ serverless-deploy-function function1,function2 --stage dev --region us-east-1
    

    or for one function, as follows:

    $ serverless-deploy-function function1 --stage dev --region us-east-1