Short answer: cdk synth runs your CDK app and writes the result, CloudFormation templates and assets, to cdk.out. It deploys nothing. cdk diff synthesises too, then compares the result with what's deployed and prints what would change. cdk deploy synthesises, uploads assets, and creates and runs a CloudFormation change set: it's the only one of the three that changes your AWS account.
Side by side
| Command | What it does | Talks to AWS? | Changes your resources? |
|---|---|---|---|
cdk synth | Runs your app and writes the cloud assembly to cdk.out. With one stack (or one named), also prints its template as YAML | Only for context lookups that aren't cached in cdk.context.json yet | No |
cdk diff | Synthesises, then compares with the deployed stack and prints the differences | Yes: by default it creates a change set to find out what would really be replaced | No |
cdk deploy | Synthesises, publishes assets, then creates and runs a change set (or updates directly with --method=direct) | Yes | Yes |
Context lookups are things like Vpc.fromLookup(): during synthesis the CLI asks your account and caches the answer in cdk.context.json. Commit that file, along with cdk.json, so every machine and CI run synthesises the same template.
What's in cdk.out
cdk.out is the cloud assembly: everything needed to deploy. This is what a one-stack app with a Lambda function writes (aws-cdk-lib 2.278.0):
| File | What it is |
|---|---|
asset.a9a29b16… | A copy of the function's code, named by its hash (zipped when it's published) |
cdk.out | The cloud assembly schema version |
manifest.json | The artifacts: stacks, asset manifests, the tree |
ShopStack.template.json | The CloudFormation template |
ShopStack.assets.json | Where each asset goes when it's published |
ShopStack.metadata.json | Metadata for the stack |
tree.json | Your construct tree |
validation-report.json | Results of policy validation |
The asset manifest says where each asset is uploaded: for file assets, a bucket the bootstrap stack created (cdk-hnb659fds-assets-<account>-<region>, with the default qualifier). Bootstrapping is why cdk deploy needs cdk bootstrap once per account and Region.
The CLI README is clear that a cloud assembly is a trust boundary: don't deploy one from a source you don't trust.
Reading the synthesised template
The template is plain CloudFormation, so you can check exactly what CDK will ask for. The Lambda function above becomes:
"Handler886CB40B": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"S3Bucket": "cdk-hnb659fds-assets-111111111111-eu-west-2",
"S3Key": "a9a29b166f3caa6456385bc20262cd2688cac2ea5b7dca3636ca84eda6aea404.zip"
},
"Handler": "index.handler",
"Runtime": "nodejs22.x",
"Role": { "Fn::GetAtt": ["HandlerServiceRoleFCDC14AE", "Arn"] }
},
"DependsOn": ["HandlerServiceRoleFCDC14AE"]
}
Things worth knowing when you read one:
- Logical IDs like
Handler886CB40Bcome from the construct's path plus a hash. Rename or move the construct and the ID changes, which CloudFormation treats as a new resource: see the CDK diff explainer. - Resources you didn't write. The L2
Functionadded an IAM role (HandlerServiceRole…) for you. The template shows everything that will exist, not just what's in your code. - References are CloudFormation functions (
Fn::GetAtt,Ref), resolved at deploy time. That's whycdk diffshows them as${Handler/ServiceRole.Arn}. BootstrapVersionandCheckBootstrapVersionare a parameter and a rule CDK adds to fail early if the account's bootstrap stack is too old.cdk diffhides the rule (and theAWS::CDK::Metadataresource) unless you pass--strict.
cdk deploy, step by step
- Synthesise, as above.
- Build and publish assets to the bootstrap bucket and ECR repository.
- Compare the template and tags with the deployed stack, and skip the deploy if nothing changed (
--forcedeploys anyway). - Create a change set called
cdk-deploy-change-set. The CLI creates it first so the approval step can use an accurate diff. - Ask for approval if the change needs it. By default (
--require-approval=broadening) that's any change that broadens IAM permissions or security group rules. With no terminal, as in CI, a deploy that needs approval fails; pass--yesto confirm automatically. - Run the change set, or delete it if you said no.
--method=prepare-change-set creates the change set without running it, so someone can review it first. --method=direct skips the change set and updates the stack straight away.
If a resource fails to create or update, CloudFormation rolls the stack back. --no-rollback keeps what succeeded so you can fix your code and retry, but it can't replace resources: if the deploy needs a replacement, the CLI offers a normal deployment instead.
Synth in CI: what this site's CDK check caught
Several Labs tools (alarms, SLOs, RUM, dashboards, IAM policies, ARNs and the CDK translator) generate CDK in TypeScript and Python: about a hundred snippets. On every change, CI wraps each generated snippet in its own stack, synthesises it in both languages against the real aws-cdk-lib, and compares the two CloudFormation templates. Nothing talks to AWS.
The first run found bugs that type-checking alone had missed. The worst was in Python. Top-level props are keyword arguments in snake_case, but a nested struct written as a plain dict must keep its camelCase keys:
# Kept: a typed class, or a dict with camelCase keys
s3.Bucket(self, "Typed", lifecycle_rules=[
s3.LifecycleRule(expiration=Duration.days(30),
noncurrent_version_expiration=Duration.days(7))])
s3.Bucket(self, "Camel", lifecycle_rules=[
{"expiration": Duration.days(30),
"noncurrentVersionExpiration": Duration.days(7)}])
# Lost: a snake_case key inside a dict
s3.Bucket(self, "Snake", lifecycle_rules=[
{"expiration": Duration.days(30),
"noncurrent_version_expiration": Duration.days(7)}])
With aws-cdk-lib 2.278.0 the third bucket synthesises without an error or warning: its rule keeps ExpirationInDays: 30 but has no NoncurrentVersionExpiration. (If the dropped key is the only one, the rule is left empty and synth fails instead, which at least tells you.) The SLO builder's Python failed to synthesise for that reason. The RUM helper and the translator lost settings silently, and only comparing the TypeScript and Python templates showed it. Now the generators keep camelCase keys in dicts, and the CDK translator warns when it sees snake_case keys inside a dict.
Then lint the templates
Synth proves your code runs, not that every value in the template is one CloudFormation will accept. cfn-lint checks templates against CloudFormation's resource provider schemas, plus extra checks: values a property allows, ranges and patterns, deprecated settings and best practices. It runs locally against schemas bundled with it, so it needs no AWS account or credentials:
pip install cfn-lint
cfn-lint cdk.out/*.template.json
It exits with code 0 if it finds nothing. Otherwise the code says what it found: 2 for errors, 4 for warnings, 8 for informational findings, combined when there's more than one kind. By default any finding fails, so it can gate CI as it is. (Informational rules don't run unless you add --include-checks I.)
This site's CI now lints all 202 templates the CDK check synthesises. With the default rules, the first run found nothing to fix and one warning to switch off: W3005, on the one Lambda function in our templates. CDK's lambda.Function added a DependsOn on its role alongside the Fn::GetAtt that already implies it. Switch off rules one at a time, with a comment saying why, in a .cfnlintrc:
templates:
- cdk.out/*.template.json
ignore_checks:
# CDK's lambda.Function adds a DependsOn next to Fn::GetAtt on its role.
- W3005
To check it really catches problems, we gave it a template with a Lambda runtime retired years ago (nodejs8.10). It failed with E2533 and named the runtime to move to.
So synth and cfn-lint in CI catch code that doesn't run, doesn't produce what you meant, or asks for values CloudFormation won't accept. They can't catch what only your account knows: missing permissions, a name that's already taken, a limit. That's what cdk diff against the real stack, and the deploy itself, are for.
Diff in CI before you deploy
cdk diff doesn't fail by default. To make a pipeline stop, use --fail-on:
cdk diff --fail-on=destructive
cdk diff --fail-on=broadening
destructivefails if anything would be replaced, deleted or orphaned, and lists those resources.broadeningfails on wider IAM permissions or security group rules.--fail(the same as--fail-on=any-change) fails on any difference.
In CI, give the job credentials that can use the deploy role and add --method=change-set, so diff fails rather than falling back, with only an info message, to a template comparison that over-reports replacements. Or combine the two steps: cdk deploy --require-approval=destructive deploys everything else unattended but stops before anything is replaced or deleted.
When the diff does flag something, paste it into the CDK diff explainer: it puts replacements and data loss first and says what to check.
