AWS Builder Center

Terraform vs Ansible: Understanding Their Roles in DevOps

Terraform and Ansible are two powerful DevOps automation tools, but they solve different problems. Learn how Terraform provisions infrastructure and how Ansible configures and manages servers, with practical examples of using both together in AWS environments.

Devops Enginner

Terraform vs Ansible: Understanding Their Roles in DevOps

In modern DevOps environments, automation is essential for building infrastructure quickly, configuring servers consistently, and deploying applications reliably.
Two popular tools used for DevOps automation are Terraform and Ansible. Although both tools automate infrastructure-related tasks, they have different purposes and approaches.
A simple way to remember the difference is:
Terraform builds the infrastructure. Ansible configures and manages it.
In this article, we will understand Terraform and Ansible, compare their roles, and see how they can be used together in a real-world DevOps workflow.

What is Terraform?

Terraform is an Infrastructure as Code (IaC) tool developed by HashiCorp.
It allows you to define infrastructure using configuration files instead of creating resources manually through a cloud provider's console.
Terraform can be used to create and manage resources such as:
  • AWS EC2 instances
  • VPCs
  • Subnets
  • Security Groups
  • Load Balancers
  • RDS databases
  • S3 buckets
  • IAM resources
  • Kubernetes resources
Terraform uses configuration files written in HashiCorp Configuration Language (HCL).
For example, an EC2 instance can be defined using Terraform:
1
2
3
4
5
6
7
8
resource "aws_instance" "web" {
ami = "ami-xxxxxxxx"
instance_type = "t2.micro"

tags = {
Name = "WebServer"
}
}
Instead of manually creating the EC2 instance, Terraform can create it from this configuration.

How Terraform Works

Terraform follows a declarative approach.
You describe what the desired infrastructure should look like, and Terraform determines the actions required to reach that state.
A typical Terraform workflow is:
1
2
3
4
5
6
7
8
9
10
11
12
13
Terraform Configuration
|
v
terraform init
|
v
terraform plan
|
v
terraform apply
|
v
Cloud Infrastructure

1. terraform init

Initializes the Terraform working directory and downloads the required providers.
1
terraform init

2. terraform plan

Shows what Terraform plans to create, modify, or destroy.
1
terraform plan

3. terraform apply

Applies the planned changes.
1
terraform apply

4. terraform destroy

Removes infrastructure managed by Terraform.
1
terraform destroy

What is Ansible?

Ansible is an automation and configuration management tool.
It is commonly used to:
  • Configure servers
  • Install software
  • Manage users
  • Modify configuration files
  • Deploy applications
  • Restart services
  • Perform system administration tasks
  • Automate repetitive operational tasks
Ansible uses YAML files called Playbooks.
For example, we can use Ansible to install Nginx:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
---
- name: Install Nginx
hosts: webservers
become: yes

tasks:
- name: Install Nginx
apt:
name: nginx
state: present

- name: Start Nginx
service:
name: nginx
state: started
enabled: yes
Instead of manually connecting to every server and installing Nginx, Ansible can perform the task automatically.

How Ansible Works

Ansible generally follows an agentless architecture.
The administrator or automation server communicates with target machines, commonly using SSH for Linux systems.
A simplified workflow looks like this:
1
2
3
4
5
6
7
8
9
Ansible Control Node
|
| SSH
|
+------> Server 1
|
+------> Server 2
|
+------> Server 3
The main components include:

Control Node

The machine from which Ansible commands and playbooks are executed.

Managed Nodes

The servers that Ansible configures and manages.

Inventory

The inventory contains the hosts that Ansible should manage.
Example:
1
2
3
4
5
6
[webservers]
web1 ansible_host=10.0.1.10
web2 ansible_host=10.0.1.11

[dbservers]
db1 ansible_host=10.0.2.10

Playbook

A YAML file containing automation instructions.

Terraform vs Ansible

Although Terraform and Ansible are both automation tools, their primary responsibilities are different.
FeatureTerraformAnsible
Main purposeInfrastructure provisioningConfiguration management
ApproachDeclarativeDeclarative
LanguageHCLYAML
State managementYesNo Terraform-style state
Agent requiredNoNo
Common communicationCloud APIsSSH / WinRM / APIs
Creates infrastructureYesPossible, but not its primary role
Configures serversLimitedYes
Application deploymentLimitedYes
Infrastructure lifecycleStrongNot its primary purpose
Cloud provisioningExcellentPossible
Server configurationNot its main strengthExcellent
The important point is that Terraform and Ansible are complementary tools.

Terraform's Main Responsibility

Terraform is best suited for creating infrastructure.
For example, suppose we need an AWS environment containing:
1
2
3
4
5
6
7
8
9
VPC
|
+-- Public Subnet
| |
| +-- EC2
|
+-- Private Subnet
|
+-- Database
Terraform can create:
  • VPC
  • Internet Gateway
  • Subnets
  • Route Tables
  • Security Groups
  • EC2 instances
  • Load Balancers
  • RDS databases
This makes Terraform useful during the infrastructure provisioning stage.

Ansible's Main Responsibility

Once the servers are created, we may need to configure them.
For example:
1
2
3
4
5
6
7
8
9
EC2 Instance
|
+-- Install Java
+-- Install Docker
+-- Install Nginx
+-- Create Users
+-- Configure Firewall
+-- Deploy Application
+-- Start Services
This is where Ansible becomes useful.
Ansible can connect to the server and perform these tasks automatically.

Using Terraform and Ansible Together

Terraform and Ansible can be combined in a DevOps pipeline.
For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Developer
|
v
GitHub
|
v
Jenkins
|
+----------------+
| |
v v
Terraform Ansible
| |
v v
AWS Infrastructure Server Configuration
| |
+-------+--------+
|
v
Application
The workflow can be:

