Many people assume a “CDK TK cheat sheet” is a single list of every command and flag. The accurate answer is that CDK (AWS Cloud Development Kit) and TK (Terraform Kit, often used with CDK for Terraform) are separate tools, and a cheat sheet must cover both the core CDK commands and the Terraform-specific workflow. This page gives you the essential commands, the order to use them, and the common pitfalls that trip up beginners.
CDK vs. TK: Two Tools, One Workflow
CDK (AWS CDK) lets you define cloud infrastructure in code (TypeScript, Python, Java, etc.) and then synthesize it into CloudFormation templates. TK (CDK for Terraform, or cdktf) does the same but generates Terraform configuration instead. You use one or the other, not both at once. The commands below are for the AWS CDK, which is more common; if you use cdktf, the commands are similar but start with cdktf instead of cdk.
Core CDK Commands
cdk init app --language typescript– Creates a new CDK project with a sample stack.cdk bootstrap– Prepares your AWS account with a staging bucket for deployment. Run once per account/region.cdk synth– Generates the CloudFormation template from your code. Use this to review what will be deployed.cdk deploy– Deploys the stack to AWS. Add--allto deploy all stacks, or specify a stack name.cdk diff– Shows the difference between your current code and what is deployed.cdk destroy– Removes the deployed stack and its resources.cdk list– Lists all stacks in your app.
Why the Order Matters: Synth Before Deploy
Always run cdk synth before cdk deploy. Synthesis validates your code and shows you the exact CloudFormation template. This catches syntax errors and missing imports before you pay for resources. The cdk deploy command will also synth automatically, but seeing the output first helps you understand what you are deploying. For a quick check, cdk diff is the fastest way to see changes without deploying.
Common Flags and Options
Flags change how a command behaves. The most useful ones are:
--profile– Use a specific AWS profile from your credentials file. Example:cdk deploy --profile prod.--region– Override the AWS region. Example:cdk synth --region us-east-1.--output– Specify where to write the synthesized template. Default iscdk.out.--verbose– Print detailed logs for debugging.--require-approval– Control when CDK asks for approval before creating IAM roles or other security-sensitive resources. Options:never,any-change,broadening(default).
When You Need to Know the Context: Stacks and Environments
CDK apps contain one or more stacks. A stack is a unit of deployment. If you have multiple stacks, cdk deploy without arguments deploys all of them, but you can target one: cdk deploy MyStack. The environment (account and region) is set in the stack’s env property. If you do not set it, CDK uses your default AWS profile. For production, always specify the environment explicitly to avoid deploying to the wrong account.
Environment Variables You Can Use
AWS_PROFILE– Sets the AWS profile for all commands.AWS_REGION– Sets the region for all commands.CDK_DEFAULT_ACCOUNTandCDK_DEFAULT_REGION– Used in your code to reference the current account/region.
What Goes Wrong: Common Mistakes and How to Avoid Them
The most common mistake is forgetting to run cdk bootstrap before the first deploy. Without it, you get an error about a missing S3 bucket. Another frequent issue is using the wrong AWS profile; always check with aws sts get-caller-identity before deploying. Also, be careful with cdk destroy – it deletes all resources in the stack, including databases and S3 buckets, with no undo. For safety, use cdk diff before any deploy to see exactly what will change.
If You Get a “No Such File” Error
This usually means you are not in the project root directory. CDK expects to run from the directory containing cdk.json. Check that file exists, then run cdk list to see your stacks.
If Deploy Fails Midway
CDK will roll back the stack to its last known good state. Check the CloudFormation console for the exact error message. Common causes: insufficient IAM permissions, resource limits, or a typo in your code. Fix the issue and run cdk deploy again.
What to Do Next: Your Quick Reference Workflow
For a typical day of CDK work, follow this order:
cdk init app --language typescript(if starting a new project)cdk bootstrap(once per account/region)- Write your infrastructure code in
lib/. cdk synthto check the template.cdk diffto see changes.cdk deployto push to AWS.cdk destroywhen you are done testing.
Keep this list handy. The key rule: always synth before deploy, and always diff before destroy. That one habit prevents most surprises.
Related
- Chess for Beginners Cheat Sheet: Rules and Moves
- Flat Sheet Under or Over Blanket: Which Way Works
- How to Fold Origami from a Single Sheet
General information for everyday wellbeing, not medical advice. If something affects your health, sleep or mood for more than a short while, talk to a qualified professional. Read more
Spotted an error? Tell us and we will correct it (corrections policy).