May 23, 2023

How to Install AWS CDK: Easy Step-by-Step Guide

Share this

By Andrei Maksimov

May 23, 2023


Enjoy what I do? Consider buying me a coffee ☕️

Are you tired of scrolling through endless lines of YAML code to manage your AWS Infrastructure? Enter AWS Cloud Development Kit (CDK), a powerful open-source framework that revolutionizes how you define and manage your resources. AWS CDK allows you to write infrastructure as code using familiar programming languages, making it easier and more efficient. In this guide, you’ll learn how to install AWS CDK, create a project, and manage your AWS infrastructure. Say goodbye to the complexities of traditional infrastructure management, and get ready to embrace the future of cloud development!

Short Summary

  • AWS CDK is an open-source framework that enables developers to define cloud infrastructure as code.
  • It requires Node.js version 18.0.0 or later and the AWS Command Line Interface (AWS CLI) with credentials set up for installation.
  • After installing, users can create their first project using cdk init command, manage resources using multiple commands such as cdk deploy, customize constructs by writing custom ones, and switch between different environments/configurations through environment variables & JSON configuration files.

Understanding AWS CDK

Install AWS CDK - Developer look

AWS CDK, or the AWS Cloud Development Kit, is an open-source framework that enables developers to define cloud infrastructure as code using familiar programming languages like TypeScript, JavaScript, Python, Java, C#, and Go. Instead of writing hundreds of lines of YAML or JSON, AWS CDK allows you to use the power of your favorite programming language to create, manage, and update your AWS resources.

AWS CDK Features

AWS CDK offers many features, such as the CDK Toolkit, AWS CDK CLI, support for multiple programming languages, and a vast collection of reusable infrastructure components called the AWS Construct Library. These constructs, available at different levels (L1, L2, and L3), enable you to rapidly create and manage your resources, from low-level AWS CloudFormation resources to more opinionated patterns for common tasks.

The Construct Hub adds value to the CDK ecosystem. It enables users to explore additional constructs from AWS, third parties, and open-source communities.

CloudFormation and AWS CDK

While AWS CloudFormation is a declarative service that requires you to define the final state of your infrastructure statically, AWS CDK is an imperative framework that leverages the power of programming languages to define the desired state of your resources. With AWS CDK, you write code that generates CloudFormation templates, which are then deployed as CloudFormation stacks.

This approach allows you to harness the full potential of your favorite programming language and enjoy the benefits of code reuse, versioning, and modularization while still using AWS CloudFormation to manage your resources.

Prerequisites for Installing AWS CDK

Install AWS CDK - node version

Before you can dive into the world of AWS CDK, you need to take care of a couple of prerequisites. First, ensure you have Node.js version 18.0.0 or later installed on your system.

Second, set up the AWS Command Line Interface (AWS CLI) with the appropriate credentials to enable programmatic access to your AWS account. With these two requirements in place, you can install the AWS CDK and harness its power.

Install Node.js

To install Node.js, you can either download the installer from the official Node.js website or use package managers like npm or nvm. For Windows users, downloading the MSI installation package from the Node.js website is the most straightforward method. In contrast, Linux and macOS users can use package managers to install the appropriate version of Node.js.

Don’t forget to verify your installation by checking the Node.js version in your terminal or command prompt.

Set up AWS CLI

Setting up the AWS CLI is essential for interacting with AWS services and managing your AWS CDK infrastructure. First, download and install the AWS CLI for your operating system.

Next, configure the AWS CLI by following the instructions in the AWS Command Line Interface User Guide to create a named profile with the necessary access keys, secret keys, and default region. For better security, we encourage you to install aws-vault.

With your AWS CLI configured you can now manage your AWS resources using the command line.

Installing AWS CDK

Install AWS CDK - cdk version

Installing the AWS CDK is a breeze. Run the following command in your terminal or command prompt to install AWS CDK:

npm install -g aws-cdk

Once the installation is complete, you can verify it by running cdk --version, which should display the installed AWS CDK version number. To explore the available AWS CDK commands, use the cdk --help command.

Verifying AWS CDK Installation

To confirm that the AWS CDK has been successfully installed on your system, run the cdk --version command in your terminal or command prompt. This command should display the AWS CDK version number, indicating that the installation was successful.

With the AWS CDK installed and verified, you’re now ready to start building and managing your cloud infrastructure using the power of your favorite programming language.

Exploring AWS CDK Commands

The AWS CDK offers a wealth of commands to help you manage your infrastructure. To explore the available commands, simply run cdk --help in your terminal or command prompt. This command will display a list of commands and their descriptions, providing you with a comprehensive overview of the capabilities of the AWS CDK CLI.

Some common AWS CDK commands include cdk deploy, cdk synth, and cdk destroy, which are used to deploy, synthesize, and destroy your infrastructure, respectively.

Creating Your First AWS CDK Project

