Two Tools, Two Philosophies
Infrastructure as Code (IaC) means managing your servers, networks, and cloud resources through code files instead of clicking through UIs. Terraform and Ansible are the two most popular tools — but they're not competitors. They complement each other.
Terraform: Provision Infrastructure
Terraform is a declarative tool: you describe the desired state of your infrastructure, and Terraform figures out how to get there. It excels at creating and managing cloud resources.
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t3.micro"
tags = {
Name = "web-server"
}
}
terraform apply creates the EC2 instance. Run it again with instance_type = "t3.small" and Terraform will resize it. Delete the block and Terraform will destroy the instance.
Best for: Creating VMs, VPCs, databases, load balancers, DNS records, S3 buckets — any cloud resource.
Ansible: Configure Servers
Ansible is a procedural tool that connects to servers via SSH and runs tasks in order. It's perfect for installing software, configuring services, and managing application deployments.
- name: Install Nginx
apt:
name: nginx
state: present
- name: Start Nginx
service:
name: nginx
state: started
enabled: yes
Best for: Installing packages, managing config files, deploying applications, running commands across many servers.
Use Them Together
The winning combination: Terraform to create infrastructure, Ansible to configure it.
- Terraform creates 3 EC2 instances and outputs their IPs
- Ansible reads those IPs and installs your app stack on each
This separation of concerns keeps each tool doing what it does best. Want your infrastructure managed as code? Let's get started.