Chapter 1: CDK Overview and Environment Setup
Chapter 1: CDK Overview and Environment Setup
Learning Objectives
- Understand what AWS CDK is and the problems it solves
- Understand the relationship between CDK and CloudFormation
- Master the setup of the CDK development environment
- Complete the creation of your first CDK application
Knowledge Point Summary
What is AWS CDK
The AWS Cloud Development Kit (CDK) is an open-source software development framework for defining cloud infrastructure resources using familiar programming languages. CDK supports TypeScript, JavaScript, Python, Java, C#/.Net, and Go.
Core Advantages of CDK
- Type Safety: Compile-time checks to reduce runtime errors
- IDE Support: Code completion, refactoring, debugging, etc.
- Code Reuse: Reuse infrastructure code through functions, classes, and packages
- Version Control: Manage infrastructure changes using tools like Git
- Testing Support: Ability to write unit and integration tests
CDK vs. Traditional IaC Tools
Relationship between CDK and CloudFormation
CDK is actually a high-level abstraction of CloudFormation, which is ultimately compiled into a CloudFormation template for deployment.
Environment Setup
System Requirements
Tool | Version Requirement | Description |
---|---|---|
Node.js | >= 18.x | CDK CLI runtime environment |
Python | >= 3.8 | Python CDK application development |
AWS CLI | >= 2.x | AWS access credential configuration |
CDK CLI | Latest version | CDK command-line tool |
Installation Steps
# 1. Install Node.js and npm (Node.js needs to be installed first)
# 2. Install AWS CLI
# pip install awscli
# 3. Configure AWS credentials
# aws configure
# 4. Install CDK CLI
# npm install -g aws-cdk
# 5. Verify installation
# cdk --version
# 6. Install Python CDK libraries
# pip install aws-cdk-lib constructs
Important Notes
- The CDK CLI requires a Node.js environment, even if you write your CDK application in Python.
- Ensure your AWS credentials are configured correctly, as CDK requires the appropriate AWS permissions.
- It is recommended to use a virtual environment to manage Python dependencies.
First CDK Application
Create a New Project
# Create a new CDK application
# cdk init app --language python
# Project structure
# my-cdk-app/
# ├── app.py # Application entry point
# ├── cdk.json # CDK configuration file
# ├── requirements.txt # Python dependencies
# └── my_cdk_app/
# ├── __init__.py
# └── my_cdk_app_stack.py # Stack definition file
Hello World Example
#!/usr/bin/env python3
# app.py - Application entry point
import aws_cdk as cdk
from my_cdk_app.my_cdk_app_stack import MyCdkAppStack
app = cdk.App()
MyCdkAppStack(app, "MyCdkAppStack")
app.synth()
# my_cdk_app/my_cdk_app_stack.py - Stack definition
from aws_cdk import (
Duration,
Stack,
aws_s3 as s3,
)
from constructs import Construct
class MyCdkAppStack(Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
# Create a simple S3 bucket
bucket = s3.Bucket(
self,
"MyFirstBucket",
bucket_name="my-first-cdk-bucket-unique-name",
versioned=True, # Enable versioning
removal_policy=cdk.RemovalPolicy.DESTROY # For development environments
)
# Output the bucket name
cdk.CfnOutput(
self,
"BucketName",
value=bucket.bucket_name,
description="Name of the S3 bucket"
)
CDK Application Architecture
Common CDK Commands
# View available commands
# cdk --help
# List all stacks
# cdk list
# Synthesize CloudFormation template
# cdk synth
# View changes to be deployed
# cdk diff
# Deploy a stack
# cdk deploy
# Destroy a stack
# cdk destroy
# Initialize Bootstrap
# cdk bootstrap
Bootstrap Explained
Bootstrap is the initialization process for CDK, which creates the necessary infrastructure resources in your AWS account (such as an S3 bucket for storing deployment assets). This only needs to be done once per AWS account/region combination.
Deployment Flow
Hands-on Practice
- Set up the environment: Follow the steps above to install the CDK development environment.
- Create a project: Use
cdk init
to create your first Python CDK application. - Modify the code: Add an S3 bucket to the Stack.
- Synthesize the template: Use
cdk synth
to view the generated CloudFormation template. - Deploy the application: Use
cdk deploy
to deploy to AWS. - Verify the result: Check the created resources in the AWS console.
- Clean up resources: Use
cdk destroy
to clean up the created resources.
Cost Reminder
Even a simple S3 bucket can incur small charges. Remember to clean up resources promptly after completing the exercise.
Troubleshooting
Common Issues
Issue | Solution |
---|---|
cdk command not found | Ensure the CDK CLI is installed correctly. |
AWS credentials not configured | Run aws configure to set up credentials. |
Bootstrap required | Run cdk bootstrap to initialize the environment. |
Insufficient deployment permissions | Check IAM user permissions. |
Debugging Tips
# 1. Use the synth command to view the generated template
# cdk synth --output ./cdk.out
# 2. Use the diff command to view changes
# cdk diff
# 3. Enable verbose logging
# cdk deploy --verbose
# 4. Specify a specific stack
# cdk deploy MySpecificStack
This chapter lays the foundation for learning CDK. Make sure you understand the basic concepts of CDK and have successfully set up your development environment.