Install AWS CDK - cdk init

Now that you have the AWS CDK installed, it’s time to create your first CDK project. This project will serve as the foundation for your AWS CDK app, allowing you to define and manage your infrastructure resources.

To create a new CDK project, you’ll use the cdk init command with the desired template and programming language. The resulting project will include a sample app, which already contains a stack with some resources, giving you a head start in building your cloud infrastructure.

Check the AWS CDK Python Lambda Deployment Example article for the AWS Lambda deployment guide.

Initializing a CDK Project

To initialize a new CDK project, navigate to the desired directory in your terminal or command prompt and run the cdk init command with the appropriate template and programming language. For example, to create a Python-based CDK project, you would run the following command:

mkdir first-project
cd first-project
cdk init app --language=python

This command generates the necessary files and directories for your CDK project, including the first_project folder containing your main stack, the file serving as the entry point of your application, and a tests folder for any tests you may create.

Understanding Project Structure

A typical AWS CDK project consists of several key components. The first_project folder containing your main stack, the file serving as the entry point of your application. You may optionally create the constructs folder to store any custom constructs representing reusable components of your infrastructure.

By understanding the structure of your CDK project, you can more effectively organize your code and manage your resources.

Building and Deploying Your AWS CDK App

Install AWS CDK - cdk demo app

Once your CDK project is set up, the next step is to build and deploy your AWS CDK app. This process involves synthesizing CloudFormation templates in YAML format, saving them in the cdk.out folder as JSON, bootstrapping your stack, and deploying your app to your AWS account.

By following these steps, you can create and manage your AWS infrastructure using the power of AWS CDK and your preferred programming language.

Synthesizing CloudFormation Templates

To synthesize CloudFormation templates for your AWS CDK app, run the cdk synth command in your terminal or command prompt. This command generates YAML-formatted CloudFormation templates for each stack in your app, encompassing all the resources you’ve defined in your CDK code.

These templates are then saved in the cdk.out folder as JSON files, which can later be deployed using the cdk deploy command.

It is essential to activate Python virtual environment and install the required modules to build the AWS infrastructure you’re interested in. In our example, we’ll install aws_core.aws_sqs module to support demo project code:

source .venv/bin/activate
pip install requirements.txt

Now, modify the first_project/ file and paste the following content there:

