Learn Infrastructure as Code from zero. Build, change, and version your cloud infrastructure safely and efficiently.
Go from zero to deploying your first resource in minutes.
Terraform is an Infrastructure as Code (IaC) tool made by HashiCorp. Instead of clicking around in a cloud dashboard to create servers, databases, and networks, you write simple configuration files that describe what you want. Terraform then figures out how to make it happen.
Think of it like a recipe: you write down the ingredients and steps (your .tf files), and Terraform is the chef that follows the recipe to build your infrastructure. Change the recipe? Terraform updates only what changed. Delete the recipe? Terraform tears everything down.
If you have Homebrew installed (and you should!), installing Terraform takes one command:
Verify the install:
terraform from any terminal window. The -version flag confirms it is installed and shows which version you have.
We'll build this in three small moves: make a project folder, create an empty config file, then paste your first Terraform code into it. We'll use the local_file resource — it creates a file on your own computer, so no cloud account needed.
1. Create a project folder. mkdir makes the directory and cd moves you into it so every command you run after this happens inside the project.
2. Create an empty main.tf file. Terraform reads every .tf file in the current folder, and main.tf is the conventional name for the primary config. touch creates the file if it doesn't exist yet.
3. Open main.tf in your editor and paste the code below. Use the Copy button on the top right of the block, then paste the whole thing into the empty file and save. Don't worry about the syntax yet — we'll unpack it right after.
terraform { required_providers { local = { source = "hashicorp/local" version = "~> 2.0" } } } resource "local_file" "hello" { content = "Hello, Terraform!" filename = "${path.module}/hello.txt" }
local provider, version 2.x. When you run terraform init in the next step, this is what drives the download."local_file" is the resource type (from the local provider), and "hello" is the local name you give this particular instance — you'd use it to reference the resource elsewhere in your config. Inside the braces, you set what goes in the file (content) and where it gets written (filename).
Every Terraform project follows this cycle. Let's walk through each step.
Initialize the project. Downloads the provider plugins you declared.
.tf files, downloads the required providers into a .terraform/ directory, and creates a .terraform.lock.hcl lock file (like package-lock.json in Node). You only need to re-run this when you add or change providers.
Preview what Terraform will do — without actually doing it.
Execute the plan. Terraform will ask you to confirm with yes.
hello.txt file with "Hello, Terraform!" inside it. You just created your first resource with Terraform!
Tear down everything Terraform created. It will ask for confirmation.
hello.txt file is gone. In real cloud projects, this would delete servers, databases, etc. — use with caution!
After running terraform apply, Terraform creates a file called terraform.tfstate. This is Terraform's memory — it tracks what resources exist and their current configuration.
Key things about state:
*.tfstate and *.tfstate.backup to your .gitignore. State files often contain secrets like passwords and API keys in plain text.
*.tfstate *.tfstate.* .terraform/ .terraform.lock.hcl
terraform plan, it compares your .tf files against the state to figure out what changed. Think of it as Terraform's "source of truth" about the real world.
Every command and syntax pattern you need, organized by category.
Providers are plugins that let Terraform talk to APIs — clouds, SaaS tools, and more.
A provider is a Terraform plugin that knows how to talk to a specific API. Want to create an AWS EC2 instance? You need the AWS provider. Want to manage a GitHub repo? You need the GitHub provider. Each provider adds resource types and data sources you can use in your config.
Providers are declared in your terraform { required_providers { } } block and downloaded automatically when you run terraform init. Most are maintained by HashiCorp or the service vendor and are published to the Terraform Registry.
hashicorp/awshashicorp/azurermhashicorp/googlehashicorp/kuberneteskreuzwerker/dockerintegrations/githubcloudflare/cloudflareDataDog/datadoghashicorp/helmhashicorp/vault