Podcast Title

Author Name

0:00
0:00
Album Art

Terraform Explained In 10 Minutes

By 10xdev team August 11, 2025

Welcome to this introduction to Terraform. In this article, we'll explore what Terraform is, why you need it, and its primary use cases. We'll cover when to use Terraform and, just as importantly, when not to. Following that, we'll examine other tools in the infrastructure as code space and see how Terraform stacks up against them, including a detailed comparison with Ansible, a prominent configuration management tool. We will clarify the key distinction between infrastructure provisioning, which Terraform excels at, and configuration management, Ansible's specialty.

We will then dive into the practical steps of using Terraform to create resources, highlighting why it's known as a declarative tool. We'll also cover essential Terraform commands to get you started. Finally, this article will touch on Terraform's plug-in architecture, explaining how providers extend its capabilities and enable authentication with various cloud platforms.

The Need for Infrastructure Automation

Manually creating servers, networks, databases, and permissions for every environment—from development and QA to staging and production—is a slow and error-prone process. Imagine having multiple environments, each with numerous instances, virtual networks, and databases. Repeating these setup steps manually for each one is inefficient and invites inconsistency. This is precisely the problem that infrastructure as code tools like Terraform are designed to solve.

Terraform is an open-source tool that lets you define and provision cloud infrastructure using simple, declarative configuration files. It champions repeatability and consistency by treating your entire infrastructure as code. This allows you to version control it, review changes, and reuse configurations across teams and environments. With a single piece of code, you can provision a development environment and then use that same code to reliably spin up QA, staging, and production environments. This automation ensures that all environments are consistent, eliminating the "it works on my machine" problem.

Key Benefits of Terraform:

  • Eliminates Manual Setup: Terraform automates the provisioning of servers, databases, and networks, which significantly reduces manual errors and delays.
  • Multi-Cloud and Service Compatibility: A major advantage of Terraform is its ability to work across numerous cloud providers and services. It supports AWS, Azure, Google Cloud, Kubernetes, GitHub, Docker, and many others, all using a consistent syntax and workflow.
  • Version Control for Infrastructure: Since your infrastructure is defined in code, you can use version control systems like Git to track changes over time, collaborate with your team on infrastructure reviews, and approve modifications just like you would with application code.
  • CI/CD Integration: You can integrate infrastructure provisioning directly into your CI/CD pipelines. This enables automatic infrastructure updates as part of your DevOps workflows, triggering provisioning as soon as code is committed to a repository.
  • Reduced Cloud Costs: Automation simplifies tearing down environments. With Terraform, you can destroy all provisioned resources with a single command, ensuring you don't leave costly, unused infrastructure running.

When to Use Terraform (and When Not To)

When Not to Use Terraform

  • Fine-Grained Configuration Management: If your primary task is managing OS configurations, installing packages, or handling runtime processes on existing servers, tools like Ansible, Chef, and Puppet are better suited. This is known as configuration management, which operates inside a server, whereas Terraform focuses on provisioning the server itself.
  • Small and Static Infrastructure: For very small infrastructures with only a couple of servers that rarely change, Terraform might be overkill and add unnecessary complexity.
  • Teams Preferring GUI-Based Provisioning: Terraform follows a code-first approach using its own language (HCL). If your team is non-technical or prefers clicking through cloud provider consoles, this may not be the ideal workflow.
  • Quick, One-Time Changes: For temporary or quick, one-off adjustments, using the cloud provider's console or CLI might be faster than writing, planning, and applying a Terraform configuration.
  • Lack of Git Practices: Terraform works best within a version control workflow. If your team is resistant to using Git or similar practices, you will miss out on many of its core benefits, such as collaboration, history, and reviewability.

When Terraform is Recommended

  • Automating Complex Cloud Infrastructure: If you need to automatically provision VMs, databases, networks, and security groups, Terraform is the perfect tool.
  • Creating Repeatable Environments: To ensure identical dev, QA, staging, and production environments and reduce environment-specific bugs, Terraform provides the necessary consistency.
  • Managing Multi-Cloud or Hybrid Cloud: Terraform shines in environments that span across AWS, Azure, Google Cloud, and on-premises resources, offering a single tool for multiple platforms.
  • Adopting Infrastructure as Code (IaC): For teams committed to IaC, Terraform is the industry-leading choice. It allows you to store infrastructure definitions in Git for superior collaboration, review, and rollback capabilities.
  • Integrating Infrastructure into CI/CD Pipelines: Terraform is an excellent fit for provisioning infrastructure directly from a CI/CD pipeline.
  • Adopting Immutable Infrastructure: In an immutable infrastructure model, you don't modify existing servers. Instead, you provision new servers with the updated configuration. Terraform is ideal for this practice, as it makes creating new resources from code simple and reliable.

How Terraform Compares to Other IaC Tools

Cloud-Specific Tools

