Terraform: Writing Your Infrastructure as Code
Imagine this. You join a new project and need Linux servers, databases, and a load balancer on the cloud for your application. If you click through cloud dashboards to do everything manually, you’ll soon face confusing problems:
- “How many servers did we create?”
- “Did we configure database backups?”
- “The employee who built all this quit—how do we know what they did?”
Manual infrastructure work is full of hidden mistakes, and fixing or understanding what happened later becomes a nightmare.
This is where Terraform walks in, like a hero.
What is Terraform?
Terraform is an Infrastructure as Code (IaC) tool. Instead of clicking buttons in a console, you write code that describes the servers, databases, and networks you need. You declare your infrastructure blueprint in code; Terraform reads it and creates exactly what you describe. Everything is tracked, so changes and fixes are easy.
Where, Why, and When
- Where: Any cloud (AWS, Azure, GCP), and even on-premises.
- Why: Avoid manual errors, improve reliability, track infrastructure changes.
- When: When building repeatable, consistent environments and collaborating as a team.
What is HCL?
Terraform’s code uses HCL (HashiCorp Configuration Language), created by HashiCorp for simplicity and readability. HCL makes writing infrastructure as code feel natural and is now used widely across HashiCorp’s tools. Almost every Terraform script you see (.tf files) is written in HCL.
HCL lets you declare resources and their properties inside blocks with curly braces. It’s human-friendly, machine-readable, and fully declarative.
Basic Example: AWS and Azure Resources
AWS Example – Creating a Simple Linux Server
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "myserver" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "MyFirstServer"
}
}
This sample declares the AWS provider and a virtual machine resource. You just change the code and re-apply to update your infrastructure.
Azure Example – Creating a Resource Group
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "mygroup" {
name = "my-terraform-group"
location = "East US"
}
This sample sets up the Azure provider and creates a resource group, which you can use as the foundation for deploying virtual machines, databases, web apps, and more.
The Beginning—and Beyond
These are just “Hello World” starting points for Terraform.
In the same way, you can provision clusters of web servers, load balancers, blob storage, and many types of resources across multiple clouds using simple declarative blocks. Terraform works exactly the same way with GCP, Oracle Cloud, and many other platforms—all with HCL syntax.
“These are basic beginning examples; like these, there are many more.”
Why HCL Stands Out
- Readable (easy for humans): Simple syntax, clear structure.
- Flexible: Handles basic and complex infrastructure.
- Declarative: Says what you want, not how to do it.
- Extensible: Works across AWS, Azure, GCP, and more.
Basic Terraform Commands to Know
When using Terraform, you work mostly with these common commands:
-
terraform init
— Initialize your working directory, download provider plugins. -
terraform validate
— Check your configuration syntax for errors. -
terraform plan
— Preview the changes Terraform will make. -
terraform apply
— Apply the desired changes and create/update resources. -
terraform destroy
— Destroy all resources managed by your configuration. -
terraform fmt
— Format your Terraform files according to HCL conventions. -
terraform show
— Display the current state or a saved plan. -
terraform output
— Show the output values defined in your configuration.
Using these commands correctly ensures your infrastructure is deployed reliably and trackable.
What happens if you change a resource definition in Terraform code but forget to run the terraform apply
command?
Top comments (0)