from aws_cdk import (
    aws_sqs as sqs,
from constructs import Construct
class FirstProjectStack(Stack):
    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)
        queue = sqs.Queue(
            self, "FirstProjectQueue",

Finally, you can synthesize the CFN template:

cdk synth

Bootstrapping AWS CDK

Before deploying your AWS CDK app, you’ll need to bootstrap your stack. Bootstrapping involves creating dedicated Amazon S3 buckets and other containers required for deploying your app using AWS CloudFormation.

To bootstrap your stack, run the cdk bootstrap command in your terminal or command prompt. This command deploys a CDKToolkit CloudFormation stack into the specified environment, provisioning the necessary resources for your AWS CDK app.

cdk bootstrap

Deploying the App

Install AWS CDK - cdk deploy

You’re ready to deploy your AWS CDK app with your synthesized CloudFormation templates and bootstrapped stack. To do this, run the cdk deploy command in your terminal or command prompt.

cdk deploy

This command deploys your CDK app to your AWS account, creating and updating the necessary AWS resources as defined in your CloudFormation templates. Once the deployment is complete, you can monitor and manage your deployed resources using the AWS Management Console or AWS CLI.

Managing Your AWS CDK Infrastructure

As your AWS CDK app grows and evolves, you’ll need to manage your infrastructure effectively. This includes updating existing resources, destroying resources when they’re no longer needed, and monitoring changes in your infrastructure.

By mastering these management tasks, you can ensure that your AWS CDK app remains efficient, secure, and cost-effective.

Updating Your Infrastructure

To update your AWS CDK infrastructure, make the necessary changes to your CDK code and run the cdk deploy command again. This command updates your CloudFormation templates and deploys the updated resources to your AWS account, ensuring that your infrastructure reflects the latest version of your code.

Remember to keep an eye on the changes you make to your infrastructure to ensure that they align with your organization’s requirements and best practices.

Destroying Your Infrastructure

Install AWS CDK - cdk destroy

When you no longer need certain resources in your AWS CDK app, you can destroy them using the cdk destroy command. This command deletes the specified stack and all its associated resources unless they have been marked with a Deletion Policy to be retained.

cdk destroy

Be cautious when using this command, as it can permanently lose data or resources. Always double-check your stack and resource configurations before running the cdk destroy command.

Monitoring Changes

Monitoring changes in your AWS CDK infrastructure is crucial for maintaining security, performance, and compliance. AWS offers several tools to help you detect and track changes in your infrastructure, such as AWS CloudFormation Stack Drift Detection and AWS Security Hub.

Utilizing these tools lets you quickly identify unauthorized modifications or misconfigurations in your infrastructure and take the necessary corrective actions to ensure your AWS CDK app remains secure and efficient.

Customizing and Extending AWS CDK Constructs

As your AWS CDK app grows, you may find that the built-in constructs provided by the AWS Construct Library are insufficient for your needs. In such cases, you can create custom constructs or extend existing constructs to tailor your AWS CDK app to your specific requirements.

By leveraging the power of custom constructs and the extensive resources available in the Construct Hub, you can build a highly customized and powerful cloud infrastructure that meets your organization’s unique needs.

Writing Custom Constructs

Install AWS CDK - Construct Hub

To create custom constructs in AWS CDK, you’ll need to extend the Construct class and encapsulate all the necessary information for AWS CloudFormation to generate the component. Custom constructs can be simple or complex, depending on your requirements, and can be shared and reused across multiple projects or even organizations.

By writing custom constructs, you can tailor your AWS CDK app to your specifications and create a unique and powerful cloud infrastructure.

AWS Construct Library

The AWS Construct Library is a vast collection of reusable constructs representing various AWS resources and services. Using these constructs, you can quickly and efficiently define your cloud infrastructure in your AWS CDK app, allowing you to focus on building and deploying your application.

The AWS Construct Library also provides a consistent way to define your resources, making it easier to maintain and update your app as your infrastructure evolves.

Exploring the Construct Hub

Install AWS CDK - Exploring Construct Hub

In addition to the AWS Construct Library, you can explore the Construct Hub, a comprehensive resource for discovering, using, and sharing constructs created by AWS, AWS Partner Network partners, third parties, and the open-source CDK community. The Construct Hub offers a wealth of constructs for various use cases, enabling you to extend the capabilities of your AWS CDK app and build a truly customized cloud infrastructure.

By leveraging the power of the Construct Hub, you can enhance your AWS CDK app with cutting-edge constructs and stay ahead of the curve in cloud development.

Working with Multiple Environments and Configurations

A computer screen with multiple environments and configurations being set up
Source: Best practices for developing cloud applications with AWS CDK

As your organization grows, you may need to manage multiple environments and configurations for your AWS CDK app. This can include separate configurations for development, staging, and production environments or different settings for various teams within your organization.

Working with multiple environments and configurations ensures your AWS CDK app is flexible and adaptable, allowing you to scale and evolve your infrastructure as needed.

Environment Variables

Environment variables are an excellent way to manage multiple configurations in your AWS CDK app. You can set environment variables in your operating system or through a configuration file and then use them in your CDK code to control specific settings or configurations.

Using environment variables allows you to easily switch between different configurations without modifying your code, making it easier to manage and maintain your AWS CDK app across multiple environments.

JSON Configuration Files

JSON configuration files offer another powerful method for managing multiple configurations in your AWS CDK app. You can create a JSON file for each environment or configuration and then load the appropriate file at runtime based on the current environment or context.

JSON configuration files can store environment variables, resource configurations, and other settings, making it easy to manage and switch between configurations in your AWS CDK app.


In this guide, you’ve learned how to install and use the AWS Cloud Development Kit (CDK) to define, build, and manage your cloud infrastructure. From creating your first CDK project and deploying your app to managing your infrastructure and leveraging custom constructs, the AWS CDK offers a powerful and flexible solution for managing your cloud resources. With its support for popular programming languages and extensive resources like the Construct Library and Construct Hub, the AWS CDK empowers you to build and manage your cloud infrastructure with ease and scalability. So, leap and transform your cloud development experience with AWS CDK today!

Frequently Asked Questions

How do I add AWS CDK to an existing project?

To add AWS CDK to your existing project, first, you need to run the command npx cdk init app --language=typescript to generate a new application at the root of your project.

Then you need to update your package.json file by changing the line "app": "npx ts-node -- prefer-ts-exts bin/cdk. ts" to "app": "dist/infra/main...".

Finally, install both aws-cdk and aws-cdk-lib constructs with the commands npm i --save-dev aws-cdk and npm i aws-cdk-lib constructs. By following these steps, you will have successfully added AWS CDK to your existing project.

Is AWS CDK free?

Yes, the AWS CDK is free to use. However, any usage of AWS resources associated with the AWS CDK may incur charges depending on your usage.

Andrei Maksimov

I’m a passionate Cloud Infrastructure Architect with more than 15 years of experience in IT.

Any of my posts represent my personal experience and opinion about the topic.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Related Posts

Install Jupyter Notebook: Easy Step-by-Step Guide
What is AWS Free Tier? An In-Depth Look at Cost Savings
Mastering AWS API Gateway: A Comprehensive Guide to Boost Your Cloud Performance
Mastering AWS DynamoDB: An Easy And Complete Guide

Subscribe now to get the latest updates!