List of CDK commands
AWS CDK provides the following commands:
cdk init- Used for Initializing a new project;cdk bootstrap- Used for bootstraping the AWS environment;cdk synth- Used for creating CloudFormation template(s);cdk deploy- Used for deploying CloudFormation template(s) to AWS;cdk list- Used for listing all stacks;cdk diff- Used for verifying changes before deploying;cdk doctor- Used for Diagnosing common problems;cdk destroy- Used for deleting all stack resources.
Initializing a new project
The cdk init command is used to initialize a new CDK project.
Example usage:
cdk initNote: 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-1This 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 synthNote: 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 deployThis will deploy all stacks in the CDK application to the specified AWS environment in the configuration.
To deploy a specific stack, use:
cdk deploy MyStackNameNote: 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 listVerifying 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 diffThis will display the differences for all stacks in the CDK application. To see changes for a specific stack, use:
cdk diff MyStackNameNote: 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 doctorDeleting 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 destroyThis will prompt us to confirm the deletion of all stacks in the CDK application.
To destroy a specific stack, use:
cdk destroy MyStackNameNote: 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.