Skip to content

Terraform Basics

Write Terraform configurations with providers, resources, variables, and state management.

What you'll learn

  • Terraform syntax: blocks, arguments, and expressions
  • Providers and resources
  • Variables and outputs for reusable configs
  • State management and remote backends

Concept

Terraform Syntax

Terraform uses HCL (HashiCorp Configuration Language), a declarative language designed for configuration. A Terraform file consists of blocks — resource, provider, variable, output, terraform — each with a type and labels. Inside blocks, arguments assign values to properties. Expressions can reference other resources, use functions, and include conditionals. HCL is designed to be both human-readable and machine-parseable.

Providers and Resources

A provider is a plugin that lets Terraform manage a specific platform — AWS, Azure, Google Cloud, GitHub, Docker. You configure the provider with credentials and region. A resource is a piece of infrastructure — an EC2 instance, an S3 bucket, a DNS record. Each resource has a type (like aws_instance) and a local name. You set its properties with arguments. Resources can reference each other, creating dependencies that Terraform resolves automatically.

Variables and Outputs

Variables make configurations reusable. Define a variable with a type, default, and description, then reference it as var.name. You can override defaults via command-line flags, environment variables, or .tfvars files. Outputs expose values after the apply — the IP address of a created instance, the URL of a load balancer. Outputs are useful for passing infrastructure details to other tools or for inspection. Use locals for intermediate calculations within a configuration.

State Management

Terraform state is a JSON file that maps your configuration to real-world resources. It tracks what Terraform has created so it knows what to update or delete. Never edit state manually. Store state in a remote backend — an S3 bucket with DynamoDB locking, Terraform Cloud, or Azure Blob Storage. Remote state enables team collaboration (locking prevents concurrent applies), keeps state secure, and survives local machine failures. Use terraform import to bring existing resources under Terraform management.

Terraform Modules

Modules are reusable Terraform configurations — like functions for infrastructure. A module encapsulates a set of resources with input variables and outputs. You can use modules from the Terraform Registry or write your own. Modules promote consistency and reduce duplication: instead of copying the same VPC configuration into every project, you define it once as a module and reuse it. Keep modules focused and well-documented. Version your modules so consumers can pin a specific version and upgrade deliberately rather than getting unexpected changes.

Example

              # variables.tf — reusable input variables
# variable "region" {
#   default = "us-east-1"
#   type    = string
# }
# variable "instance_count" {
#   default = 2
#   type    = number
# }

# main.tf — the infrastructure
# resource "aws_instance" "web" {
#   count         = var.instance_count
#   ami           = "ami-0c02fb559f6fe2234"
#   instance_type = "t3.micro"
#   tags = { Name = "web-${count.index}" }
# }
# output "instance_ips" {
#   value = aws_instance.web[*].public_ip
# }

# Workflow
terraform init && terraform validate      # init and check syntax
terraform plan -var="instance_count=3"    # preview with override
terraform apply                           # create the resources
terraform output instance_ips             # show the outputs
            

Variables make configs reusable, outputs expose created resource attributes. The workflow is: init (setup), validate (check), plan (preview), apply (create), output (read results).

Try it

Common mistakes

The mistake

Storing Terraform state in git

The fix

Store state in a remote backend with locking (S3 + DynamoDB). State files can contain secrets and are not designed for version control. Git-based state causes conflicts and data loss.

The mistake

Not running terraform plan before apply

The fix

Always review the plan output before applying. The plan shows exactly what will be created, changed, or destroyed. Applying without reviewing risks unintended destruction of resources.

Practice Terraform Basics

2 exercises

Practice Terraform Basics

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors