AWS CDK(Cloud Development Kit)는 TypeScript, Python, Java 등 프로그래밍 언어로 AWS 인프라를 정의하는 오픈소스 프레임워크다. 코드로 CloudFormation 템플릿을 생성한다.
| 항목 | CDK | CloudFormation | Terraform |
|---|
| 언어 | TS/Python/Java 등 | YAML/JSON | HCL |
| 추상화 수준 | 높음 (L1/L2/L3) | 낮음 | 중간 |
| 멀티 클라우드 | 불가 | 불가 | 가능 |
| 재사용성 | 클래스/라이브러리 | 중첩 스택 | 모듈 |
CDK Constructs 계층
- •L1 (Cfn): CloudFormation 리소스 직접 매핑
- •L2: 기본값과 편의 기능이 포함된 고수준 API
- •L3 (Patterns): 완전한 아키텍처 패턴
typescript
import * as cdk from 'aws-cdk-lib';
import * as ecs from 'aws-cdk-lib/aws-ecs';
import * as ecsPatterns from 'aws-cdk-lib/aws-ecs-patterns';
export class MyStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// VPC
const vpc = new ec2.Vpc(this, 'MyVpc', { maxAzs: 2 });
// ECS 클러스터
const cluster = new ecs.Cluster(this, 'MyCluster', { vpc });
// Fargate 서비스 (L3 패턴)
new ecsPatterns.ApplicationLoadBalancedFargateService(this, 'MyService', {
cluster,
cpu: 256,
memoryLimitMiB: 512,
desiredCount: 2,
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry('nginx:latest'),
containerPort: 80,
},
});
}
}
주요 CDK 명령어
bash
# 프로젝트 초기화
cdk init app --language typescript
# CloudFormation 템플릿 합성
cdk synth
# 차이점 확인
cdk diff
# 배포
cdk deploy
# 스택 삭제
cdk destroy
CDK Pipelines (CI/CD)
typescript
const pipeline = new pipelines.CodePipeline(this, 'Pipeline', {
synth: new pipelines.ShellStep('Synth', {
input: pipelines.CodePipelineSource.gitHub('my-org/my-repo', 'main'),
commands: ['npm ci', 'npm run build', 'npx cdk synth'],
}),
});
pipeline.addStage(new MyAppStage(this, 'Prod', {
env: { account: '123456789', region: 'ap-northeast-2' },
}));