Python CDK Simplified: A Comprehensive Guide!

Yash Trivedi
6 min readNov 27, 2023

--

Introduction:

Welcome to the world of Infrastructure as Code (IaC) with Python CDK!

The AWS CDK is an open-source software development framework to define cloud infrastructure in code and provision it through AWS CloudFormation. It allows developers to define cloud resources using familiar programming languages, such as TypeScript, Python, Java, and more. This shift from traditional template-based approaches enables a more intuitive and efficient way of managing infrastructure as code.

In this comprehensive guide, we’ll dive into the exciting realm of the Python Cloud Development Kit (CDK), a game-changer for developers. Whether you’re a seasoned pro or just getting started with IaC, the Python CDK offers a user-friendly and Pythonic way to model and provision your cloud infrastructure. We will look into how to create an S3 bucket, Lambda Function, and how can use the existing CloudFormation template.

Let’s get started!

Installation of CDK:

Step 1: Prerequisites
Before diving into the CDK installation, ensure you have the following prerequisites:

  • Node.js: CDK is built on Node.js, so you need to have it installed. You can download it from here.
  • AWS CLI: Make sure you have the AWS Command Line Interface installed. You can find instructions on installation here.
  • Python and pip (Python package installer): We are going to code in Python so make sure they are installed. If not, install Python and pip.

Step 2: Create a Virtual Environment
Open your terminal and run:

python -m venv venv

Activate the virtual environment:

For Windows:

venv\Scripts\activate

For macOS/Linux:

source venv/bin/activate

Step 3: Install CDK in Python env
With the virtual environment activated, install the AWS CDK Python package:

pip install aws-cdk.core

Step 4: Install Constructs in Python env
In addition to the core CDK package and Node.js dependencies, you’ll also need to install the constructs package. Run the following command:

pip install constructs

This package provides the necessary tools to define and use constructs in your AWS CDK application.

Step 5: Verify Installation
Confirm that both the CDK Python package and Node.js dependencies are installed successfully by running:

cdk --version

This command should display the installed CDK version, ensuring that both the Python and Node.js components are ready for use.

Step 6: Initialize CDK Project
Now that the AWS CDK is installed, you can initialize the CDK project inside your project directory using the following command:

cdk init --language python

This command sets up a basic CDK project structure with Python as the chosen language. It generates essential files like cdk.json and a sample construct in Python.

Note: If you prefer, you can also install the AWS CDK globally using Node.js. Run the following command:

npm install -g aws-cdk

Please note that installing CDK globally might require elevated permissions (use sudo on Linux/macOS or run your command prompt as an administrator on Windows).

Creating an S3 Bucket:

import aws_cdk as cdk
from aws_cdk import aws_s3 as _s3
from constructs import Construct

class CdkPracticeStack(cdk.Stack):

def __init__(self, scope: Construct, construct_id: str, is_prod=False, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)

# Create an S3 Bucket
_s3.Bucket(
self,
"MyBucketId",
bucket_name="cdkpracticebucket45", # Set the name of the S3 bucket
encryption=_s3.BucketEncryption.S3_MANAGED, # Specify server-side encryption managed by Amazon S3
versioned=False, # Disable versioning for the S3 bucket
block_public_access=_s3.BlockPublicAccess.BLOCK_ALL # Block all public access to the S3 bucket
)

Understanding the Code:

  1. Importing AWS CDK:

The script begins by importing the necessary AWS CDK modules, providing the tools for defining cloud infrastructure in a programmatic way.

2. Initializing the S3 Bucket:

Inside the CdkPracticeStack class, the _s3.Bucket construct is utilized to create an S3 bucket. Parameters like bucket_name, encryption, versioned, and block_public_access are specified to customize the bucket's configuration.

3. S3 Bucket Configuration:

  • bucket_name="cdkpracticebucket45": Sets the name of the S3 bucket to "cdkpracticebucket45".
  • encryption=_s3.BucketEncryption.S3_MANAGED: Specifies server-side encryption managed by Amazon S3.
  • versioned=False: Indicates that versioning is disabled for the S3 bucket.
  • block_public_access=_s3.BlockPublicAccess.BLOCK_ALL: Blocks all public access to the S3 bucket.

Creating a Lambda Function:

import aws_cdk as cdk
from aws_cdk import aws_lambda as _lambda
from constructs import Construct

class CustomLambdaStack(cdk.Stack):

def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)

# Read Lambda Code from an external file
try:
with open("serverless_stacks/lambda_src/processor.py", mode="r") as f:
lambda_fn_code = f.read()
except OSError:
print("Unable to read Lambda Function Code")

