Terraform Configuration Files

If you’re building infrastructure with Terraform, configuration files are the heart of everything. They define what resources you want, how they should behave, and how they connect together all in a clean, readable format.

What Are Terraform Configuration Files

Terraform configuration files are plain text files written in HCL (HashiCorp Configuration Language). These files tell Terraform:

  • What infrastructure to create (servers, databases, networks, etc.)
  • Which cloud provider to use (AWS, Azure, GCP, etc.)
  • How resources should be configured

All Terraform config files usually have the extension .tf

Basic File Structure

A typical Terraform project might look like this:

main.tf (Main configuration)

variables.tf (Input variables)

outputs.tf (Outputs)

terraform.tfvars (Variable values)

You can put everything in one file, but separating them makes your code cleaner and easier to manage.

Core Components of Terraform Config Files

1. Provider Block

This tells Terraform which platform you are working with.

provider "aws" {
region = "us-east-1"
}
Think of it as: “Where should Terraform create resources?”

2. Resource Block

This is where you define what type of resource you want to create.

resource "aws_instance" "resource_name_local" {
ami = "ami-123456"
instance_type = "t2.micro"
}

  • aws_instance is resource type
  • resource_name_local is local name of the resource
  • Inside it there are configuration details

This means: “Create an EC2 instance with these settings.”

3. Variables

Variables make your configuration reusable.

variable "instance_type" {
default = "t2.micro"
}

Use it like this:

instance_type = var.instance_type

This lets you change values without editing the main code.

4. Output Values

Outputs display useful information after execution.

output "instance_ip" {
value = aws_instance.resource_name_local.public_ip
}

After running Terraform, you’ll see the instance IP printed.

5. terraform.tfvars (Optional)

Used to assign values to variables:

instance_type = "t2.small"

Keeps your configuration flexible and environment-specific.

How Terraform Uses These Files

When you run Terraform commands, it processes your config files in this order:

terraform init

  • Initializes the project
  • Downloads provider plugins

terraform plan

  • Shows what changes will happen

terraform apply

  • Actually creates/updates resources

Important Concepts

Declarative Approach

You describe what you want, not how to do it.

State File

Terraform keeps track of resources in a file called: terraform.tfstate

This helps Terraform understand what already exists.

Idempotency

Running Terraform multiple times won’t duplicate resources. It only applies changes when needed.

Example: Complete Minimal Config

provider "aws" {
region = "us-east-1"
}

variable "instance_type" {
default = "t2.micro"
}

resource "aws_instance" "resource_local_name" {
ami = "ami-123456"
instance_type = var.instance_type
}

output "instance_ip" {
value = aws_instance.resource_local_name.public_ip
}

Best Practices

  • Keep files modular (split into multiple .tf files)
  • Use variables instead of hardcoding values
  • Store secrets securely (avoid putting them in .tf files)
  • Use version control (Git)
  • Use remote state for teams

Terraform configuration files are simple once you understand the structure:

  • Provider → where to create
  • Resource → what to create
  • Variables → make it flexible
  • Outputs → get useful info