Sub topic 11
Synthesize, diff, deploy and destroy
This chapter covers the four commands you run most often with the CDK: cdk synth, cdk diff, cdk deploy and cdk destroy. It assumes you have a working CDK app with at least one stack, like the AppStack from earlier chapters. It also assumes your account and region are bootstrapped and your AWS credentials are configured.
The lifecycle at a glance
Each command takes one more step of the pipeline than the one before it. synth only runs your code. diff also asks CloudFormation what is currently deployed. deploy changes the cloud. destroy removes what deploy created.
cdk synth
cdk synth executes the command in the app field of cdk.json. For a TypeScript project this is typically npx ts-node bin/my-app.ts. Your constructs are instantiated, and the result is written as a cloud assembly in the cdk.out directory. The assembly holds one CloudFormation template per stack, a manifest, and any bundled assets.
cdk ls # list stack IDs in the app
cdk synth # print the template if the app has one stack
cdk synth AppStack # print one stack's template
cdk synth --quiet # write cdk.out without printing templatesSynthesis makes no changes in AWS. Most validation errors surface here, for example a missing required prop or a construct that fails its own checks. Because your code runs, anything non-deterministic in it, such as random values or the current time, can produce a different template on every run. Keep stack code deterministic.
Some lookups, such as Vpc.fromLookup, call AWS during synth. The results are cached in cdk.context.json. Commit that file so every machine synthesizes the same template.
cdk diff
cdk diff synthesizes the app, fetches the template of the deployed stack from CloudFormation, and prints the differences. It marks additions with [+], removals with [-] and modifications with [~]. It also shows IAM statements and security group rules in a separate table, because these are the changes most likely to widen access.
cdk diff
cdk diff AppStack
cdk diff --fail # exit with a non-zero status if there are differencesTwo points are easy to miss:
diffcompares your synthesized template with the template last deployed through CloudFormation. It does not inspect the live resources. A setting changed by hand in the console does not appear in the diff.- A property change can mean replacement. If the diff shows a resource with
replacenext to it, CloudFormation will create a new resource and delete the old one. For a stateful resource such as a database, that can lose data. Read these lines before you deploy.
cdk deploy
cdk deploy runs the following steps:
- Synthesizes the app.
- Uploads assets (Lambda code, files, container images) to the bootstrapped bucket and repository.
- Creates a CloudFormation change set for each stack and executes it.
- Streams resource events to your terminal and prints stack outputs when done.
cdk deploy # one stack
cdk deploy AppStack
cdk deploy --all # every stack in the app
cdk deploy --profile staging # use a named AWS profile
cdk deploy --outputs-file out.json # save stack outputs as JSON
cdk deploy --require-approval never # skip the approval prompt (CI)If one stack depends on another, deploying the dependent stack deploys its dependencies first. Stacks without dependencies between them can be deployed in parallel with --concurrency.
Approval prompts
By default, deploy pauses and asks for confirmation when a change broadens permissions, such as new IAM statements or opened security group rules. The --require-approval flag controls this:
| Value | Behavior |
|---|---|
| `never` | Never prompt |
| `any-change` | Prompt on every change |
| `broadening` | Prompt only on security-broadening changes (default) |
In a pipeline there is nobody to answer the prompt. Run cdk diff as a review step, then deploy with --require-approval never.
Failures and rollback
CloudFormation rolls back by default when a deploy fails. An update that fails ends in UPDATE_ROLLBACK_COMPLETE, and the stack returns to its previous state. A first-time create that fails ends in ROLLBACK_COMPLETE. That stack cannot be updated and must be deleted before you try again, which cdk destroy does.
For faster iteration in development only, two options skip the full change set:
cdk deploy --hotswapupdates some resources directly, for example Lambda code, without CloudFormation. This makes the deployed stack differ from its template, so never use it in production.cdk watchredeploys automatically when files change and uses hotswap by default.
cdk destroy
cdk destroy deletes the stack and the resources CloudFormation created for it, after you confirm.
cdk destroy AppStack
cdk destroy --all --force # no confirmation promptWhat survives a destroy depends on each resource's removal policy. The bucket in our example uses RemovalPolicy.RETAIN, so it stays in the account after the stack is gone. That protects data, but it also means a later deploy that creates a bucket with the same physical name will fail. A bucket with RemovalPolicy.DESTROY is only deleted if it is empty. Add autoDeleteObjects: true if you want the CDK to empty it first, and use this only for disposable environments.
A stack with termination protection enabled refuses to be deleted until you turn the protection off.
Selecting stacks
All four commands accept stack names and patterns. Stack names are the IDs you passed when instantiating the stacks. In an app with ApiStack and DbStack:
cdk diff 'Api*'
cdk deploy ApiStack DbStack
cdk synth '*'When you pass no name and the app has multiple stacks, the CLI asks you to pick one. This prevents accidental deployment of everything.
A safe working loop
- Edit your stack code.
- Run
cdk synthto catch errors. - Run
cdk diffand read the IAM table and any replacements. - Run
cdk deploy. - Delete scratch environments with
cdk destroy.
Exercise
You rename the construct ID of a bucket from 'UploadsBucket' to 'Uploads' and run cdk diff. The output shows the old bucket removed and a new one added. Why, and what should you check before deploying?
Show answer
The construct ID is part of the logical ID that CloudFormation uses to track the resource, so a new ID looks like a different resource: CloudFormation deletes the old one and creates a new one. Check the removal policy and whether the bucket holds data. With RETAIN the old bucket stays but the new one is empty and the data is not migrated. If the bucket has an explicit physical name, the create can also fail because the name is still taken. Avoid the rename, or keep the original construct ID.
Exercise
Someone changes a bucket's versioning setting in the console. You run cdk diff and it reports no differences. Why?
Show answer
cdk diff compares your synthesized template with the template last deployed through CloudFormation. It does not read the live resource, so manual changes (drift) are not shown. Use CloudFormation drift detection to find them.