Installing Node.js and the AWS CDK CLI
Before you begin, ensure that Node.js is installed on your machine. Node.js provides the JavaScript runtime required for the AWS CDK CLI, which is installed via npm. Download the appropriate installer for your operating system from the official Node.js website.
cdk --help shows a list of commands and options, the installation was successful.
─────────────────────────────
Overview of AWS CDK CLI Commands
The AWS CDK CLI offers several commands for managing your infrastructure as code:- cdk list: Lists all stacks in your CDK application.
- cdk synth: Synthesizes the CloudFormation template from your CDK app.
- cdk bootstrap: Sets up the CDK toolkit stack in your AWS account.
- cdk deploy: Deploys one or more stacks to your AWS account.
- cdk diff: Compares your local stack configuration with the deployed version.
- cdk destroy: Deletes the specified stack(s).
- cdk init: Initializes a new CDK project from a template.
Initializing a New CDK Project
Kickstart your project using the CDK CLI by running:- A Python virtual environment.
- A
requirements.txtfile that lists dependencies likeaws-cdk-libandconstructs. - A basic project structure with a
README, configuration file (cdk.json), and source files.
app.py:
cdk_stack.py, which defines a sample stack:
Setting Up Your Python Environment
To ensure that your dependencies remain isolated, set up a Python virtual environment:-
Create the virtual environment:
-
Activate the environment:
- On macOS/Linux:
- On Windows:
- On macOS/Linux:
-
Install required packages from
requirements.txt:
Configuring an S3 Bucket in Your CDK Stack
Enhance your CDK stack by adding an S3 bucket. Start by importing the S3 module and update your stack configuration as follows:Bucket names in AWS must be globally unique. If your chosen name is already in use, the AWS CDK may append extra characters to ensure uniqueness.
Configuring AWS Credentials
Before deploying your resources, configure your AWS CLI with the necessary credentials:


Previewing Changes and Deploying the Stack
Before deploying your changes, usecdk diff to inspect the differences between your local setup and the deployed stack:

Verifying and Cleaning Up Resources
After deploying, runcdk diff again to validate that no discrepancies exist between your configuration and what has been deployed:
