Terraform Tutorial · Terraform Basics · Lesson 2 of 25

Terraform Get Started

Install Terraform, learn the init → plan → apply → destroy workflow, and understand the files Terraform creates.

Install Terraform

  • macOS: brew tap hashicorp/tap && brew install hashicorp/tap/terraform
  • Windows: winget install HashiCorp.Terraform or choco install terraform
  • Linux: use HashiCorp's apt/yum repository, or download the binary from developer.hashicorp.com/terraform/install.

Check it works:

terraform version

The core workflow

mkdir hello-terraform && cd hello-terraform
# create main.tf (see below)
terraform init      # download providers, prepare the directory
terraform plan      # preview the changes
terraform apply     # make the changes (asks for confirmation)
terraform destroy   # delete everything this configuration created
What terraform plan shows (a local file, no cloud account needed)
terraform {
  required_providers {
    local = {
      source  = "hashicorp/local"
      version = "~> 2.5"
    }
  }
}

resource "local_file" "hello" {
  filename = "${path.module}/hello.txt"
  content  = "Hello, Terraform!"
}

Output

Terraform will perform the following actions:

  # local_file.hello will be created
  + resource "local_file" "hello" {
      + content              = "Hello, Terraform!"
      + content_base64sha256 = (known after apply)
      + content_base64sha512 = (known after apply)
      + content_md5          = (known after apply)
      + content_sha1         = (known after apply)
      + content_sha256       = (known after apply)
      + content_sha512       = (known after apply)
      + directory_permission = "0777"
      + file_permission      = "0777"
      + filename             = "./hello.txt"
      + id                   = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Run this on your computer — see “Get Started” for setup.

Files you will see

File / folder What it is Commit to Git?
*.tf Your configuration Yes
.terraform.lock.hcl Exact provider versions and checksums Yes
.terraform/ Downloaded providers and modules No
terraform.tfstate The state: what Terraform manages No (store it in a remote backend)
*.tfvars Variable values Only if they contain no secrets

A minimal .gitignore:

.terraform/
*.tfstate
*.tfstate.*

Useful everyday commands

  • terraform fmt formats your files in the standard style.
  • terraform validate checks the configuration for errors.
  • terraform output prints outputs from the state.
  • terraform console lets you try expressions interactively.

Test yourself

Which file must you commit so everyone uses the same provider versions?

  1. .terraform/
  2. terraform.tfstate
  3. .terraform.lock.hcl
  4. main.tfplan
Show answer

Answer: .terraform.lock.hcl