Install & Your First Apply
Both CLIs are single static binaries written in Go: download, put on your PATH, done. This lesson installs them, then runs the whole life cycle once (init, plan, apply, a second plan, destroy) on a resource that only writes a file, so nothing can cost money.
Installing
# OpenTofu: the official installer script (also: brew install opentofu, snap, winget, .deb/.rpm repos)
curl -fsSL https://get.opentofu.org/install-opentofu.sh -o install-opentofu.sh
chmod +x install-opentofu.sh && ./install-opentofu.sh --install-method deb
# Terraform: HashiCorp's apt repository (also: brew install hashicorp/tap/terraform, a zip from releases.hashicorp.com)
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" \
| sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraformTeams that work on several projects pin the CLI version per repository with a version manager such as tenv (handles both OpenTofu and Terraform; reads .opentofu-version / .terraform-version files) or tfenv. The Toolkit lists them. Shell completion: tofu -install-autocomplete.
The first configuration
A configuration is every .tf file in one folder (the root module); file names don't matter to the CLI. By convention you start with main.tf, and later split out variables.tf, outputs.tf, versions.tf.
mkdir hello && cd hello
cat > main.tf <<'HCL'
terraform {
required_version = ">= 1.6"
required_providers {
local = {
source = "hashicorp/local"
version = "~> 2.9"
}
}
}
resource "local_file" "motd" {
filename = "${path.module}/motd.txt"
content = "Welcome to the platform team.\n"
file_permission = "0644"
}
HCL
tofu init
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/local versions matching "~> 2.9"...
- Installing hashicorp/local v2.9.1...
- Installed hashicorp/local v2.9.1 (unauthenticated)
OpenTofu has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that OpenTofu can guarantee to make the same selections by default when
you run "tofu init" in the future.
Warning: Incomplete lock file information for providers
Due to your customized provider installation methods, OpenTofu was forced to
calculate lock file checksums locally for the following providers:
- hashicorp/local
The current .terraform.lock.hcl file only includes checksums for linux_amd64,
so OpenTofu running on another platform will fail to install these providers.
To calculate additional checksums for another platform, run:
tofu providers lock -platform=linux_amd64
(where linux_amd64 is the platform to generate)
OpenTofu has been successfully initialized!
You may now begin working with OpenTofu. Try running "tofu plan" to see
any changes that are required for your infrastructure. All OpenTofu commands
should now work.
If you ever set or change modules or backend configuration for OpenTofu,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
init prepared the folder: it found the local provider (here in the sandbox's mirror; normally on the registry), downloaded it into .terraform/ and wrote .terraform.lock.hcl, which records the exact provider version and checksums. Commit the lock file; never commit .terraform/. The warning is specific to installing from a local mirror, and the Providers lesson shows the fix.
Plan, apply, plan again, destroy
mkdir hello && cd hello
cat > main.tf <<'HCL'
resource "local_file" "motd" {
filename = "${path.module}/motd.txt"
content = "Welcome to the platform team.\n"
file_permission = "0644"
}
HCL
tofu init >/dev/null
tofu plan
echo "=== apply"
tofu apply -auto-approve
cat motd.txt
echo "=== plan again"
tofu plan
echo "=== destroy"
tofu destroy -auto-approve | tail -3
ls
OpenTofu used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
+ create
OpenTofu will perform the following actions:
# local_file.motd will be created
+ resource "local_file" "motd" {
+ content = <<-EOT
Welcome to the platform team.
EOT
+ 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 = "0644"
+ filename = "./motd.txt"
+ id = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.
─────────────────────────────────────────────────────────────────────────────
Note: You didn't use the -out option to save this plan, so OpenTofu can't
guarantee to take exactly these actions if you run "tofu apply" now.
=== apply
OpenTofu used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
+ create
OpenTofu will perform the following actions:
# local_file.motd will be created
+ resource "local_file" "motd" {
+ content = <<-EOT
Welcome to the platform team.
EOT
+ 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 = "0644"
+ filename = "./motd.txt"
+ id = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.
local_file.motd: Creating...
local_file.motd: Creation complete after 0s [id=065892264f7c96edce01af3e1785d176101f75cd]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Welcome to the platform team.
=== plan again
local_file.motd: Refreshing state... [id=065892264f7c96edce01af3e1785d176101f75cd]
No changes. Your infrastructure matches the configuration.
=== destroy
local_file.motd: Destruction complete after 0s
Destroy complete! Resources: 1 destroyed.
main.tf
terraform.tfstate
terraform.tfstate.backup
Read the plan carefully, every time:
+ createis the action; the summary linePlan: 1 to add, 0 to change, 0 to destroy.is what reviewers look at first.(known after apply)marks values the provider only learns while creating the object (ids, hashes, ARNs in AWS).- The default
directory_permissionshows up although you never wrote it: providers fill in defaults.
Then apply plans again and asks for yes; -auto-approve skips the question (only for sessions like this and pipelines that apply a reviewed plan file). The second plan says No changes: the tool is idempotent, and that line is how you know reality matches the code. destroy is a plan in which everything is deleted.
apply also wrote terraform.tfstate, the state file; destroy left it in place with no resources in it, plus terraform.tfstate.backup, the previous version. You will meet it properly in What State Is.
Files in a working folder
| File | Commit? | What it is |
|---|---|---|
*.tf, *.tftest.hcl |
yes | your code and tests |
.terraform.lock.hcl |
yes | pinned provider versions and checksums |
.terraform/ |
no | downloaded providers and modules (a cache) |
terraform.tfstate, *.tfstate.backup |
never | state; may contain secrets; belongs in a remote backend |
*.tfvars |
depends | variable values; never with secrets in them |
tfplan (saved plans) |
no | binary plan files; can contain secrets |
A typical .gitignore:
.terraform/
*.tfstate
*.tfstate.*
*.tfplan
crash.log
override.tf
override.tf.json
*_override.tfTest yourself
Which file must you commit so everyone gets the same provider versions?
.terraform/.terraform.lock.hclterraform.tfstatetfplan
After a successful apply you run tofu plan again without changing anything. What should it print?
- The same plan as before
- No changes. Your infrastructure matches the configuration.
- An error: already applied
- A destroy plan
What does (known after apply) mean in a plan?
- The value is secret
- The provider learns the value only when it creates the object
- The value failed validation
- The attribute is optional