Articles

What is Terraform? Complete Guide to IaC Tools

Terraform is an open-source Infrastructure as Code (IaC) tool that automates the provisioning and management of cloud infrastructure. Created by HashiCorp, this open-source tool lets us define and deploy infrastructure resources across AWS, Azure, Google Cloud, and 100+ other providers using simple, human-readable configuration files.

Terraform is primarily used for multi-cloud infrastructure provisioning, automating environment deployments across development, staging, and production, and managing infrastructure as version-controlled code. Organizations use it to eliminate manual cloud configuration, ensure consistency across deployments, and enable disaster recovery through infrastructure replication.

Infrastructure as Code means managing infrastructure through version-controlled files instead of manual configuration. The tool uses HCL (HashiCorp Configuration Language), a declarative syntax where you describe what infrastructure we need, and Terraform automatically creates it.

Terraform Infrastructure as Code vs manual setup comparison

Now that the basics are clear, the next section explores how Terraform actually works.

  • Learn Terraform Infrastructure as Code covering essential concepts, provisioning techniques, deployment best practices and infrastructure management skills.
    • Includes 44 Courses
    • With Certificate
    • Intermediate.
      30 hours
  • Learn Kubernetes fundamentals from orchestration to security, provisioning, and workflows. Discover its use cases and compare Kubernetes vs Docker.
    • Beginner Friendly.
      1 hour

How does Terraform work?

What is Terraform architecture?

Terraform has two main components:

  • Terraform Core is the execution engine written in Go. It manages state, calculates dependencies, and determines what changes need to happen.
  • Terraform Providers are plugins that connect to APIs of different platforms like AWS, Azure, or GCP. HashiCorp and the community maintain thousands of providers.

Terraform core state management architecture diagram

Three-Stage workflow of Terraform

Terraform follows a simple three-step process:

  • Write: Define infrastructure in .tf files using HCL syntax. These files describe what resources are needed.
  • Plan: Run terraform plan to preview changes. Terraform compares the desired state with the current state and shows what will be created (+), modified (~), or destroyed (-).
  • Apply: Run terraform apply to execute changes. Terraform creates, updates, or deletes resources in the correct order, respecting dependencies.

Terraform three-stage write plan apply workflow diagram

Core components of Terraform

  • Configuration Files contain infrastructure definitions written in .tf files.
  • State Files track current infrastructure in terraform.tfstate (JSON format). This is how Terraform knows what exists and what needs to change.
  • Providers connect Terraform to cloud platforms.
  • Resources are infrastructure components like VMs, databases, or networks. Each resource has a type and configuration parameters.
  • Data Sources query existing infrastructure that Terraform didn’t create.
  • Modules are reusable infrastructure patterns that package multiple resources together.
  • Variables make configurations flexible by accepting different values for different environments.
  • Outputs extract information like IP addresses or IDs after deployment.

The next section covers important Terraform commands.

Terraform commands

Terraform operates through command-line interface (CLI) commands. These commands control the entire infrastructure lifecycle from initialization to deployment.

Essential Commands:

Command Purpose When to use
terraform init Initialize working directory and download providers First command in any new project or after adding providers
terraform fmt Format code to HCL standards Before committing code to version control
terraform validate Check configuration for syntax errors After writing or modifying configuration files
terraform plan Preview infrastructure changes Before applying changes to see what will happen
terraform apply Execute the infrastructure changes When ready to create, update, or delete resources
terraform destroy Remove all managed infrastructure When tearing down an environment completely
terraform output Display output values To retrieve information like IP addresses or IDs
terraform show Inspect current state or saved plan To view what infrastructure exists
terraform state list List all resources in state To see what Terraform is currently managing

The standard sequence for working with Terraform follows four steps:

Terraform CLI commands workflow diagram

Step 1: Initialize

terraform init

Downloads required providers and prepares the working directory. Must be run before any other commands.

Step 2: Validate

terraform validate

Checks configuration files for errors without connecting to any remote services.

Step 3: Plan

terraform plan

Shows exactly what changes will be made. Output includes:

  • Resources to be created (+ symbol)
  • Resources to be modified (~ symbol)
  • Resources to be destroyed (- symbol)

Step 4: Apply

terraform apply

Executes the planned changes. Terraform displays the plan again and requests confirmation before proceeding.

These commands form the foundation of daily Terraform operations. The next section explores real-world applications and use cases.

What is Terraform used for?

Terraform automates cloud infrastructure provisioning and management. Organizations use it for multi-cloud deployments, environment creation, disaster recovery, and infrastructure automation.

Terraform use cases across multi-cloud environments

Multi-Cloud infrastructure

Terraform manages resources across AWS, Azure, and Google Cloud simultaneously from a single configuration. The unified interface eliminates vendor lock-in by providing consistent workflows regardless of cloud provider.

Environment provisioning

Development, staging, and production environments are built from identical infrastructure code. Variable files determine environment-specific settings like server count and instance size, ensuring consistency while allowing appropriate scaling.

Environment Configuration Resources
Development Small instances 2 servers
Staging Medium instances 5 servers
Production Large instances 20+ servers

Disaster recovery

Infrastructure code in version control becomes the disaster recovery plan. The same Terraform configuration can recreate entire environments in different regions or cloud providers when failures occur.

