I'm trying to deploy AWS SAM in a CI/CD pipeline. I'm calling the "deploy" command from an ansible shell which then fails if the stack exists
If "aws cloudformation deploy" is run on an existing stack (e.g. re-run) it outputs:
No changes to deploy. Stack
is up to date
And exits with error code 255.
Based on
https://docs.aws.amazon.com/cli/latest/topic/return-codes.html
I'd suggest this is not really a "Command failed" error.
Better would be to return 0, or another value (!=0 and != 255) to indicate no-op.
Hit the same issue recently, this is really a show stopper regarding using deploy in a CI/CD pipeline as we need to swallow the exception or sniff the message to determine if the exception can be ignored.
I wrote the wrapper aws-cloudformation.sh script as a workaround: https://gist.github.com/jghaines/6b25a48c8531c236b0ec4831f4465ce4
Same here. If the call was a no-op, I just do || exit 0 - not ideal but works for my case (shrug)
Good point. We will get this fixed in the CLI.
If anyone is interested in sending a PR, here is the relevant code bit:
Exception is thrown from here: https://github.com/aws/aws-cli/blob/develop/awscli/customizations/cloudformation/deployer.py#L135
Catch ChangeEmptyError Exception here and return a 0 status: https://github.com/aws/aws-cli/blob/develop/awscli/customizations/cloudformation/deploy.py#L164
And, write tests :-)
I was _just_ prepping to fix this and I see that @stan3 has an open PR https://github.com/aws/aws-cli/pull/2606 馃憤 馃巻 Thanks Tristan!
Closing this in favor of aws cli PR
Still an issue in version 1.15.26
aws --version
aws-cli/1.15.26 Python/2.7.13 Linux/4.14.33-51.37.amzn1.x86_64 botocore/1.10.26
Debian GNU/Linux 9.4 (stretch) Release: 9.4 Codename: stretch
Linux f2f521fcf707 4.14.33-51.37.amzn1.x86_64 #1 SMP Thu May 3 20:07:43 UTC 2018 x86_64 GNU/Linux
If you are using a bash script to automate the package & deploy then you can add this after the sam deploy ... command to exit gracefully:
```
if [ "$?" -eq 255 ]
then
echo "No changes to deploy."
true
fi
Adding --no-fail-on-empty-changeset seems to work i.e., allow bash script execution to continue even if there were no updates made to the stack.
aws cloudformation deploy --template-file cloud-formation.yml \
--stack-name xxx \
--no-fail-on-empty-changeset
@sanathkr I am confused as to why this is closed in favour of the aws cli change. How does this help those using sam deploy?
@latheesan-k do you have evidence that the 255 exit status is only used when there are no changes to deploy, and for no other reason?
@sanathkr I am confused as to why this is closed in favour of the aws cli change. How does this help those using sam deploy?
Because sam deploy uses aws cloudformation deploy in the background
@bethesque From what I observed, when you run sam deploy, it always returned 255 with the message about no changes to apply, but I think this is misleading.
the whole process is deferred, when you deploy it packages up the CF template and uploads it to S3 and then another process kicks in to apply the changes, which can independently fail for various reasons.
@mvanbaak does sam deploy now expose that flag? I tried using the flag with the same name for sam deply, and it failed saying it didn't know what the option was, but I am suspecting my version of the awscli is out of date.
@bethesque Yes, then your aws cli is out of date.
sam deploy is just an alias/wrapper around aws cloudformation deploy and as the help from sam deploy --help states, you can use all the flags that aws cloudformation deploy understands.
I just tested the flag with the latest awscli installed and it works perfectly.
After you updated your awscli, things should work. Good luck!
Thanks @mvanbaak!
Adding --no-fail-on-empty-changeset works with sam deploy as well
sam deploy --no-fail-on-empty-changeset --template-file packaged.yaml --stack-name Stack-Name
Most helpful comment
Adding
--no-fail-on-empty-changesetseems to work i.e., allow bash script execution to continue even if there were no updates made to the stack.