Cloud Engineering
11 min readMarch 17, 2024

Infrastructure as Code Best Practices: Terraform vs. CloudFormation Deep Dive

Which IaC tool should you choose for your AWS infrastructure? We dive deep into the pros, cons, and best practices for both Terraform and CloudFormation.

AJ
Ajeet Yadav
Platform & Cloud Engineer
Infrastructure as Code Best Practices: Terraform vs. CloudFormation Deep Dive

Infrastructure as Code (IaC) is the foundation of modern cloud engineering. But the choice of tool often sparks heated debates. Should you go with the cloud-agnostic powerhouse Terraform, or the native AWS depth of CloudFormation?

The answer isn't "which is better," but "which is right for your team's workflow." Let’s dive deep into the technical nuances.

1. State Management: The Great Divide

Terraform: Managed State

Terraform keeps track of your infrastructure in a terraform.tfstate file. This is both its greatest strength and its primary management burden.

  • Strength: You can inspect exactly what Terraform thinks is reality.
  • Burden: You must manage state locking (e.g., using S3 and DynamoDB) to prevent concurrent runs from corrupting your infrastructure.

CloudFormation: Managed Reality

CloudFormation is "state-less" from the user's perspective. AWS manages the state internally.

  • Strength: No state files to lose or corrupt. Rollbacks are handled automatically by the AWS engine.
  • Constraint: It can sometimes be "stuck" in a DELETE_FAILED or UPDATE_ROLLBACK_FAILED state that requires manual intervention in the console.

2. Language: HCL vs. YAML/JSON

  • Terraform (HCL): HashiCorp Configuration Language is purpose-built for infrastructure. It is more concise than YAML and supports powerful logic like for_each, dynamic blocks, and modules.
  • CloudFormation (YAML/JSON): While YAML is readable, complex CloudFormation templates can become "Wall of YAML" nightmares. However, the introduction of AWS CDK allows you to write CloudFormation using real programming languages (TypeScript, Python, etc.).

3. The "Drift" Problem

Infrastructure drift occurs when someone makes a manual change in the AWS Console.

  • Terraform: Excellent at detecting drift. A simple terraform plan will show you exactly what changed outside of code.
  • CloudFormation: Has built-in Drift Detection, but it is a manual trigger and doesn't always cover every resource property perfectly.

Best Practices for Both

Regardless of the tool you choose, follow these three golden rules:

A. Modularize Everything

Don't write 2,000-line files. Break your infrastructure into reusable modules.

  • Terraform: Use modules/ directories.
  • CloudFormation: Use Nested Stacks or CloudFormation StackSets.

B. CI/CD Integration

Never run IaC from a developer's laptop. Use a pipeline to ensure every change is planned, reviewed, and then applied.

Rendering diagram…

C. Version Control Your Provider/Engine

  • In Terraform, lock your provider versions (aws ~> 5.0).
  • In CloudFormation, be wary of using latest versions of macros if you have high stability requirements.

Comparison Summary

FeatureTerraformCloudFormation
Cloud TargetMulti-CloudAWS Only
State ManagementUser-managed (External)AWS-managed (Internal)
LanguageHCLYAML/JSON/CDK
Community ContentMassive (Terraform Registry)Good (AWS Samples)
Resource SupportFast (often same-day)Official (Native)

The Verdict

  • Choose Terraform if: You use multiple clouds, want a cleaner language (HCL), or need the most advanced orchestration features.
  • Choose CloudFormation if: You are 100% committed to AWS, prefer "zero-maintenance" state, or want the deepest integration with AWS-native security and support.

Evaluating your IaC strategy? Coding Protocols can help you refactor, migrate, or secure your Infrastructure as Code.

Related Topics

Terraform
CloudFormation
IaC
AWS
Best Practices
DevOps
HashiCorp

Read Next