Kubernetes management

Terraform provisions managed Kubernetes clusters across major platforms:

  • Amazon EKS - Elastic Kubernetes Service
  • Azure AKS - Azure Kubernetes Service
  • Google GKE - Google Kubernetes Engine

Cluster creation, node pools, networking, and IAM configurations are all handled through Terraform definitions.

CI/CD integration

Infrastructure changes flow through continuous deployment pipelines alongside application code. When commits are pushed, automated workflows execute Terraform to keep infrastructure synchronized with application requirements.

Key benefits

Benefit Description
Cloud-agnostic Single tool works across all major cloud providers
Declarative Define desired state, Terraform handles implementation
State tracking Monitors all resources and detects configuration drift
Preview changes See exact changes before execution
Version control Infrastructure code lives in Git with full history
Cost control Destroy unused resources instantly with one command

Terraform provides value wherever infrastructure automation, consistency, and multi-cloud support are needed. The next section compares Terraform with alternative tools.

Terraform vs other IaC tools

Terraform isn’t the only infrastructure automation tool available. This section compares it with Ansible, CloudFormation, Pulumi, and Kubernetes to show how they differ and when each one works best.

Comparison overview

Feature Terraform Ansible CloudFormation Pulumi Kubernetes
Primary purpose Infrastructure provisioning Configuration management Infrastructure provisioning Infrastructure provisioning Container orchestration
Cloud support 100+ providers Multi-cloud via modules AWS only Multi-cloud Any cloud (containers)
Language HCL YAML JSON/YAML Python, TypeScript, Go, etc. YAML
Approach Declarative Procedural Declarative Declarative Declarative
State management State file Stateless Stack-based state State file Desired state for containers
Learning curve Moderate Easy Moderate Moderate to Hard Hard
Best for Multi-cloud infrastructure Server configuration AWS-only infrastructure Code-first approach Container management
License BSL (1.5+), MPL 2.0 (older) GPL 3.0 Proprietary Apache 2.0 Apache 2.0

Tool selection guide

  • Terraform excels at multi-cloud infrastructure provisioning with comprehensive state management across all major cloud platforms.
  • Ansible specializes in server configuration and application deployment, working on infrastructure that already exists.
  • CloudFormation provides native AWS integration for teams operating exclusively within the Amazon ecosystem.
  • Pulumi allows infrastructure definitions using standard programming languages like Python, TypeScript, and Go instead of domain-specific languages.
  • Kubernetes orchestrates containerized applications at scale, managing deployment, scaling, and operations of application containers.

Using tools together

These tools complement each other in production environments:

  • Terraform + Ansible: Terraform establishes the infrastructure foundation while Ansible configures the servers and deploys applications.
  • Terraform + Kubernetes: Terraform creates the cluster infrastructure, then Kubernetes orchestrates the containers running within those clusters.

Terraform with our tools

Each tool addresses specific problems. Terraform excels at infrastructure provisioning, while complementary tools handle configuration management and container orchestration.

Conclusion

Terraform has transformed how teams manage cloud infrastructure. What once required hours of manual configuration now takes minutes of code execution.

What makes Terraform essential:

  • Automates infrastructure across AWS, Azure, GCP, and 100+ platforms using simple HCL syntax
  • Follows a straightforward write-plan-apply workflow that catches errors before deployment
  • Enables teams to version control infrastructure just like application code
  • Prevents vendor lock-in by working seamlessly across all major cloud providers

The shift from manual infrastructure management to Infrastructure as Code isn’t just about automation. It’s about consistency, reliability, and the ability to scale operations without scaling headcount. Teams using Terraform deploy faster, make fewer mistakes, and recover from failures more quickly.

If you’re ready to start building with Terraform, take Codecademy’s Terraform Expedition: Exploring Infrastructure as Code, a hands-on skillpath with real-world projects to master infrastructure automation from the ground up.

Frequently asked questions

1. What is the main use of Terraform?

Terraform provisions and manages cloud infrastructure through code, automating the creation of servers, databases, networks, and other resources across AWS, Azure, Google Cloud, and 100+ platforms.

2. Which language is used in Terraform?

Terraform uses HCL (HashiCorp Configuration Language), a declarative language with simple, human-readable syntax. JSON format is also supported.

3. What is the difference between Terraform and Kubernetes?

Terraform provisions infrastructure (servers, networks, databases) while Kubernetes orchestrates containers on that infrastructure. Teams often use both: Terraform creates the clusters, and Kubernetes manages the containers.

4. Why use Terraform instead of AWS?

Terraform works with AWS, Azure, GCP, and 100+ providers, preventing vendor lock-in. CloudFormation only works with AWS, while Terraform provides multi-cloud capability and transferable skills.

Codecademy Team

'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'

Meet the full team

Learn more on Codecademy

  • Learn Terraform Infrastructure as Code covering essential concepts, provisioning techniques, deployment best practices and infrastructure management skills.
    • Includes 44 Courses
    • With Certificate
    • Intermediate.
      30 hours
  • Learn Kubernetes fundamentals from orchestration to security, provisioning, and workflows. Discover its use cases and compare Kubernetes vs Docker.
    • Beginner Friendly.
      1 hour