๐ Terraform Notes โ The Complete Guide for Beginners

Author: Deepakraj Ravi
Use Case: A structured and practical Terraform reference for DevOps practitioners and learners.
โ What is Terraform?
Terraform is an Infrastructure as Code (IaC) tool developed by HashiCorp that enables you to define, provision, and manage infrastructure in a safe, repeatable way using a simple configuration language called HCL (HashiCorp Configuration Language).
๐ What Can You Do With Terraform?
Manage any kind of infrastructure across multiple providers like AWS, Azure, GCP, etc.
Track infrastructure changes using a state file (
terraform.tfstate
).Automate provisioning and changes in your infrastructure.
Version control infrastructure (excluding state files) using Git.
Standardize and collaborate on infrastructure as code with teams.
๐ Terraform Lifecycle
Write Configuration
Use.tf
files written in HCL to describe the desired infrastructure. Documentation is well-maintained on the Terraform Docs.Initialize
Runterraform init
to download the required provider plugins and initialize the working directory.Plan (Dry Run)
Useterraform plan
to preview changes without applying them. This helps you review what Terraform intends to do.Apply
Runterraform apply
to provision the defined infrastructure. It updates the state file after execution.Destroy
Useterraform destroy
to remove all infrastructure resources defined in the configuration.
๐ ๏ธ Common Terraform Commands
terraform init # Initialize Terraform project
terraform plan # Show execution plan
terraform apply # Apply the changes
terraform destroy # Destroy all infrastructure
๐งฑ Terraform File Structure
main.tf โ Main configuration file (provider & resources)
input.tf โ Input variables
output.tf โ Output values after deployment
terraform.tfstate โ Auto-generated file that stores the infrastructure state
- ๐ Best Practice: Never version-control
terraform.tfstate
. Store it in a remote backend (e.g., S3) instead.
๐ State File Management
Terraform uses the state file as the single source of truth.
Store it in a centralized location (e.g., S3 bucket) with read-only access for users.
Terraform should have write access to update it after every apply.
For safe concurrent use, lock it using DynamoDB.
# backend config for remote state
terraform {
backend "s3" {
bucket = "your-terraform-state-bucket"
key = "dev/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-locks"
}
}
๐ Example main.tf
โ Basic AWS Instance
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "web" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro"
tags = {
Name = "HelloWorld"
}
}
๐ The provider block (e.g., AWS region) remains the same for most files unless the infrastructure or environment changes.
๐งฎ Variables and Outputs
Input Variables โ input.tf
variable "instance_type" {
type = string
default = "t3.micro"
}
Output Values โ output.tf
output "instance_id" {
value = aws_instance.web.id
}
๐ Using Modules
Modules are reusable Terraform components. They help organize large Terraform projects and reduce duplication.
module "ec2_instance" {
source = "./modules/ec2"
instance_type = var.instance_type
}
๐๏ธ Isolate and Organize Terraform Scripts
Break Terraform into logical directories by service or component.
Helps reduce the blast radius during changes.
Keeps the infrastructure modular and maintainable.
๐ Remote State Setup
Create an S3 bucket for storing state files.
Create a DynamoDB table to lock state during concurrent operations.
Define the remote backend in the
terraform
block of your localmain.tf
.
โ Best Practice: Keep Terraform configuration in Git (version control) and store state separately in a remote backend.
โ๏ธ Terraform Roles & Responsibilities
Main.tf โ Resource definitions and providers.
Remote State โ S3 + DynamoDB for lock.
Local Execution โ Terraform CLI.
Version Control โ Exclude state file (
.gitignore
).
โ Common Problems with Terraform
Single Source of Truth
- The state file is the only record of current infra. If itโs lost or corrupted, recovery is difficult.
Manual Drift
- Changes made directly in the cloud console are not detected unless explicitly refreshed.
Not GitOps Friendly
- Poor native integration with GitOps tools like ArgoCD or Flux.
Complexity at Scale
- Without modules and workspaces, managing large infrastructure can become chaotic.
Not for Configuration Management
- Terraform is meant for provisioning, not managing software/configurations on provisioned systems (use Ansible for that).
๐ฆ Final Structure Example
terraform-project/
โ
โโโ main.tf
โโโ input.tf
โโโ output.tf
โโโ variables.tf
โโโ modules/
โ โโโ ec2/
โ โโโ main.tf
โ โโโ variables.tf
โ โโโ outputs.tf
โโโ backend/
โโโ s3_dynamodb.tf
โ Summary
Feature | Description |
Infrastructure Type | Cloud, on-prem, hybrid |
Language | HCL (HashiCorp Configuration Language) |
Main File | main.tf |
Variable File | input.tf or variables.tf |
Output File | output.tf |
State File | terraform.tfstate (store in S3, lock with DynamoDB) |
Tool for Config Mgmt | Use Ansible, not Terraform |
GitOps Compatibility | Lacks native support |
๐ That's it! You're now equipped with a solid foundation to work with Terraform confidently.
Subscribe to my newsletter
Read articles from Deepak Raj directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
