Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use `cdk diff` to programmatically check whether a stack needs an update?

I am using CDK to deploy cf stack to AWS. It has cdk diff command to tell me what changed in this deployment. If there is nothing changed, it just shows There were no differences for each stack included in the cdk project.

I have a requirement to run different command based on whether the cdk requires a change. How can I know whether it requires a change from a script? I have checked that cdk diff return code is 0 for both change and no change. What is the right way to know whether the change-set will change anything?

like image 491
Joey Yi Zhao Avatar asked Sep 12 '25 23:09

Joey Yi Zhao


1 Answers

While cdk diff --fail generally works, I feel this is a bit dangerous. In case the app has errors or there is a problem with the aws credentials or missing permissions (etc), one would only notice by looking at the output of the CI job.

In our pipeline I'm now checking the output:

cdk diff "*" 2>&1 | tee cdk.diff
grep "There were no differences" cdk.diff && echo "no diffs found" || echo "diffs found"

With the tee command the output is simultaneously written to stdout and the file cdk.diff.

like image 197
udondan Avatar answered Sep 14 '25 12:09

udondan