# Define Lambda Function
lambda_fn = _lambda.Function(
self,
"LambdaFunction",
function_name="lambda_function", # Set the name of the Lambda function
runtime=_lambda.Runtime.PYTHON_3_7, # Specify the runtime as Python 3.7
handler="index.lambda_handler", # Specify the entry point to the Lambda function
code=_lambda.InlineCode(lambda_fn_code), # Inline code for the Lambda function
timeout=cdk.Duration.seconds(3), # Set the timeout for the Lambda function to 3 seconds
environment={
"LOG_LEVEL": "INFO" # Set an environment variable for the Lambda function
}
)

Understanding the Code:

  1. Importing AWS CDK:

The script begins by importing the necessary AWS CDK modules, providing the tools for defining cloud infrastructure in a programmatic way.

2. Lambda Function Code Retrieval:

The code attempts to read the Lambda function code from a file located at “serverless_stacks/lambda_src/processor.py”. If successful, the code is stored for later use. Otherwise, an error message is printed indicating the inability to read the Lambda function code.

3. Define Lambda Function:

  • A Lambda function is defined using the AWS CDK’s _lambda.Function construct.
  • The Lambda function is named “lambda_function” and is configured to run with the Python 3.7 runtime.
  • The entry point to the Lambda function is specified as “index.lambda_handler”.
  • The actual code for the Lambda function is provided inline, using the code read from the file.
  • A timeout of 3 seconds is set for the Lambda function.
  • An environment variable, “LOG_LEVEL”, is configured with the value “INFO”.

Use the Existing CloudFormation Template:

import aws_cdk as cdk
from constructs import Construct

class StackFromCloudformationTemplate(cdk.Stack):

def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)

# Import Existing Cloudformation Template
# This section imports existing Cloudformation resources into the CDK stack
resources_from_cfn_template = cdk.cloudformation_include.CfnInclude(
self,
'blogInfra',
template_file='stack_from_cfn/Sample_cfn_templates/create_s3_bucket_template.json'
)

# Retrieve the ARN of the Encrypted S3 Bucket from the Cloudformation Template
encrypted_bkt_arn = cdk.Fn.get_att("EncryptedS3Bucket", "Arn")

# Output ARN of Encrypted Bucket
# This section defines a CDK output to display the ARN of the encrypted S3 bucket
output_1 = cdk.CfnOutput(
self,
"EncryptedBucketArn",
value=f"{encrypted_bkt_arn.to_string()}",
description="ARN of Encrypted Bucket from Cloudformation Template"
)

Understanding the Code:

  1. Importing AWS CDK:

The script begins by importing the necessary AWS CDK modules, providing the tools for defining cloud infrastructure in a programmatic way.

2. Importing Existing CloudFormation Template:

  • Import existing AWS Cloudformation resources using cdk.cloudformation_include.CfnInclude.
  • The Cloudformation template file is specified as ‘stack_from_cfn/Sample_cfn_templates/create_s3_bucket_template.json’.

3. Retrieving ARN from Cloudformation Template:

Use cdk.Fn.get_att to retrieve the Amazon Resource Name (ARN) of the Encrypted S3 Bucket defined in the Cloudformation template.

4. Outputting ARN:

  • Define a CDK output (cdk.CfnOutput) to display the ARN of the Encrypted S3 Bucket.
  • The output value is set to the ARN retrieved from the Cloudformation template.
  • A description is provided for clarity.

Conclusion:

In this comprehensive exploration of the Python Cloud Development Kit (CDK), we’ve navigated through the key steps, from the installation process to hands-on examples of creating an S3 bucket, a Lambda function, and leveraging an existing CloudFormation template. The Python CDK emerges as a powerful ally, offering a Pythonic approach to defining and managing cloud infrastructure, making Infrastructure as Code (IaC) an intuitive and efficient experience.

This is just the tip of the iceberg!

The AWS CDK opens the door to a vast array of possibilities for cloud infrastructure management. As you delve deeper into the intricacies of the CDK, you’ll discover its versatility in handling various AWS services and scenarios.

If you’re eager to expand your knowledge and harness the full potential of AWS CDK with Python, there’s a world of resources waiting for you. For a more in-depth exploration and to further sharpen your CDK skills, check out AWS CDK Python Reference the official documentation.

May your coding adventures with Python CDK be both educational and rewarding!

Thanks for Reading!

--

--

Yash Trivedi

Associate Cloud Engineer at Rishabh Software, AWS and PCAP Certified.