Terraform Basics
Learn how Terraform turns infrastructure into repeatable code, from providers and state to planning safe changes.
Terraform is an infrastructure as code tool that lets teams describe cloud and platform resources in configuration files, then create, update, and remove those resources in a predictable way.
What Terraform Does
Instead of clicking through cloud dashboards, you write the desired infrastructure in HashiCorp Configuration Language, usually called HCL. Terraform reads that configuration, compares it with the real environment, and works out what needs to change.
Core Concepts
- Providers: Plugins that let Terraform talk to platforms such as AWS, Azure, Google Cloud, Kubernetes, Cloudflare, and many others.
- Resources: The infrastructure objects Terraform manages, such as virtual machines, networks, databases, DNS records, and security rules.
- Variables: Inputs that make configurations reusable across environments like development, staging, and production.
- Outputs: Values Terraform prints after applying changes, such as an instance IP address or load balancer hostname.
- State: Terraform’s record of the resources it manages and how they map to real infrastructure.
The Terraform Workflow
- Write: Define infrastructure in
.tffiles. - Init: Run
terraform initto download providers and prepare the working directory. - Plan: Run
terraform planto preview what Terraform will create, update, or destroy. - Apply: Run
terraform applyto make the planned changes. - Review: Keep configuration, state, and real infrastructure aligned over time.
Why State Matters
Terraform state is critical because it tracks what Terraform controls. For individual learning, local state may be enough. For teams, remote state with locking is usually safer because it prevents multiple people from changing the same infrastructure at the same time.
Modules and Reuse
Modules are reusable Terraform packages. A module can define a standard network, application stack, or database pattern once, then allow teams to deploy it repeatedly with different inputs. Good modules reduce duplication and help enforce infrastructure standards.
Good Terraform Habits
- Always review
terraform planbefore applying changes. - Use version control for Terraform configuration.
- Keep secrets out of plain Terraform files and state where possible.
- Pin provider versions to avoid unexpected behavior changes.
- Use remote state and locking for shared environments.
- Start with small, understandable changes before managing large systems.
Final Thoughts
Terraform’s main value is consistency. It turns infrastructure into code that can be reviewed, reused, tested, and improved over time. Once the fundamentals are clear, teams can move faster while reducing manual configuration mistakes.