[AWS] DevTools Evangelism CDK Edition

Published: (December 2, 2025 at 08:19 AM EST)
3 min read
Source: Dev.to

Source: Dev.to

Introduction

This is the fourth post in the Japan AWS Top Engineers Advent Calendar 2025.
We’ll discuss the AWS CDK, a commonly used tool for writing IaC code.

While CloudFormation, another AWS IaC tool, is easy to use with any text editor, it can lead to redundant code. The AWS CDK is a bit more difficult to set up, but it allows you to write IaC code efficiently with less code.

Japan AWS Top Engineers Advent Calendar 2025

What is the AWS CDK?

The AWS Cloud Development Kit (CDK) is a tool for creating Infrastructure as Code (IaC) that codifies infrastructure configuration.

Benefits of IaC

Reduced manual configuration errors and repetitive tasks

Infrastructure can also be defined using the AWS Management Console, but doing it manually each time is time‑consuming and prone to errors. By codifying the configuration, the same code can be reused, eliminating manual mistakes and hassle.

Ease of sharing and version control

Codified infrastructure can be shared within a team as a single block of code. Managing it with a version‑control system makes it easy to track differences, and it also enables testing that the code is defined correctly.

A rough overview can be seen in the following image:

IaC Overview

Differences between CloudFormation and CDK

CloudFormation allows detailed definitions, but the resulting IaC code can become very long, leading to increased writing time and reduced readability.

CDK achieves abstraction through constructs. By automatically configuring some definitions, IaC code can be written with fewer lines.

A rough overview of the process:

CDK Process Overview

Initializing AWS CDK

Creating a Project

mkdir my-project
cd my-project

Initialize the CDK project (TypeScript in this example):

cdk init app --language typescript

CDK Init Output

AWS CDK Implementation Example

Note (Test Environment)
I used Kiro (IDE) on a Windows PC.

Implementing an API using API Gateway, Lambda, and S3

The implementation was done with Amazon Q Developer and Kiro using Vibe coding.
The request was: “Define an API using API Gateway and Lambda. Write the Lambda function in TypeScript. The Lambda function should return a list of files in the S3 bucket.”
During development I consulted Kiro via chat to revise the S3 bucket name and Lambda function name.

Implementation Details

Folder Structure

The Lambda function code resides in the lambda folder, and the CDK IaC code resides in the lib folder.

Folder Structure

lib Folder

Stack definitions are placed in files inside this folder.

Imports, Classes, and Constructs

Required modules are imported, the class name and constructor are defined, and each resource is declared.

Imports and Constructs

S3 Bucket Definition

The bucket definition includes:

  • bucketName – fixed bucket name (optional; otherwise CDK generates a name like myprojectstack-mybucket-).
  • removalPolicyDESTROY (deletes the bucket when the stack is deleted).
  • autoDeleteObjectstrue (allows deletion even if the bucket contains objects).

S3 Bucket Definition

Lambda Function Definition

The Lambda definition includes:

  • functionName – fixed function name (optional; otherwise CDK generates a name like MyProjectStack-ListFilesFunction-).
  • runtime – Node.js 20.x.
  • handler – entry point in index.js.
  • code – source from the lambda/ directory.
  • environment – passes the bucket name as an environment variable.

Lambda Definition

The Lambda function retrieves a list of files in the S3 bucket, so read permission is granted to the bucket.

Lambda Permission

Back to Blog

Related posts

Read more »