I know it's been a while since this question was asked, but I was curious and looked into it myself, so I'll share it with you.
(I also thought that the current accepted answer was about the CDK CLI and not about the CDK application code.)
The App
class has an option called autoSynth
which is set to true
by default, so even if you omit app.synth()
in your code, it will be executed automatically.
https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.AppProps.html#autosynth
As mentioned in the documentation, this feature may not work in some languages, so it seems to be explicitly specified in many sample codes.
According to the documentation, it is OK to run app.synth()
multiple times.
https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.App.html#synthoptions
autoSynth
works fine on Node.js.
It is implemented in Node.js using process.once("beforeExit", listener)
.
https://github.com/aws/aws-cdk/blob/v2.51.1/packages/@aws-cdk/core/lib/app.ts#L166-L170
I started this investigation because when I ran CDK in Deno, I had to explicitly run app.synth()
to generate the CDK assembly. As a result of this investigation, I found out that this is because the Node compatibility layer in Deno does not currently support "beforeExit" events.
https://github.com/denoland/deno_std/blob/113435ada1948b90188586f022d55745c6d2d19b/node/process.ts#L51-L52
As per this experience, it is better to explicitly call app.synth()
as recommended.