Terraform Basics Getting Started with Infrastructure as Code
A beginner-friendly guide to understanding Terraform, its core concepts, and how to provision infrastructure efficiently using code.
Terraform Basics: Getting Started with Infrastructure as Code
Terraform is one of the most popular tools for managing infrastructure using code. Instead of manually creating resources in cloud platforms, Terraform allows you to define everything declaratively and version it just like application code.
In this article, we’ll walk through the fundamentals of Terraform and how to get started.
⸻
What is Terraform?
Terraform is an Infrastructure as Code (IaC) tool developed by HashiCorp. It allows you to define and provision infrastructure using a simple configuration language called HCL (HashiCorp Configuration Language).
With Terraform, you can manage: • AWS, Azure, GCP resources • Kubernetes clusters • Databases • Networking • And much more
⸻
Why Use Terraform?
Here are some key benefits:
- Declarative Configuration
You define what you want, not how to do it.
resource "aws_s3_bucket" "example" {
bucket = "my-bucket"
}
- Version Control
Infrastructure changes can be tracked via Git, just like application code.
- Reusability
Modules allow you to reuse infrastructure patterns.
- Multi-Cloud Support
Terraform works across multiple cloud providers with the same workflow.
⸻
Core Concepts
- Provider
A provider is responsible for interacting with APIs (AWS, Azure, etc.).
provider "aws" {
region = "ap-southeast-2"
}
⸻
- Resource
A resource defines a piece of infrastructure.
resource "aws_instance" "example" {
ami = "ami-123456"
instance_type = "t2.micro"
}
⸻
- Variables
Variables make your configuration flexible.
variable "instance_type" {
default = "t2.micro"
}
⸻
- Outputs
Outputs expose useful information after deployment.
output "instance_ip" {
value = aws_instance.example.public_ip
}
⸻
- State
Terraform keeps track of infrastructure using a state file (terraform.tfstate).
This helps Terraform: • Know what exists • Detect changes • Avoid duplications
⸻
Terraform Workflow
The typical workflow includes 4 steps:
- Initialize
terraform init
Downloads providers and initializes the project.
⸻
- Plan
terraform plan
Shows what Terraform will do before applying changes.
⸻
- Apply
terraform apply
Executes the plan and provisions infrastructure.
⸻
- Destroy
terraform destroy
Removes all resources defined in the configuration.
⸻
Example: Simple AWS EC2 Setup
provider "aws" {
region = "ap-southeast-2"
}
resource "aws_instance" "web" {
ami = "ami-123456"
instance_type = "t2.micro"
tags = {
Name = "terraform-example"
}
}
Run:
terraform init
terraform plan
terraform apply
⸻
Best Practices • Use remote state (S3 + DynamoDB for AWS) • Organize code with modules • Keep configs small and reusable • Avoid hardcoding values → use variables • Use .tfvars for environment configs
⸻
When Should You Use Terraform?
Terraform is ideal when: • You manage cloud infrastructure • You want reproducibility • You need consistent environments (dev/staging/prod) • You work in a DevOps or microservices architecture
⸻
Conclusion
Terraform is a powerful tool that brings automation, consistency, and scalability to infrastructure management. Once you understand the basics—providers, resources, and workflow—you can start building more complex systems with confidence.
If you’re a software engineer moving into DevOps or cloud, Terraform is a must-have skill.
Support My Work
If you've enjoyed my content and found it helpful, consider buying me a coffee. It keeps me caffeinated and creating!
Buy me a coffee