Most cloud platforms offer their own native IaC tools:

  • AWS CloudFormation: Native to AWS, it uses JSON or YAML templates. It offers deep integration with AWS services but lacks multi-cloud flexibility.
  • Azure Resource Manager (ARM) & Bicep: ARM is the native IaC service for Azure, using verbose JSON templates. Bicep is a more recent, cleaner domain-specific language that compiles to ARM templates. Both are best for Azure-only projects.
  • Google Cloud Deployment Manager: Google's native tool uses YAML and Jinja2 templates and is suitable only for Google Cloud setups.

While these tools are powerful within their respective ecosystems, a cloud-neutral tool like Terraform allows your team to develop a single skillset applicable across any platform.

Cloud-Neutral Alternatives

  • Pulumi: A direct competitor to Terraform, Pulumi also manages infrastructure as code. The key difference is that Pulumi allows you to use familiar programming languages like Python, JavaScript, TypeScript, and Go instead of a domain-specific language like HCL. This is a great option for development teams who want to leverage their existing coding skills.
  • Configuration Management Tools (Ansible, Chef, Puppet): As mentioned earlier, these tools focus on configuring what's inside an existing server—managing applications, processes, and files. While they have some provisioning capabilities, their strength lies in configuration, whereas Terraform's strength is in provisioning.

A Closer Look: Terraform vs. Ansible

| Feature | Terraform | Ansible | | :--- | :--- | :--- | | Primary Focus | Infrastructure Provisioning | Configuration Management | | Approach | Declarative ("what I want") | Imperative ("how to do it") | | Core Use | Create, modify, and delete cloud resources | Configure apps on existing servers | | Language | HashiCorp Configuration Language (HCL) | YAML-based Playbooks | | State Management | Stateful (tracks resources in a state file) | Stateless (by default) | | Strengths | Managing infrastructure at scale | Fine-grained server configuration | | Best For | Provisioning infrastructure on cloud providers | Configuring both cloud and on-prem servers |

Our recommendation is to use the right tool for the job: Terraform for provisioning infrastructure and Ansible for configuring it.

Creating Cloud Resources with Terraform: A Step-by-Step Guide

So, how do you actually use Terraform to create resources? The process involves a few key steps.

Step 1: Write HCL Configuration Files

You start by defining the infrastructure you want in files with a .tf extension, using the HashiCorp Configuration Language (HCL).

For example, to create an S3 bucket and an IAM user in AWS, your configuration might look like this:

# Configure the AWS provider
provider "aws" {
  region = "us-east-1"
}

# Create an S3 bucket
resource "aws_s3_bucket" "my_s3_bucket" {
  bucket = "my-unique-app-bucket-name-2025" # Must be globally unique

  versioning {
    enabled = true
  }

  tags = {
    Name        = "My App Bucket"
    Environment = "Dev"
  }
}

# Create an IAM user
resource "aws_iam_user" "my_user" {
  name = "my-iam-user"
}

Step 2: Initialize the Project

Run terraform init. This command downloads the necessary provider plugins (in this case, for AWS) and sets up the local working directory.

Step 3: Validate the Configuration

Run terraform validate. This checks your .tf files for syntax errors or misconfigurations before you proceed.

Step 4: Plan the Changes

Run terraform plan. This is a crucial step where Terraform shows you exactly what it will create, update, or destroy. It prevents surprises by giving you a chance to review the changes before they are applied.

Step 5: Apply the Changes

Run terraform apply. This command executes the plan and provisions the defined infrastructure in your cloud account.

To make changes, you simply update your .tf files, run terraform plan to see the difference, and terraform apply to implement it. When you're done with the resources, terraform destroy will tear down everything managed by the configuration.

The Power of a Declarative Approach

Terraform uses a declarative approach, meaning you describe the desired end state, not the step-by-step instructions on how to get there. You declare that you want an S3 bucket with versioning enabled, and Terraform figures out the necessary API calls to make it happen.

This is different from an imperative approach (used by shell scripts or Ansible), where you provide explicit commands: "create a subnet," "launch a VM," etc.

Why is the declarative approach so powerful?

  • Execution Plan Visibility: terraform plan gives you a detailed preview of all actions, helping you catch unintended consequences before they happen.
  • Idempotent Behavior: Running the same configuration multiple times produces the same result. If the infrastructure already matches the code, Terraform does nothing on subsequent runs. An imperative script might try to create a duplicate resource, causing an error.
  • Minimized Human Error: You don't have to write complex, step-by-step logic. Terraform understands resource dependencies (e.g., a network must exist before a VM can be placed in it) and computes the correct order of operations automatically.
  • Safer Environment Creation: Because your final, version-controlled code represents the exact desired state, you can confidently use it to create new environments (dev, QA, prod) that are consistent and repeatable.

Join the 10xdev Community

Subscribe and get 8+ free PDFs that contain detailed roadmaps with recommended learning periods for each programming language or field, along with links to free resources such as books, YouTube tutorials, and courses with certificates.

Recommended For You

Up Next