In modern cloud computing, manually clicking buttons in the AWS Management Console to create servers and databases is considered an anti-pattern. Manual clicks cannot be version-controlled, peer-reviewed, or reproduced during a disaster recovery event.
To automate cloud infrastructure, DevOps engineers rely on Infrastructure as Code (IaC). The two most prominent tools in this domain are HashiCorp Terraform and Red Hat Ansible.
While beginners often view them as rivals, senior DevOps architects treat them as complementary partners. Let's explore how they differ and how to combine them on AWS.
Provisioning (Terraform) vs Configuration (Ansible)
- Terraform is an Infrastructure Provisioner: It creates and manages cloud resources from scratch. It builds your AWS Virtual Private Clouds (VPCs), subnets, Internet Gateways, IAM security roles, Amazon EKS clusters, and RDS PostgreSQL databases.
- Ansible is a Configuration Manager: Once those servers exist, Ansible configures them. It connects over SSH to install Docker, configure Nginx reverse proxies, update security patches, mount network storage, and deploy application binaries.
How Terraform Works: State Files & Declarative HCL
Terraform uses HashiCorp Configuration Language (HCL) to describe the desired end-state of your cloud:
- Declarative Syntax: You declare what you want (e.g., 'I want three EC2 instances in an Auto Scaling Group with an Application Load Balancer'), not the step-by-step instructions to create it.
- Terraform State File (
terraform.tfstate): Keeps track of the real-world IDs, IP addresses, and metadata of deployed resources. When you runterraform plan, Terraform compares your local HCL code against the remote cloud state and calculates exact additions, modifications, or destructions. - Provider Ecosystem: Thousands of providers allow Terraform to manage AWS, Google Cloud, Azure, Cloudflare DNS, and GitHub repositories in a unified syntax.
Industrial Training in Mohali
Cloud Computing & DevOpsMaster AWS, Kubernetes & DevOps CI/CD
Zero-downtime microservices deployment, infrastructure as code, Terraform, Docker, and Kubernetes clusters in Mohali & Chandigarh.
How Ansible Works: Agentless SSH & YAML Playbooks
- Agentless Architecture: Unlike older tools (Chef or Puppet) that required installing background daemons on target servers, Ansible connects over standard OpenSSH on Linux or WinRM on Windows.
- YAML Playbooks: Written in human-readable YAML. Tasks define sequential idempotent actions:
- name: Install and start Docker service
hosts: webservers
become: yes
tasks:
- name: Ensure docker is installed
apt:
name: docker.io
state: present
- name: Ensure docker service is running
systemd:
name: docker
state: started- Idempotency: If Docker is already installed and running, running the Ansible playbook again does nothing. It only executes changes when the actual state deviates from the desired state.
Declarative vs Procedural Paradigms Explained
- Declarative (Terraform): If you declare
count = 5EC2 instances and later change it tocount = 3, Terraform automatically calculates that 2 instances must be terminated. - Procedural (Scripts): If you write a bash script that says 'launch 2 servers', running it three times will create 6 servers unless you write complex defensive logic. Ansible sits gracefully in the middle: procedural task execution with idempotent modules.
The Perfect DevOps Duo: Provision with Terraform, Configure with Ansible
In production environments at Coder Roots:
- Step 1 (Terraform): Terraform provisions the AWS VPC, subnets, security groups, and an EC2 instance. Terraform outputs the public/private IP address of the newly provisioned server.
- Step 2 (Dynamic Inventory): Ansible queries AWS using the
amazon.aws.aws_ec2inventory plugin to automatically discover the newly created instances based on AWS resource tags (Environment=Production). - Step 3 (Ansible): Ansible connects to the instances via SSH, installs Docker, configures firewall rules (UFW), deploys application containers, and mounts encrypted storage volumes.
Frequently Asked Questions
Can Terraform do configuration management?
Terraform has remote-exec and user_data scripts, but they lack error handling, state tracking, and idempotent rollback capabilities for complex operating system management.
Can Ansible provision AWS resources?
Yes, Ansible has AWS modules (amazon.aws.ec2_instance). However, managing complex cloud dependency graphs, state tracking, and resource destruction is significantly clunkier in Ansible than in Terraform. Use Terraform for infrastructure and Ansible for OS configuration.