Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

tofu

OpenTofu: the open-source fork of Terraform. Same HCL code and the same workflow — tofu init, plan, apply.

Level
DevOps tool
Group
Infrastructure as Code and cloud
Needs root
No
Package
opentofu
Also called
opentofu

What it does

OpenTofu started in 2023 when Terraform changed its licence (BSL). It is run by the Linux Foundation and stays open source (MPL). Most Terraform code and providers work without changes: replace terraform with tofu.

Extra features: state encryption built in, early variable evaluation, and its own registry (registry.opentofu.org).

How to write it

tofu COMMAND [OPTIONS]

Main parts

Commands

init
Download providers and modules, set up the backend.
fmt / validate
Format code / check syntax.
plan [-out FILE]
Show the changes.
apply [FILE]
Make the changes.
destroy
Delete everything in the state.
output [NAME]
Show outputs.
state list|show|mv|rm
Work with the state.
import ADDR ID
Take an existing resource into the state.
workspace list|new|select
Several states for one config.
test
Run .tftest.hcl tests.
providers lock
Write provider checksums for all platforms.

Options 9

Most used

Options

-var 'k=v' / -var-file FILE
Input variables.
-out FILE
Save the plan (apply exactly this plan).
-auto-approve
Apply without a question (CI only).
-target ADDR
Only this resource (for emergencies).
-refresh-only
Only read the real world (find drift).
-upgrade
Upgrade providers in init.
-lock-timeout 60s
Wait for the state lock.
-json / -no-color
Machine output / no colours.
-chdir DIR
Run in another folder.

Examples 11

$ run as a normal user# run as root (or with sudo)

  1. 1The normal workflow

    tofu init
    tofu fmt -recursive && tofu validate
    tofu plan -out tfplan
    tofu apply tfplan
  2. 2Move a Terraform project to OpenTofu

    terraform version
    tofu init -upgrade
    tofu plan

    The plan should show no changes. Make a backup of the state first.

  3. 3Encrypt the state with a key from an environment variable

    cat encryption.tf
    terraform {
      encryption {
        key_provider "pbkdf2" "main" { passphrase = var.state_passphrase }
        method "aes_gcm" "main" { keys = key_provider.pbkdf2.main }
        state { method = method.aes_gcm.main }
      }
    }
  4. 4Variables per environment

    tofu plan -var-file envs/prod.tfvars -out tfplan
  5. 5Find drift

    tofu plan -refresh-only
  6. 6Read an output in a script

    tofu output -raw lb_ip
  7. 7Import an existing resource

    tofu import azurerm_resource_group.main /subscriptions/SUB/resourceGroups/rg-shop
  8. 8List and move state

    tofu state list
    tofu state mv azurerm_storage_account.old azurerm_storage_account.logs
  9. 9Run tests

    tofu test
  10. 10Lock providers for Linux and Mac

    tofu providers lock -platform=linux_amd64 -platform=darwin_arm64
  11. 11In CI

    tofu init -input=false
    tofu plan -input=false -no-color -out tfplan

Watch out

  • Do not run terraform and tofu on the same state at the same time; choose one tool per project.
  • New Terraform-only features (after 1.6) may not exist in OpenTofu, and the other way.

Files

*.tf / *.tofu
The code (.tofu files are read only by OpenTofu).
.terraform.lock.hcl
Provider versions and checksums.

Tips to remember

  • Learn with Terraform docs (more examples); the commands are the same.

Exit codes

0OK (plan: no changes).
1Error.
2plan -detailed-exitcode: there are changes.