DEV Community

Nao San for AWS Community Builders

Posted on

[AWS] DevTools Evangelism CDK Edition

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.
image.png

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:
image.png

Initializing AWS CDK

Creating a Project

Create a folder for your project and navigate to that folder hierarchy.

mkdir my-project
cd my-project
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

image.png

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.
image.png

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.
image.png

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.

image.png

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

image.png

This Lambda function retrieves a list of files in the S3 bucket, so grant read permission to the S3 bucket.
image.png

API Gateway Definition

This API Gateway is defined to call the Lambda function using the GET method.
image.png

Other Output Upon Deployment Completion

The bucket name and API URL are displayed upon deployment completion.
image.png

Preparing for Deployment

Build (Compiling TypeScript)

Since we're creating a CDK application using TypeScript, compile it using the following command:

npm run build
Enter fullscreen mode Exit fullscreen mode

If an import error occurs, as shown in the image below, dependencies have not yet been resolved.
image.png
Execute the following command to resolve dependencies and then build again.

cd lambda
npm install
Enter fullscreen mode Exit fullscreen mode

Verify that the build completed without errors.
image.png

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/192949/4025df0b-bd18-456e-b53a-
Check the "AWS Access Key ID" and "AWS Secret Access Key" in the Access Portal screen, and set them as the required variables after executing the following command.

aws configure
Enter fullscreen mode Exit fullscreen mode

Bootstrap (First Time Only)

npx cdk bootstrap
Enter fullscreen mode Exit fullscreen mode

:::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
Enter fullscreen mode Exit fullscreen mode

:::
image.png

If Bootstrap is successful, the CDK's initial S3 bucket and CloudFormation stack will be created.
image.png
image.png

Deploy execution

Run the following command:

npx cdk deploy
Enter fullscreen mode Exit fullscreen mode

A confirmation of the deployment details will be displayed. If there are no problems, enter [y] to approve.
image.png

Check the deployment results in the AWS console

Once deployment is complete, you can view the stack in the deployed CloudFormation console screen.
image.png
I checked the CloudFormation template in Infrastructure Composer, and confirmed that the details defined in the CDK were reflected.
image.png

Deleting Deployment Content

To delete deployed content from your AWS environment, run cdk destroy.

npx cdk destroy
Enter fullscreen mode Exit fullscreen mode

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.
image.png

Reference

↓ Official AWS CDK documentation

https://docs.aws.amazon.com/ja_jp/cdk/v2/guide/home.html

↓ AWS CDK BlackBelt documentation (Japanese)

https://pages.awscloud.com/rs/112-TZM-766/images/AWS-Black-Belt_2023_AWS-CDK-Basic-1-Overview_0731_v1.pdf

Top comments (0)