Sub topic 10
Synthesize, diff, deploy and destroy
This chapter covers the four commands you run most often in a CDK app. It assumes you have a CDK project, a bootstrapped environment, and a stack like the bucket and Lambda example from earlier chapters.
The workflow at a glance
The commands run in a fixed order. Each one gives you a chance to catch a problem before the next one changes anything real.
cdk synth
cdk synth runs your app and writes the result to the cdk.out directory. This output is the cloud assembly: CloudFormation templates, asset files, and a manifest. With one stack, the command also prints the template as YAML.
cdk synth
cdk synth AppStack > template.yamlSynthesis makes no AWS changes. It does fail on code errors, such as a missing required property, so it works as a fast check. If your app has several stacks, name the one you want.
cdk diff
cdk diff synthesizes the app, then compares the new template with the one deployed in CloudFormation. It prints added, changed and removed resources. It also shows IAM changes in a separate table, so you can review permissions that grantRead generated.
cdk diffRead the diff for lines marked as a replacement. A replacement means CloudFormation creates a new resource and deletes the old one. For a stateful resource such as a database, that can lose data.
cdk deploy
cdk deploy synthesizes, uploads assets to the bootstrap bucket, and asks CloudFormation to create or update the stack. If the change adds IAM permissions or security group rules, the CLI shows them and asks you to confirm.
cdk deploy
cdk deploy AppStack --require-approval never
cdk deploy --allUse --require-approval never only in automation where a person has already reviewed the change. Outputs, such as a bucket name, print when the deployment finishes.
If CloudFormation fails part way through, it rolls the stack back to the last working state. Read the failure reason in the CLI output or in the CloudFormation console under the stack's Events tab.
cdk destroy
cdk destroy deletes the stack and the resources it owns.
cdk destroy AppStackWhat happens to each resource depends on its removal policy. The bucket in our example uses RETAIN, so the stack is deleted but the bucket stays in your account. You must remove it yourself if you no longer want it.
Common errors
| Symptom | Likely cause |
|---|---|
| "This stack uses assets, so the toolkit stack must be deployed" | The environment is not bootstrapped. Run `cdk bootstrap`. |
| "Unable to resolve AWS account to use" | No credentials, or no `env` set. Check your AWS CLI profile. |
| Diff shows replacement of a resource | You changed a property that CloudFormation cannot update in place, often a name. |
| Destroy leaves a bucket behind | Its removal policy is `RETAIN`. |
Exercise
You change the logical ID of a bucket in your code, for example from 'UploadsBucket' to 'FilesBucket', and run cdk diff. What do you expect to see, and why does it matter for a bucket with RETAIN?
Show answer
The diff shows the old bucket removed and a new one added, because the construct ID determines the logical ID that CloudFormation tracks. With RETAIN, the old bucket and its data stay in the account but are no longer managed by the stack. The new bucket starts empty, so you must migrate data or keep the original ID.