Infrastructure as Code: Terraform vs. CloudFormation

devops terraform aws infrastructure

Infrastructure as Code (IaC) is no longer optional. Manually clicking through consoles doesn’t scale. The question is: which tool?

For AWS-centric teams, the choice often comes down to Terraform vs. CloudFormation. Let’s compare.

CloudFormation: AWS Native

CloudFormation is AWS’s built-in IaC service.

Example Template

# cloudformation.yml
AWSTemplateFormatVersion: '2010-09-09'
Description: Simple EC2 instance

Resources:
  MyEC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: t3.micro
      ImageId: ami-0123456789abcdef0
      SecurityGroups:
        - !Ref InstanceSecurityGroup
  
  InstanceSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Allow SSH
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 22
          ToPort: 22
          CidrIp: 0.0.0.0/0

Deploy

aws cloudformation create-stack \
  --stack-name my-stack \
  --template-body file://cloudformation.yml

Strengths

Weaknesses

Terraform: Provider Agnostic

Terraform is HashiCorp’s multi-cloud IaC tool.

Example Configuration

# main.tf
provider "aws" {
  region = "us-west-2"
}

resource "aws_instance" "main" {
  ami           = "ami-0123456789abcdef0"
  instance_type = "t3.micro"
  
  vpc_security_group_ids = [aws_security_group.main.id]
}

resource "aws_security_group" "main" {
  name = "allow_ssh"
  
  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

Deploy

terraform init
terraform plan
terraform apply

Strengths

Weaknesses

Head-to-Head Comparison

FactorCloudFormationTerraform
Multi-cloud❌ AWS only✅ 1000+ providers
Learning curveLowerHigher
SyntaxYAML/JSONHCL
State managementAWS handlesYou manage
Drift detection✅ Built-inPartial
Speed of updatesSlowerFaster
Loops/conditionalsLimitedPowerful
CostFreeFree (OSS) / Paid (Cloud)

When to Choose CloudFormation

When to Choose Terraform

Can You Use Both?

Yes! Some teams use:

But beware of managing the same resources with both—that leads to drift.

State Management Deep Dive

Terraform’s state is critical. Store it safely:

# backend.tf
terraform {
  backend "s3" {
    bucket         = "my-terraform-state"
    key            = "prod/terraform.tfstate"
    region         = "us-west-2"
    encrypt        = true
    dynamodb_table = "terraform-locks"
  }
}

CloudFormation doesn’t have this problem—AWS manages state internally.

Modern Alternatives

Worth considering:

My Recommendation

For most teams: Start with Terraform. The multi-cloud flexibility and HCL syntax are worth the state management overhead.

For pure AWS shops: CloudFormation (or CDK) is perfectly valid. One less tool to manage.

Either way: Pick one and stick with it. Consistency matters more than the specific choice.

Final Thoughts

The IaC landscape is mature. Both tools are production-ready and well-supported. Your choice depends on your cloud strategy and team preferences.

The important thing is using IaC at all. Manual infrastructure is technical debt waiting to happen.


Infrastructure should be code. Period.

All posts