Developing service with AWS CDK
Tags: aws, cdk, iac
Published: 2022-10-28 02:31:17

# 1. What is AWS CDK(Cloud Development Kit)?
AWS CDK is an open-sourced software development framework which possible to design cloud infra with codes.  AWS opened to the public in July 2019.  The [official website](https://aws.amazon.com/cdk/) defines it as "lets you define your cloud infrastructure as code in one of its supported programming languages".  Unlike the previous IaC, the CDK provides a syntax for configuring cloud resources with default values verified using existing familiar programming languages.  This allows non-professionals to start developing cloud applications by configuring service infra quickly, easily, and in a secure and reusable way.

### Which IaC would I use while I develop with AWS?
In my case, Infrastructure management in service development using the public cloud seems to have gone through the following process.

> ### a. Deploy/Manage service infra using the UI console
>> If you use the AWS Console to configure the infrastructure for service development or hands-on, and o design and deploy the entire service architecture with a mouse click, you would have the question 'Is it really possible to configure a complex system through this process?'.  In addition, it becomes inconvenient when you have to build the same infrastructure elements over and over again or rebuild the entire infrastructure across multiple clusters.

> ### b. Use the CLI command
>> Feeling limited by the click-and-click approach, people usually start deploying services and configuring cloud infrastructure using the AWS CLI as the script of the command line interface.  However, it is hard to cope with retrying in the error situation or coping with the processing the race condition in the multi-task.

> ### c. Interest into the IaC (Infra as Code)
>> You can also learn AWS CloudFormation script, tools that allow you to manage service infrastructure.  Even you can choose Hashicorp's Terraform to describe and manage the infrastructure components in a corresponding format of Terraform script.  However, I was doubtful when I look at those kinds of scripts as IaC and watched the process of the work with it from the side.  From a programmer's view, the previous IaC script with the format of JSON or Yaml seems like too much repeating of the same texts.  Therefore, it was clear that even a small increase in the size of the system would result in the number and files becoming too large to manage.

> ### d. CDK, the truly programmable IaC
>> I found out about the AWS CDK when I felt that scripting IaC is not my way.  CDK drew interestd me who was developing software for a long time as it is possible to write programming language and design it with software development skills that were getting improved over decades.  Therefore, I thought that it would possible to be more efficient in designing, composing, and managing service infrastructure with CDK.  Furthermore, this idea has become even more solid now that I have completed the API service development project using CDK.  
 The comparison with Terraform, the representative of the existing IaC, is attached in the table below.

<br>
#### Terraform VS AWS CDK
- | __Terraform__ | __CDK__
--- | --- | ---
__Programming feature__ | Implement with Yaml or HCL, Need to learn a new language different from the existing ones.  Auto-completion by installing an assist tools(Not perfect).  Difficult to implement safely due to compile errors, ... etc | 5 existing programming languages are possible to use.  Various extensions are possible using OOP.  flexible structure and reusability can be improved by using patterns.  Non-limited expandability by the author's programming ability.  Automation through existing IDE(VSCode).  Safe implementation through compile errors 
__Workload for composing infra__ | Many references to configuring infrastructure as an approach from IaaS | Combining the latest technologies in the container/serverless architecture rather than IaaS.  Fully support IaaS too.
__Support for Public Cloud__ | Support various public cloud | Specialized in AWS.  High grouth potential because ecosystems such as CDK for Terraform and CDK8S ar being created.
__License, Maturity and Stability__ | Requires an enterprise contract for more than basic feature.  Slightly lowered the stability in deployment as directly with SDK | Free for CloudFormation, ParameterStore, Matured the stability in deployment using CloudFormation as the backend.
<br>

### Who's needed for?
- Rather than using the AWS Console, you want more flexible, convenient, and robust infrastructure management.
- I want to configure/manage resources using IaC, but I can't afford to manage monotonous and huge yaml files.
- I want to actively utilize my programming-related development skills for IaC writing.
- I want the optimal IaC to configure the service with a serverless architecture in AWS.
Developers with the above concerns can consider CDK as a tool for designing and managing service infrastructure.
<br>

### How is it operate?
Written in a software development language, cloud infrastructure can be modeled as reusable components. Currently, 5 development languages (Typescript, Javascript, Python, Java, C#) are supported.  If you look at the operation process of CDK, the written CDK application is executed with CDK CLI, synthesized into CloudFormation Template, and distributed through AWS CloudFormation.  It might help to understand if you think of it as running through.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93xinpbohft90zacpzyy.png)
[Deploying infra by writing code]
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wyicr6cwv3zokgcq3ml5.png)
<br>

# 2. How to start?
Since the official Amazon documentation is well done, you can easily access it by referring to the documentation.  You can refer to the Developer Guide and refer to the API Reference for detailed specifications of APIs during actual development.  In the case of CDK API, since AWS services are well abstracted, I think that it is a good approach to refer to the CDK API Reference as a way to learn AWS services.  In addition, a hands-on lab for first-time users is provided through the CDK Workshop page.
- Developer Guide (https://docs.aws.amazon.com/cdk/latest/guide/home.html)
- API Reference (https://docs.aws.amazon.com/cdk/api/latest/docs/aws-construct-library.html)
- CDK Workshop (https://cdkworkshop.com/)

<br>

## Development using AWS CDK
cdk is implemented using node. Therefore, no matter what programming language is used for development, node must be installed by default. Install aws-cdk using npm in the environment where node is installed. After initializing the project through 'cdk init', install the necessary node libraries through 'npm install'.  Preparations for cdk development are complete, and now, cdk development through coding and development of the stack are checked for errors in the code through 'cdk list'.  You just have to proceed. During development and debugging, 'list', 'diff', and 'synth' are used, and 'cdk deploy' is repeatedly used when deploying the confirmed code.  If the service is finally terminated or the infrastructure needs to be deleted, you can cleanly delete the installed applications and infrastructure with 'cdk destroy'.

### Install CDK CLI

```
npm install -g aws-cdk
cdk version
mkdir hello-cdk
cd hello-cdk
cdk init --language [typescript/javascript/python/java/csharp]
npm install
```
### Commands of CDK
```
cdk bootstrap		# Deploy the stack for the CDK Toolkit in your AWS environment
cdk init		# Initializes a new default application in the language of the user's choice
cdk diff		# Identify differences between local AWS CDK code and applications running on AWS 
cdk synth		# Compiling AWS CDK Applications to AWS CloudFormation Templates
cdk deploy		# Deploy the AWS CDK application to a set up AWS account 
cdk destory		# Delete the deployed CDK application 
```
<br>
In this article, I will leave it as an introduction and plan a detailed article for developing with CDK next time for more detailed development methods.
For those of you who want to test a little more, please use the reference link above and try the hands-on lab on the CDK Workshop page, it seems to be helpful for understanding.
<br>

# 3. Feature in development with CDK
### Advantages of AWS development using CDK
CDK is developed using Constructs that abstract AWS resources into high-level components. You can use the basic settings used in the best practices of AWS services just by referring to these Constructs, and further understand each service in depth just by understanding the API documents.
<br>
[AWS CDK Construct]
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7911pt9sveciuplehbl7.png)
<br>

On the AWS CDK official page, the advantages of developing using cdk are divided into 4 categories as follows.
> - Easier cloud onboarding
>> Even if you're new to using AWS, you can speed up your onboarding to the Cloud. cdk's API abstracts AWS resources into high-level components and is initialized with optimal default settings, so you can configure an appropriate system without an expert.

> - Faster development process
>> Since the infrastructure is defined using the characteristics of the programming language, efficient and fast development is possible depending on how the logic such as OOP, loop, and conditional statements are configured.

> - Customizable and shareable
>> It is possible to design with reusable components suitable for each requirement, and it is possible to quickly expand security, regulations, and compliance with requirements through easy sharing of libraryd components.

> - No context switching
>> Since code development and distribution are possible in the IDE in the development environment, developers can develop applications and manage infrastructure without changing or setting up a separate development environment.

### References in the solutions constructs
There are structures that are patterned by arranging components that can be used repeatedly, and I have made them into solutions and made them into CDK Constructs. You can find it on the [AWS Solutions Constructs](https://docs.aws.amazon.com/solutions/latest/constructs/welcome.html) page, take it, use it, or refer to it and reuse it in the system configuration you develop to more quickly construct your desired service design.

### Ecosystem Expansion
CDK is a great choice for development on AWS and has great advantages, especially when designing serverless architectures. However, you may think that it is not possible to use other public clouds or to directly build an on-premise or IaaS-focused Kubernetes cluster. In order to solve these points, there are projects that can link the advantages of CDK to other platforms and make Kubernetes design with cdk, together with other platforms or CNCF.

1. [Terraform-cdk](https://github.com/hashicorp/terraform-cdk)
> It was started supporting the CDK as a tool for defining and provisioning infrastructure using Terraform.
>[Terraform Providers]
> ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ufzf8866af5eb9k7w417.png)
> (https://www.hashicorp.com/blog/cdk-for-terraform-enabling-python-and-typescript-support/)
> (https://www.hashicorp.com/blog/announcing-cdk-for-terraform-0-1)

2. [CDK8s](https://cdk8s.io/)
> You can use CDK for Kubernetes (CDK8s) to define and manage Kubernetes applications. CDK8s is currently registered as [CNCF's Sandbox Project](https://www.cncf.io/sandbox-projects/).
> [Workflow of CDK8s]
> ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4jzk5sm3h1nta22s9jqh.png)
> (https://aws.amazon.com/ko/blogs/korea/using-cdk8s-for-kubernetes-applications/)
> (https://aws.amazon.com/blogs/containers/introducing-cdk-for-kubernetes/)
<br>

# Ending
In this article, I introduced the CDK and briefly explained the development flow of how development proceeds. In the next article, I'm planning an article that explains in more detail how to proceed with CDK development.  My team is developing a system that provides APIs related to Vision AI using AWS CDK.  Next, I will be able to share related tips and development tips on the blog while development is in progress.  
 
Thank you.