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:
Installing the AWS CDK CLI
Once Node.js and NPM are installed, you can install the AWS CDK CLI with:cdk list(orcdk ls): List all stacks in the app.cdk synth(orcdk 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:- 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-libandconstructs).
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:-
Create and Activate the Virtual Environment:
On Windows, activate with:On Mac/Linux, use:
-
Install Dependencies:
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):Remember that bucket names must be globally unique. The CDK will append extra characters to ensure uniqueness.
Synthesizing the CloudFormation Template
To generate the CloudFormation templates from your CDK app, run:Configuring AWS Credentials
Before deploying, configure your AWS CLI credentials since the AWS CDK utilizes them for resource deployment. Run:It is best practice to create a dedicated IAM user with limited permissions for CDK deployments in production. Avoid using full administrative privileges.



Validating the Deployment with CDK Diff
Before deploying your changes, run a diff to compare your local template with what is currently deployed:Bootstrapping and Deploying the CDK Application
Certain environments require bootstrapping before deployment. If you encounter an error such as:


Verifying the Deployed Template
After deployment, run the diff again to ensure that your deployed state matches your local configuration:Destroying the Stack
When you’re finished with your demo, you can clean up your AWS environment by destroying the stack: