Skip to main content
In this guide, you’ll learn how to use the AWS Cloud Development Kit (CDK) to create and manage AWS resources. We’ll cover installing prerequisites, initializing a CDK project, exploring generated files, and then deploying, verifying, and cleaning up a simple S3 bucket infrastructure.

Prerequisites

Before you begin with AWS CDK, ensure that Node.js is installed on your machine. Node.js comes with NPM (Node Package Manager), which is required to install the AWS CDK CLI tool. After installing Node.js, run these commands to install and verify the AWS CDK CLI:
Ensure Node.js is installed correctly by referring to the Node.js documentation. The site usually detects your operating system and provides the appropriate download options.
The image shows the Node.js download page, offering options to download the LTS and Current versions for Windows (x64). It also mentions available security releases.

Installing the AWS CDK CLI

Once Node.js and NPM are installed, you can install the AWS CDK CLI with:
Then, verify your installation by running:
You should see a list of available commands such as:
  • cdk list (or cdk ls): List all stacks in the app.
  • cdk synth (or cdk synthesize): Generate the CloudFormation template.
  • cdk bootstrap: Deploy the CDK toolkit stack into your AWS environment.
  • cdk deploy: Deploy your stack(s) to your AWS account.
  • cdk destroy: Remove the deployed stack(s).
  • cdk diff: Compare your local template with the deployed stack.

Initializing a New CDK Project

To quickly get started with a new project, initialize a boilerplate application with:
If you prefer a sample project template that includes a demo application, you can run:
This command creates multiple files and directories to jumpstart your CDK project:
  • Python Virtual Environment: Isolates project dependencies.
  • Activation Script: A file like source.bash (or equivalent for Mac/Linux) to activate the virtual environment.
  • requirements.txt: Lists third-party dependencies (typically including aws-cdk-lib and constructs).
Example requirements.txt content:
  • README: Contains instructions for installing dependencies and running commands:
  • cdk.json: Contains project configuration. For example:
  • app.py: Initializes your CDK application and instantiates a stack:
  • Stack File (e.g., cdk/cdk_stack.py): Initially defines resources like an SQS queue and an SNS topic with a subscription:
In this demo, we will comment out the SQS/SNS implementation to focus on creating an S3 bucket.

Configuring the Python Virtual Environment

Set up your Python virtual environment and install the necessary packages:
  1. Create and Activate the Virtual Environment:
    On Windows, activate with:
    On Mac/Linux, use:
  2. Install Dependencies:
Once these steps are completed, you’re ready to work on your CDK project.

Editing the CDK Stack for an S3 Bucket

For this demo, we’ll modify the CDK stack to create an S3 bucket. First, update the import statements to include AWS S3 (and optionally AWS KMS if you need encryption):
Then update the stack definition to include the S3 bucket:
Remember that bucket names must be globally unique. The CDK will append extra characters to ensure uniqueness.
For advanced configurations such as encryption using a KMS key, you might use:
In this demo, however, we use the basic bucket configuration.

Synthesizing the CloudFormation Template

To generate the CloudFormation templates from your CDK app, run:
This command synthesizes templates that detail the AWS resources like the S3 bucket and any related policies. A sample snippet of the generated output might be:

Configuring AWS Credentials

Before deploying, configure your AWS CLI credentials since the AWS CDK utilizes them for resource deployment. Run:
You will be prompted to enter your AWS Access Key ID, AWS Secret Access Key, default region, and output format. For example:
It is best practice to create a dedicated IAM user with limited permissions for CDK deployments in production. Avoid using full administrative privileges.
The following screenshots illustrate the process of creating an IAM user and generating access keys:
The image shows the AWS IAM console where a user is specifying details to create a new user, including a field for the username and options for providing access to the AWS Management Console.
The image shows an AWS IAM console screen for creating a user, with details like user name, permissions summary, and an option to add tags. The "Create user" button is visible at the bottom.
The image shows an AWS console screen for creating an access key, with options for different use cases like CLI, local code, and third-party services.

Validating the Deployment with CDK Diff

Before deploying your changes, run a diff to compare your local template with what is currently deployed:
The output will indicate which resources are set to be created, modified, or deleted. Since the S3 bucket hasn’t been deployed yet, the diff output should reflect that a new S3 bucket will be created.

Bootstrapping and Deploying the CDK Application

Certain environments require bootstrapping before deployment. If you encounter an error such as:
Run the bootstrap command:
After successful bootstrapping, deploy your stack with:
During deployment, you will see messages indicating synthesis progress, publishing steps, and CloudFormation resource creation. An example output might include:
Once the deployment is complete, verify the S3 bucket in the AWS Management Console by refreshing the Buckets page.
The image shows an Amazon S3 bucket interface with no objects listed, and options to upload or manage files.
Also, inspect the CloudFormation console to ensure the stack was deployed correctly:
The image shows an AWS CloudFormation console with a list of stacks, their statuses, and creation times. One stack has a "DELETE_FAILED" status, while others are marked "CREATE_COMPLETE."
The image shows an AWS CloudFormation console with a stack named "CdkStack" that has completed creation. It lists resources like "My-kodekloud-cdk-bucket" and "CDKMetadata" with a status of "CREATE_COMPLETE."

Verifying the Deployed Template

After deployment, run the diff again to ensure that your deployed state matches your local configuration:
Expected output:
This confirms that the deployed environment is in sync with your CDK application.

Destroying the Stack

When you’re finished with your demo, you can clean up your AWS environment by destroying the stack:
You’ll be prompted to confirm deletion:
After destruction, confirm in the AWS console that both the CloudFormation stack and the S3 bucket have been removed.
The image shows an Amazon S3 dashboard with a list of buckets, their regions, access settings, and creation dates.

Conclusion

This guide provided an overview of using AWS CDK to manage AWS resources. You learned how to install and set up AWS CDK, initialize a Python project, modify the CDK stack to create an S3 bucket, deploy your infrastructure using CloudFormation, verify your deployment, and finally clean up resources. Happy building with AWS CDK!

Watch Video