Terraform for Beginners

matt
Matthew Gros · Sep 7, 2025

TLDR

Define resources in HCL, plan before apply, use remote state, modularize common patterns.

Terraform for Beginners

Infrastructure as Code

Define infrastructure in files. Review, version, repeat.

Basic Structure

# main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

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

resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t3.micro"

  tags = {
    Name = "web-server"
  }
}

Common Commands

# Initialize (download providers)
terraform init

# Preview changes
terraform plan

# Apply changes
terraform apply

# Destroy resources
terraform destroy

Variables

# variables.tf
variable "environment" {
  description = "Environment name"
  type        = string
  default     = "dev"
}

variable "instance_type" {
  type    = string
  default = "t3.micro"
}

# Use them
resource "aws_instance" "web" {
  instance_type = var.instance_type
  tags = {
    Environment = var.environment
  }
}

Outputs

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

Remote State

Don't commit state files:

terraform {
  backend "s3" {
    bucket = "my-terraform-state"
    key    = "prod/terraform.tfstate"
    region = "us-east-1"
  }
}

Modules

Reusable components:

module "vpc" {
  source = "./modules/vpc"
  cidr   = "10.0.0.0/16"
}

module "web" {
  source    = "./modules/ec2"
  vpc_id    = module.vpc.id
  subnet_id = module.vpc.public_subnet_id
}

Tips

  1. Always run plan before apply
  2. Use workspaces for environments
  3. Keep modules small and focused
  4. Lock provider versions

About the Author

matt

I build and ship automation-driven products using Laravel and modern frontend stacks (Vue/React), with a focus on scalability, measurable outcomes, and tight user experience. I’m based in Toronto, have 13+ years in PHP, and I also hold a pilot’s license. I enjoy working on new tech projects and generally exploring new technology.