CDK
AWS Cloud Development Kit
An IaC framework that defines AWS resources in programming languages. Supports TypeScript, Python, and others.
AWS Cloud Development Kit
An IaC framework that defines AWS resources in programming languages. Supports TypeScript, Python, and others.
AWS CDK (Cloud Development Kit) is an open-source IaC (Infrastructure as Code) framework that lets you define AWS infrastructure in familiar programming languages such as TypeScript, Python, Java, C#, and Go. Since its general availability in 2019, it has grown rapidly as an alternative to CloudFormation.
The core of CDK is an abstraction layer called 'Construct'. Constructs are divided into L1 (1:1 mapping to CloudFormation resources), L2 (high-level abstractions with best practices applied), and L3 (patterns combining multiple resources). For example, a single L2 ApplicationLoadBalancedFargateService can create an ALB, ECS Fargate, Target Group, and Security Group all at once.
CDK code is converted into a CloudFormation template with the `cdk synth` command and deployed to AWS with `cdk deploy`. Therefore, you can use all CloudFormation features (Drift Detection, Rollback, Change Set, etc.) as they are.
In practice, the advantages of CDK are IDE autocompletion, type checking, and the ability to use loops and conditionals. In many cases, 300 lines of CloudFormation YAML shrink to 50 lines of TypeScript. You can also reuse community patterns from Construct Hub.
// CDK TypeScript - ECS Fargate 서비스 배포
import * as cdk from 'aws-cdk-lib';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as ecs from 'aws-cdk-lib/aws-ecs';
import * as ecsPatterns from 'aws-cdk-lib/aws-ecs-patterns';
export class MyServiceStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// VPC 생성 (NAT Gateway 포함)
const vpc = new ec2.Vpc(this, 'MyVpc', {
maxAzs: 2,
natGateways: 1,
});
// ECS 클러스터 생성
const cluster = new ecs.Cluster(this, 'MyCluster', {
vpc,
containerInsights: true,
});
// ALB + Fargate 서비스 (L3 Construct)
const service = new ecsPatterns.ApplicationLoadBalancedFargateService(
this, 'MyService', {
cluster,
cpu: 256,
memoryLimitMiB: 512,
desiredCount: 2,
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry('nginx:latest'),
containerPort: 80,
},
publicLoadBalancer: true,
}
);
// Auto Scaling 설정
const scaling = service.service.autoScaleTaskCount({
minCapacity: 2,
maxCapacity: 10,
});
scaling.scaleOnCpuUtilization('CpuScaling', {
targetUtilizationPercent: 70,
});
// ALB DNS 출력
new cdk.CfnOutput(this, 'LoadBalancerDNS', {
value: service.loadBalancer.loadBalancerDnsName,
});
}
}
"Terraform is good too, but our team is familiar with TypeScript and uses only AWS, so I think CDK is a better fit. IDE autocompletion speeds up development, and L2/L3 Constructs reduce boilerplate. Since it ultimately converts to CloudFormation, its stability is also proven."
"I managed microservice infrastructure with CDK. I separated Stacks by service and referenced shared resources such as the VPC and RDS through Cross-Stack References. I reviewed changes with cdk diff and deployed Lambda quickly with cdk deploy --hotswap. I also wrote infrastructure unit tests with Jest."
"You defined this directly with an L1 Construct, but if you use an L2 Construct, the Security Group and IAM Role are automatically created with least privilege. If you switch to ApplicationLoadBalancedFargateService, the code is cut in half."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
Before deployment, CDK requires you to create an S3 bucket and IAM Role with the `cdk bootstrap` command. It must be run once per region, and if you skip it, the error "This stack uses assets, so the toolkit stack must be deployed" occurs.
CDK generates the CloudFormation Logical ID based on the Construct ID. If you change the ID, the existing resource is deleted and a new one is created. Be especially careful with resources that hold data, such as production DBs or S3.
Always check changes with `cdk diff` before deploying. Set RemovalPolicy.RETAIN on stateful resources (RDS, DynamoDB, etc.), and for production, use the `--require-approval broadening` option so that approval is required when IAM changes.