Terraform Fundamentals (IaC)
Understand Terraform’s core workflow: init, validate, plan, apply. Learn state, providers, variables, and modules with practical examples.
Terraform is infrastructure-as-code.
Instead of manually creating resources (VMs, networks, IAM roles), you describe desired infrastructure in .tf files.
Terraform then computes what to change.
Theory first: declarative infrastructure lifecycle
Terraform is a declarative model where configuration defines target state and state files track current reality. Planning is the critical feedback phase between intent and mutation.
Treat the workflow as a lifecycle: design the desired architecture, review planned drift and impact, then apply controlled changes. This reduces accidental infrastructure mutations and improves team safety.
Learning outcomes
By the end you’ll know:
- the Terraform workflow (init/validate/plan/apply)
- what providers, resources, variables, outputs are
- how state influences changes
1) Terraform workflow
Typical commands:
terraform init
terraform validate
terraform plan
terraform apply
2) A minimal example
main.tf
terraform {
required_version = ">= 1.6.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = var.aws_region
}
resource "aws_s3_bucket" "example" {
bucket = "my-unique-bucket-name-12345"
}
variables.tf
variable "aws_region" {
type = string
default = "us-east-1"
}
outputs.tf
output "bucket_name" {
value = aws_s3_bucket.example.bucket
}
3) State (important)
State is stored in terraform.tfstate locally by default.
In real teams you typically use remote state (e.g. S3 + DynamoDB locking) so:
- multiple engineers can work safely
- Terraform uses locking to prevent concurrent updates
4) Plan before apply
Always review terraform plan:
- what will be created
- what will be changed
- what might be destroyed
5) Modules: reuse configuration
A module is a folder containing Terraform code that can be called like a component.
Example:
module "vpc" {
source = "./modules/vpc"
cidr_block = "10.0.0.0/16"
}
6) Next steps
Next tutorials:
- Terraform modules (reusable patterns)
- Terraform plan/apply in CI/CD (GitHub Actions)
- Terraform best practices for state and secrets