Day 1: What is Terraform?
We’ve established the “why” of Infrastructure as Code. Now, let’s meet the “how”: Terraform.
What is Terraform?
Terraform is an open-source Infrastructure as Code (IaC) tool, created by HashiCorp, that allows you to safely and predictably create, change, and improve infrastructure.
It uses a high-level configuration language called HashiCorp Configuration Language (HCL) to describe your desired infrastructure, and then it generates a plan to provision that infrastructure on any cloud provider.
Think of Terraform as a “master translator” for cloud APIs. You write a single configuration in HCL, and Terraform can translate that into API calls for AWS, Azure, Google Cloud, and hundreds of other providers.
The “Write, Plan, Apply” Workflow
Terraform’s core workflow is simple, yet incredibly powerful:
- Write: You define your infrastructure in HCL files (e.g.,
main.tf). - Plan: You run
terraform planto see what changes Terraform will make to your infrastructure. This is a crucial step that allows you to review and validate your changes before they are applied. - Apply: You run
terraform applyto execute the plan and provision your infrastructure.
This workflow is central to Terraform’s philosophy of “safe and predictable changes.”
+-----------------------+ +----------------------+ +---------------------+
| You (The User) | | Terraform | | Cloud Providers |
+-----------------------+ +----------------------+ +---------------------+
| | |
| 1. Writes HCL config | |
| (e.g., main.tf) | |
+---------------------------> | |
| | |
| 2. Runs `terraform plan` | |
+---------------------------> | Generates execution plan |
| | |
| <---------------------------+ Shows you the plan |
| | |
| 3. Runs `terraform apply` | |
+---------------------------> | Executes the plan |
| +---------------------------> | AWS API |
| +---------------------------> | Azure API |
| +---------------------------> | GCP API |
| | |
Key Benefits of Terraform
While many of the benefits of Terraform are inherent to IaC in general, Terraform has some unique features that make it a standout choice.
1. Provider-Agnostic (Multi-Cloud)
This is perhaps Terraform’s most significant advantage. Terraform is not tied to a specific cloud provider. You can use the same tool and workflow to manage your infrastructure across AWS, Azure, Google Cloud, and even on-premises data centers.
Scenario: A Multi-Cloud Strategy
Your company decides to adopt a multi-cloud strategy to avoid vendor lock-in and leverage the best services from different providers. They want to use AWS for their core computing (EC2), Google Cloud for their data analytics (BigQuery), and Cloudflare for their DNS and CDN.
Without Terraform, your operations team would need to learn and master three different sets of tools and APIs. With Terraform, they can use a single, consistent workflow to manage all of these resources.
Your main.tf might look something like this:
# Configure the AWS provider
provider "aws" {
region = "us-west-2"
}
# Configure the Google Cloud provider
provider "google" {
project = "my-gcp-project"
region = "us-central1"
}
# Configure the Cloudflare provider
provider "cloudflare" {
api_token = var.cloudflare_api_token
}
# Provision an EC2 instance on AWS
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
# Provision a BigQuery dataset on GCP
resource "google_bigquery_dataset" "dataset" {
dataset_id = "my_dataset"
}
This ability to manage a multi-cloud environment from a single control plane is a game-changer for modern enterprises.
2. The State File
Terraform records information about the infrastructure it manages in a state file. This file acts as a “map” between your Terraform configuration and the real-world resources.
The state file is crucial for:
- Performance: Terraform stores the IDs of the resources it manages, so it doesn’t have to query the cloud provider for every resource on every run.
- Dependency Management: Terraform can build a dependency graph of your resources and provision them in the correct order. For example, it knows that it needs to create a VPC before it can create a subnet within that VPC.
- Drift Detection: As we discussed earlier, Terraform uses the state file to compare your configuration with the real world and detect drift.
Important: The state file is a critical piece of your infrastructure. It often contains sensitive information and must be stored securely. We will cover state management in depth later in this course.
3. The Declarative Model
Terraform uses a declarative approach to IaC. This means you describe the desired end state of your infrastructure, and Terraform figures out the most efficient way to get there.
This is in contrast to an imperative approach (used by tools like Ansible or shell scripts), where you have to specify the exact sequence of steps to take.
Scenario: Scaling a Web Farm
You have a fleet of 5 web servers defined in your Terraform configuration:
resource "aws_instance" "web" {
count = 5
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
Now, you need to scale up to 10 servers. With a declarative tool like Terraform, you simply change count = 5 to count = 10.
When you run terraform plan, Terraform will see that the desired state is 10 instances, the current state (from the state file) is 5 instances, and it will generate a plan to create 5 new instances.
With an imperative tool, you would have to write a script that says, “check how many servers are running, and if it’s less than 10, create the missing number of servers.” This is much more complex and error-prone.
Terraform’s provider-agnostic nature, powerful state management, and declarative approach make it the de facto standard for IaC in the modern cloud era. As we continue our journey, you’ll see just how powerful and flexible this tool can be.