This article is a machine translation of the contents of the following URL, which I wrote in Japanese:
https://qiita.com/Nana_777/items/f5c2366d092226179477
Introduction
This is the fourth post in the Japan AWS Top Engineers Advent Calendar 2025.
This time, we'll discuss the AWS CDK, a commonly used tool for writing IaC code.
LoudFormation, another AWS IaC tool, is easy to use with any text editor, but it can lead to redundant code. While AWS CDK is a bit more difficult to set up, it allows you to write IaC code efficiently with less code.
This article explains how to set up and deploy the AWS CDK environment.
↓ Click here for the Japan AWS Top Engineers Advent Calendar 2025
https://qiita.com/advent-calendar/2025/aws-top-engineers
What is the AWS CDK?
The AWS Cloud Development Kit (CDK) is a tool for creating Infrastructure as Code (IaC) code that codifies infrastructure configuration.
Benefits of IaC
Reduced manual configuration errors and repetitive tasks
Infrastructure configuration can also be defined using the AWS Management Console. However, repeatedly configuring each service manually is time-consuming and introduces the risk of configuration errors.
By codifying infrastructure configuration, the same code can be reused repeatedly, eliminating manual configuration errors and hassle.
Ease of sharing and version control through codification
Codifying infrastructure configuration allows it to be shared within a team as a single block of code.
In addition, managing it with a version control tool makes it easy to manage differences.
Furthermore, it also makes it possible to test whether the code is correctly defined.
↓ A rough overview can be seen in the following image.

Differences between CloudFormation and CDK
CloudFormation is another AWS IaC tool.
While it allows for detailed definitions, the detailed nature of the IaC code can make it very long.
This can lead to issues such as increased code writing time and reduced readability.
CDK achieves abstraction through constructs. By automatically configuring some definitions through abstraction, IaC code can be written with fewer lines.
↓ A rough overview of the process looks like this:

Initializing AWS CDK
Creating a Project
Create a folder for your project and navigate to that folder hierarchy.
mkdir my-project
cd my-project
To initialize your AWS CDK project, run "cdk init app."
In this example, we're specifying Typescript as the programming language, so we're specifying "--language typescript" as the option.
cdk init app --language typescript
AWS CDK Implementation Example
:::note warn
Test Environment
I used Kiro (IDE) on a Windows PC.
:::
Implementing an API using API Gateway, Lambda, and S3
The implementation was done using AmazonQDeveloper and Kiro with Vibe coding.
This time, I used Kiro to implement the following request: "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."
After that, I consulted with Kiro via chat to revise the S3 bucket name and Lambda function name as I went along.
Implementation Details
Folder Structure
The Lambda function program is created in the lambda folder, and the CDK IaC code is created in the lib folder.

lib Folder
The stack definition is created in the files in this folder.
Imports, Classes, and Constructs
The modules required for the stack are imported.
The class name and constructor are defined, and the following lines define each resource.

S3 Bucket Definition
The S3 bucket definition defines the following three items:
- bucketName: Fixed bucket name
- If no bucket name is specified, one will be automatically named in the format "myprojectstack-mybucket-".
- removalPolicy: DESTROY: Deletes the bucket when the stack is deleted.
- autoDeleteObjects: true: The bucket can be deleted even if it contains files.
Lambda Function Definition
A Lambda function definition defines the following five elements:
- functionName: Fixed function name
- If no function name is specified, a name will be automatically generated in the format "MyProjectStack-ListFilesFunction-".
- runtime: Uses Node.js 20.x
- handler: Executes the handler function in index.js
- code: Uses the code in the lambda/ directory
- environment: Passes the bucket name as an environment variable
This Lambda function retrieves a list of files in the S3 bucket, so grant read permission to the S3 bucket.
![]()
API Gateway Definition
This API Gateway is defined to call the Lambda function using the GET method.

Other Output Upon Deployment Completion
The bucket name and API URL are displayed upon deployment completion.

Preparing for Deployment
Build (Compiling TypeScript)
Since we're creating a CDK application using TypeScript, compile it using the following command:
npm run build
If an import error occurs, as shown in the image below, dependencies have not yet been resolved.

Execute the following command to resolve dependencies and then build again.
cd lambda
npm install
Verify that the build completed without errors.

Generate a CloudFormation Template (Recommended)
This step is not required as it is performed automatically during deployment. However, you can generate a CloudFormation template from the CDK code by running the following command.
npx cdk synth
By default, the results are displayed in JSON format on the console. However, if you want to save the results as a YAML file, for example, run the following command:
npx cdk synth --yaml > template.yaml
The output CloudFormation template is a 428-line file, as shown below.
The CDK code (TypeScript) in the lib folder was 58 lines long, so we were able to reduce a significant number of lines, demonstrating its readability.

npx cdk bootstrap
:::note warn
If an authentication error occurs
If the aws configure setting does not work, set the "AWS Access Key ID," "AWS Secret Access Key," and "AWS Session Token" in the file at the following path and run the Bootstrap command again.
C:\Users\[username]\.aws\credentials
If Bootstrap is successful, the CDK's initial S3 bucket and CloudFormation stack will be created.


Deploy execution
Run the following command:
npx cdk deploy
A confirmation of the deployment details will be displayed. If there are no problems, enter [y] to approve.

Check the deployment results in the AWS console
Once deployment is complete, you can view the stack in the deployed CloudFormation console screen.

I checked the CloudFormation template in Infrastructure Composer, and confirmed that the details defined in the CDK were reflected.

Deleting Deployment Content
To delete deployed content from your AWS environment, run cdk destroy.
npx cdk destroy
Enter [y] in response to the confirmation message to delete the content.
The deployed project will be deleted, but the S3 bucket and stack created by Bootstrap will not be deleted.

Reference
↓ Official AWS CDK documentation
https://docs.aws.amazon.com/ja_jp/cdk/v2/guide/home.html
↓ AWS CDK BlackBelt documentation (Japanese)




Top comments (0)