Infrastructure as Code: Terraform vs CloudFormation
As an infrastructure engineer, one of the most common questions I get is: "Should we use Terraform or CloudFormation?" The answer, as always, is "it depends." But let's break down the trade-offs so you can make an informed decision for your organization.
The Case for Terraform
Terraform, by HashiCorp, is the industry standard for multi-cloud infrastructure. Its biggest strength is its provider ecosystem. You can use Terraform to manage AWS, Azure, Google Cloud, Kubernetes, Datadog, and even your DNS provider—all in one language (HCL).
State Management in Terraform
Terraform maintains a "state file" that maps your code to real-world resources. This gives you a lot of power, but also responsibility. You need to manage this state file securely (usually in an S3 bucket with locking). If the state file gets corrupted or out of sync, you are in for a bad day.
The Case for CloudFormation
CloudFormation is AWS's native IaC tool. If you are 100% on AWS, it offers deep integration. The biggest advantage is that state is managed by AWS. You don't have to worry about a state file. It also supports "StackSets" for easy multi-account deployment.
Drift Detection
Both tools handle drift (when manual changes happen in the console) differently. Terraform detects drift every time you run a plan. CloudFormation has a drift detection feature, but you have to trigger it. In my experience, Terraform's feedback loop on drift is tighter and more visible during the development process.
The Syntax War: HCL vs YAML/JSON
Terraform uses HCL (HashiCorp Configuration Language). It is designed for infrastructure. It is readable, supports comments, and has good logic capabilities. CloudFormation uses YAML or JSON. YAML can get very verbose and hard to read for complex stacks. HCL is generally preferred by developers for its expressiveness.
Vendor Lock-in
This is the big one. CloudFormation locks you into AWS. If you ever want to move to another cloud or use a multi-cloud strategy, you have to rewrite everything. Terraform is cloud-agnostic in its workflow, even if the code itself (resource definitions) is specific to the provider. Learning Terraform gives you a skill that applies everywhere.