Step 1: Terraform creates infrastructure

Terraform provisions:
  • VPC
  • Subnets
  • Security Groups
  • EC2 instances
  • Load Balancer

Step 2: Ansible configures the servers

Ansible can then:
  • Install required packages
  • Configure the operating system
  • Install Docker
  • Configure Nginx
  • Create application users
  • Deploy application files

Step 3: Application deployment

The application can then be deployed using Docker, Kubernetes, Jenkins, or another deployment mechanism.

Example Real-World Scenario

Imagine a company wants to deploy an application on AWS.
The infrastructure requirement is:
1
2
3
4
5
6
7
8
9
10
11
AWS
|
VPC
|
+----------+----------+
| |
Public Subnet Private Subnet
| |
Load Balancer DB
|
EC2 Servers

Terraform

Terraform creates the AWS infrastructure.
1
2
3
4
5
6
VPC
Subnets
Security Groups
EC2
Load Balancer
Database

Ansible

Ansible configures the EC2 instances.
1
2
3
4
5
6
Install Docker
Install Nginx
Configure Application
Create Users
Copy Configuration
Start Services
This separation makes the automation easier to maintain.

Why Not Use Only Terraform?

Terraform can perform some configuration-related tasks, but it is not primarily designed to be a server configuration management tool.
For example, Terraform can create an EC2 instance, but installing and maintaining dozens of packages across hundreds of servers is better handled by a configuration management tool such as Ansible.
Terraform should generally focus on the infrastructure lifecycle, while Ansible focuses on configuration and operational tasks.

Why Not Use Only Ansible?

Ansible can provision cloud resources using cloud modules, but Terraform is often a better fit for managing complex infrastructure and its lifecycle.
Terraform provides:
  • Infrastructure state
  • Dependency management
  • Resource planning
  • Infrastructure lifecycle management
  • Strong cloud-provider integration
For large infrastructure environments, Terraform makes it easier to understand and manage the desired infrastructure state.

Terraform State

One important Terraform concept is the state file.
Terraform uses state to keep track of infrastructure resources that it manages.
For example:
1
2
3
4
5
6
7
Terraform Configuration
|
v
Terraform State
|
v
AWS Resources
The state allows Terraform to understand what resources already exist and what changes are required.
In production environments, Terraform state is commonly stored remotely, such as in an Amazon S3 bucket, often with appropriate locking/concurrency controls depending on the Terraform backend setup.

Ansible Idempotency

An important Ansible concept is idempotency.
Idempotency means that running the same automation multiple times should produce the same desired result rather than repeatedly making unnecessary changes.
For example:
1
2
3
4
- name: Install nginx
apt:
name: nginx
state: present
If Nginx is already installed, Ansible does not need to reinstall it.
This makes automation safer and more predictable.

Terraform vs Ansible: Simple Example

Consider building a restaurant.
Terraform is like the construction team.
It creates:
  • Building
  • Rooms
  • Electricity
  • Water connections
  • Infrastructure
Ansible is like the operations team.
It:
  • Arranges the rooms
  • Installs equipment
  • Configures systems
  • Maintains the environment
So:
1
2
3
Terraform → Build the infrastructure

Ansible → Configure and manage the infrastructure

Terraform + Ansible + Jenkins

These tools can also be integrated into a CI/CD pipeline.
A Jenkins pipeline could look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Developer Push
|
v
GitHub
|
v
Jenkins
|
v
Terraform Plan
|
v
Terraform Apply
|
v
Ansible Playbook
|
v
Application Deployment
|
v
Production
This provides an automated infrastructure and configuration workflow.

Best Practices

When using Terraform and Ansible in production, consider the following practices:

Terraform

  • Use reusable modules.
  • Store state securely.
  • Use remote state for team environments.
  • Review terraform plan before applying changes.
  • Avoid hardcoding credentials.
  • Use variables and outputs.
  • Use separate environments such as development, staging, and production.

Ansible

  • Use roles for reusable configurations.
  • Store playbooks in Git.
  • Use Ansible Vault for sensitive data.
  • Maintain a clean inventory.
  • Follow idempotent practices.
  • Avoid hardcoding passwords and secrets.
  • Test changes before applying them to production.

When Should You Use Terraform?

Use Terraform when you need to:
  • Create cloud infrastructure
  • Manage AWS resources
  • Build repeatable environments
  • Implement Infrastructure as Code
  • Manage infrastructure lifecycle
  • Create development, staging, and production environments

When Should You Use Ansible?

Use Ansible when you need to:
  • Configure servers
  • Install software
  • Manage operating system settings
  • Deploy applications
  • Automate administrative tasks
  • Maintain consistent server configurations

Final Comparison

The easiest way to remember the difference is:
1
2
3
4
5
6
7
8
9
10
11
Terraform
↓
Provision Infrastructure
↓
AWS / Cloud Resources

Ansible
↓
Configure Infrastructure
↓
Servers / Applications
Terraform and Ansible should not always be viewed as alternatives.
They can work together to create a complete automation workflow:
1
2
3
4
5
6
7
8
9
10
11
12
13
Terraform
|
| Creates
v
Infrastructure
|
| Configured by
v
Ansible
|
| Deploys/Configures
v
Application
In a modern DevOps environment, using the right tool for the right job helps teams build infrastructure faster, maintain consistency, and reduce manual work.

Conclusion

Terraform and Ansible solve different problems in the DevOps lifecycle.
Terraform is primarily an Infrastructure as Code tool used to provision and manage infrastructure, while Ansible is primarily an automation and configuration management tool used to configure systems and deploy applications.
Instead of choosing one tool in every situation, organizations can use both:
Terraform → Provision
Ansible → Configure
Together, they can form a powerful automation workflow for cloud infrastructure and application environments.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article