What Is Infrastructure as Code? Terraform Basics for Beginners

Most teams set up their first server by clicking through a cloud console. The second server gets the same treatment, the third too, and by the tenth nobody remembers exactly which settings differ. Terraform is the most common way teams practice infrastructure as code, and these infrastructure as code Terraform basics will take you from zero to a real server you created with a file you can review, version, and roll back.

Infrastructure as Code Terraform Basics: What It Actually Means

This is infrastructure as code explained in one line: your servers become files you can review, test, and roll back, just like application code. Instead of clicking through a console, you describe what you want in a configuration file, and a tool creates it, updates it, or destroys it. The description lives in version control, so a change to your infrastructure goes through the same pull request, review, and history as a change to your app.

Terraform is the tool most teams reach for first. It is open source, works with nearly every cloud provider, and uses a readable language called HCL. You do not need to learn a cloud provider’s API or console quirks. You write what you want, Terraform figures out the calls to make, and the state file records what exists so the next run only changes what drifted.

Why Teams Bother With IaC

Manual setup fails in a specific way: it works once, then slowly drifts. Someone changes a firewall rule to fix an incident and forgets to write it down. A staging server ends up with different settings than production, and a bug that never appears in staging takes production down. Nobody can reproduce the environment from scratch, so rebuilding after an outage becomes a guessing game.

Infrastructure as code fixes the drift problem by making the file the source of truth. If the live server differs from the file, the next run flags it. If you need a second environment, you apply the same files with different variables. If a change breaks something, you revert the commit and re-apply. Teams that adopt it usually do so after one painful outage, not before, which is a shame, because the setup cost is an afternoon, not a quarter.

Terraform’s Core Concepts

Terraform has four ideas you need before you write anything. Learn these and the rest is detail.

Providers

A provider is a plugin that talks to one platform: AWS, DigitalOcean, Cloudflare, or your own VPS through an API. You declare which provider to use and hand it credentials, usually through environment variables rather than in the file. Never put secrets in a Terraform file that goes into version control.

Resources

A resource is one thing Terraform manages: a virtual machine, a DNS record, a database, a firewall rule. Each resource has a type and a name you choose, and Terraform tracks it by that identity. Delete the block from your file and Terraform will offer to destroy the real thing, which is powerful and worth respecting.

State

State is Terraform’s record of what exists in the real world and how it maps to your files. By default it is a local file called terraform.tfstate. Keep it safe: losing state means Terraform forgets what it manages, and it may try to create duplicates. For anything beyond a personal experiment, store state remotely with locking, for example in an S3 bucket with a DynamoDB lock table, so two people never apply at the same time.

Plan and Apply

Terraform splits every change into two steps. terraform plan shows what it will create, change, or destroy, and terraform apply does it after you confirm. Read the plan every time. It is the review step that stops a typo from deleting a database.

A Terraform Configuration Example You Can Run

This terraform beginner guide sticks to one working example instead of listing every feature. The snippet below creates a small virtual machine on AWS. It assumes you have Terraform installed and AWS credentials in your environment. If you have never launched a server on AWS, start with a first walkthrough of EC2 so the console concepts make sense before you automate them.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t3.micro"

  tags = {
    Name = "terraform-first-server"
  }
}

Save that as main.tf in an empty directory, then run three commands:

terraform init
terraform plan
terraform apply

init downloads the AWS provider. plan shows one instance to create and nothing to destroy. apply asks for confirmation, then creates the server. Run terraform destroy when you are done experimenting, and check the plan output before confirming, since destroy really does delete the instance. Check official pricing before you leave anything running, because even a small instance bills by the hour.

Variables keep the same files reusable across environments. Move the region and instance type into variables and you can apply the same code to staging and production with different values:

variable "instance_type" {
  description = "EC2 instance size"
  type        = string
  default     = "t3.micro"
}

variable "environment" {
  description = "staging or production"
  type        = string
}

Reference them as var.instance_type and var.environment in the resource block, and pass values with -var flags or a terraform.tfvars file.

Plan, Apply, Destroy: The Daily Workflow

Day-to-day Terraform work is a short loop. Edit a file, run plan, read the diff, run apply. The loop stays the same whether you manage one server or one hundred, which is why teams can hand infrastructure work to any developer who can read a diff. Pair this loop with a CI/CD pipeline that runs terraform plan on every pull request, and bad changes get caught in review instead of in production.

Two habits keep the loop safe. First, never run apply with -auto-approve on shared infrastructure; the confirmation prompt is the last chance to catch a destroy you did not expect. Second, keep the state file backed up and locked. Losing state on a system with live servers means rebuilding your mental map of what exists, and that is exactly the manual work Terraform exists to remove. The same instinct applies to data: a separate off-site backup of state and data is cheap insurance.

State Files: The Part Beginners Break

State is where most beginners get hurt, usually in one of three ways. They commit terraform.tfstate to git, which exposes resource IDs and sometimes sensitive values. They delete the state file thinking it is a cache, then Terraform tries to create everything again and collides with what already exists. Or two people apply at once from different laptops and one overwrites the other’s changes.

All three have the same fix: remote state with locking, set up on day one, even for a side project. It takes ten minutes and it removes an entire class of accidents. Local state is fine for the first afternoon of learning. It is not fine for anything you would miss if it broke.

Common Mistakes and How to Avoid Them

  • Hardcoding secrets in files. Use environment variables or a secrets manager. A leaked cloud key in git history is a real incident, not a hypothetical.
  • Applying without reading the plan. The plan is the whole point. A destroy line buried in a long diff is how production databases disappear.
  • One giant file for everything. Split by concern: networking in one file, servers in another, DNS in a third. Terraform reads every .tf file in a directory as one configuration, so splitting costs nothing.
  • Skipping version pinning. Pin provider versions with ~> ranges. An unpinned provider can upgrade under you and change behavior between runs.
  • Managing everything with Terraform. Not every resource belongs in code. Short-lived test servers and one-off experiments are often faster to create by hand, or with a simpler tool. A manual deploy to a VPS teaches you what the abstraction is doing, and Docker Compose covers local multi-container setups that do not need a cloud provider at all.

Where to Go Next

Start with one real resource and keep it running for a week. A DNS record or a single server is enough. Practice the loop: change a tag, plan, apply, see the diff. Then move state to remote storage, split your files by concern, and add a second resource that depends on the first, like a firewall rule attached to your server. The concepts stop feeling abstract once you have destroyed and recreated the same server three times from the same files.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.