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 and create resources (AWS, Azure, GCP, etc.)
  • How resources should be configured

All Terraform config files usually have the extension .tf

Basic File Structure in a Terraform Project

A typical Terraform project might look like this:

main.tf (Main configuration file. It is not mandatory to create main.tf but it is a standard convention that is used in terraform projects)

variables.tf (variables are declared in the file)

outputs.tf (Outputs file has code that prints useful information when terraform creates resources)

terraform.tfvars (Variable values are assigned to the declared variables)

It is not necessary to create all above files. 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 want to create resources on

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.

  • aws_instance is resource type. ex: server, database, cdn etc.
  • resource_name_local is local name of the resource. think of it as a local variable which will be used to refer to the resource inside the file.
  • Inside it there are configuration details

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

3. Variables

Variables make your configuration reusable.

Use it like this:

This lets you change values without editing the main code.

4. Output Values

Outputs display useful information after execution.

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

5. terraform.tfvars file (Optional)

Used to assign values to variables:

Keeps your configuration files 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 Configuration

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