# CloudFormation and AWS CDK

> Use AWS-native IaC: CloudFormation templates and stacks, change sets and drift detection, and the CDK to write infrastructure in TypeScript or Python. Free...

Canonical: https://www.internhack.in/roadmaps/tracks/cloud-engineer/cloudformation-cdk
Last updated: 2026-11-28

Use AWS-native IaC: CloudFormation templates and stacks, change sets and drift detection, and the CDK to write infrastructure in TypeScript or Python.

Intermediate · about 12 hours · difficulty 3/5

Learn first: [Infrastructure as Code Concepts](https://www.internhack.in/roadmaps/tracks/cloud-engineer/iac-concepts)

**AWS CloudFormation** is AWS's built-in IaC service, and the **AWS CDK** lets you define the same infrastructure in TypeScript, Python, Java or C# and synthesizes CloudFormation for you. Choose this path if your team is AWS-only or already uses CDK. Azure's equivalent is Bicep; Google Cloud uses Infrastructure Manager (Terraform-based).

### CloudFormation essentials

- **Templates** (YAML or JSON) with Parameters, Resources, Outputs, Mappings and Conditions.
- A **stack** is a deployed template. State is managed by AWS, so there is no state file to protect.
- **Change sets** preview changes; **rollback** happens automatically if a deployment fails.
- **Drift detection** finds manual changes.
- **StackSets** deploy the same stack across many accounts and regions.

### AWS CDK

- **Constructs** come in levels: L1 (raw CloudFormation), L2 (sensible defaults, such as a VPC in a few lines), L3 patterns (an ALB-fronted Fargate service in one construct).
- Commands: cdk init, cdk synth, cdk diff, cdk deploy, cdk destroy.
- You get loops, types, unit tests and IDE autocompletion, and grant methods (bucket.grantRead(fn)) write least-privilege IAM for you.
- **cdk bootstrap** prepares each account and region once.

### Also know

**AWS SAM** is a CloudFormation extension focused on serverless apps, with local testing of Lambda functions.

### Pitfalls

- Stacks stuck in UPDATE_ROLLBACK_FAILED after manual changes.
- Deleting a stack that owns a production database without a DeletionPolicy of Retain or Snapshot.
- Overusing L1 constructs and losing CDK's safer defaults.

You are ready to move on when you can deploy a VPC and Fargate service with CDK, read the synthesized template, review cdk diff, and destroy it cleanly.

## Key concepts

- **Template anatomy**
- **Stacks and change sets**
- **Rollback and drift detection**
- **CDK constructs L1, L2, L3**
- **cdk synth, diff, deploy**
- **DeletionPolicy**
- **StackSets**
- **AWS SAM**

## Other options for this step

- [Terraform on AWS](https://www.internhack.in/roadmaps/tracks/cloud-engineer/terraform): Manage AWS with Terraform: providers, resources, variables, remote state in S3 with locking, modules and safe plan/apply workflows.

## Mini project

Write a CDK app in TypeScript or Python that creates a two-AZ VPC without NAT gateways, an S3 bucket with Block Public Access and a Lambda with read access granted through bucket.grantRead. Run cdk diff, deploy, test, then cdk destroy. Everything is free-tier sized; confirm the stack and bucket are gone afterwards.

## Check yourself

What is the difference between a CDK L1 and L2 construct, and why would you usually prefer L2?

## Free resources

- [AWS CloudFormation documentation](https://docs.aws.amazon.com/cloudformation/) (AWS)
- [AWS CDK documentation](https://docs.aws.amazon.com/cdk/) (AWS)
- [AWS SAM documentation](https://docs.aws.amazon.com/serverless-application-model/) (AWS)

Previous: [Terraform on AWS](https://www.internhack.in/roadmaps/tracks/cloud-engineer/terraform) · Next: [CI/CD Concepts and Pipeline Design](https://www.internhack.in/roadmaps/tracks/cloud-engineer/ci-cd-concepts)

## More in Infrastructure as Code

- [Infrastructure as Code Concepts](https://www.internhack.in/roadmaps/tracks/cloud-engineer/iac-concepts)
- [Terraform on AWS](https://www.internhack.in/roadmaps/tracks/cloud-engineer/terraform)
- [CloudFormation and AWS CDK](https://www.internhack.in/roadmaps/tracks/cloud-engineer/cloudformation-cdk)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Cloud Engineer (AWS) Roadmap](https://www.internhack.in/roadmaps/tracks/cloud-engineer)
