List of CDK commands

AWS CDK provides the following commands:

Initializing a new project

The cdk init command is used to initialize a new CDK project.

Example usage:

cdk init

Note: This command needs to be run on an empty directory.

Bootstraping the AWS environment

The cdk bootstrap command is used to prepare, (bootstrap), the AWS environment so that AWS CDK can deploy resources on one’s behalf.

See also bootstraping CDK.

Example usage:

cdk bootstrap 000000000000/eu-west-1

This will bootstrap the eu-west-1 region, on the AWS account with account number 000000000000.

Note: This command needs to be run when first setting up a new project or when adding a new environment to the CDK project, (e.g. a new region or a new environment).

Creating CloudFormation template(s)

The cdk synth command is used generate, (synthesize), CloudFormation template(s) from the CDK application code.

This command translates the high-level CDK constructs into a CloudFormation template (in YAML or JSON format), which can then be deployed to AWS.

Example usage:

cdk synth

Note: This command is run automatically as part of the cdk deploy process, but we can use it standalone to inspect the generated template before deployment.

Deploying CloudFormation template(s) to AWS

The cdk deploy command is used to deploy a CDK application to AWS.

This command synthesizes CDK code, (using cdk synth), into CloudFormation template(s) and then deploys those templates to the configured AWS account, creating or updating the specified resources.

Example usage:

cdk deploy

This will deploy all stacks in the CDK application to the specified AWS environment in the configuration.

To deploy a specific stack, use:

cdk deploy MyStackName

Note: This command should be run after bootstraping the AWS environment. It is the final step in the CDK workflow.

Listing all stacks inside the CloudFormation template(s)

The cdk list command is used to list all the stacks defined in the CDK application.

Example usage:

cdk list

Verifying changes before deploying

The cdk diff command is used to compare the currently deployed stack(s) in the AWS account with the changes defined in the CDK application.

It shows a detailed preview of what resources will be created, updated, or deleted during the next deployment.

Example usage:

cdk diff

This will display the differences for all stacks in the CDK application. To see changes for a specific stack, use:

cdk diff MyStackName

Note: Similar to terraform plan.

Diagnosing common problems

The cdk doctor command is used to diagnose common problems in the AWS CDK project and environment.

It checks for issues such as missing dependencies, misconfigurations, or unsupported Node.js versions, and provides actionable recommendations to resolve them.

Example usage:

cdk doctor

Deleting all stack resources

The cdk destroy command is used to delete all the AWS resources that were created by a specific CDK stack.

Example usage:

cdk destroy

This will prompt us to confirm the deletion of all stacks in the CDK application.

To destroy a specific stack, use:

cdk destroy MyStackName

Note: Calling cdk destroy does not necessarily mean that all resources will be deleted. E.g. When an S3 is created using CDK, unless removalPolicy has the default value of RETAIN, the bucket will be retained, orphaned from the stack.