Sls deploy status code to bash scripts

Hi,

I am using sls deploy inside a bash script to deploy some lambdas to multiple regions. In some cases, the deploy fails, and I want to somehow get that from a return code or status, and stop the script. No text parsing, please. Is there a way to do that?

Thanks,
Oren

In bash $? contains the exit value of the last command. 0 means success and 1 means failure.

Thank you, I know that. The question was mainly if sls deploy fills that with correct values. I’ll try.

To answer your new question - yes.

I have a variation of this issue on this in my bash scripts.

echo -e “Changing to messaging-services directory to deploy messaging service lambda\n”
pushd …/messaging-service
echo “Deploying Messaging Service” | tee -a …/scripts/deploy.log
serverless deploy 2>&1 | tee -a …/scripts/deploy.log
popd

STATUS=$?

if [ $STATUS -eq 0 ]; then
echo “Deployment of Messaging Service Succeeded!” | tee -a …/scripts/deploy.log
else
echo “Deployment of Messaging Service Failed!” | tee -a …/scripts/deploy.log
exit 1
fi

It always returns 0 even if it fails. Not sure if the redirect is the issue here? I have used this logic for executing other programs in a bash script. Like oren-sarid I want to stop the script if I get a non-zero return code

Set STATUS on the line immediately after you run the serverless command. I’ve never used popd but I’ll bet almost anything you’re setting STATUS to the return value from that command which is why it’s always 0 even when serverless fails.

Thanks that was part of the problem. I also was using a pipe so I implemented the $PIPESTATUS to get the true return code of the process I wanted.

serverless deploy 2>&1 | tee -a …/scripts/deploy.log
STATUS=${PIPESTATUS[0]}

if [ $STATUS -eq 0 ]; then

I found this one which stops my deployment. After sls get the error with datadog dependencies, it doesn’t return immediately, but call 2 more (useless) functions:

  • Get Support
  • Environment Information.
    Certainly, these functions cannot be failed, then it makes the whole deployment script return 0
    Could you advice how to fix that @buggy
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Excluding development dependencies...
Serverless: Excluding development dependencies...
Serverless: Excluding development dependencies...
Serverless: Excluding development dependencies...
Serverless: Excluding development dependencies...
 
  Datadog Forwarder Not Found Error ----------------------
 
  DatadogForwarderNotFoundError: Could not perform GetFunction on arn:aws:lambda:ap-southeast-1:fake_for_failed:function:vss-dd-forwarder.
      at /drone/src/node_modules/serverless-plugin-datadog/src/forwarder.ts:62:11
      at Generator.throw (<anonymous>)
      at rejected (/drone/src/node_modules/serverless-plugin-datadog/dist/src/forwarder.js:6:65)
      at runNextTicks (internal/process/task_queues.js:62:5)
      at processImmediate (internal/timers.js:434:9)
 
     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.
 
  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com
 
  Your Environment Information ---------------------------
     Operating System:          linux
     Node Version:              12.22.10
     Framework Version:         1.65.0
     Plugin Version:            3.8.4
     SDK Version:               2.3.2
     Components Version:        2.34.9