Site icon JD Bots

How to Use Input Variables in OpenTofu

Card titled Input Variables in OpenTofu, showing a variables.tf file that declares shop_name as a string with the default Bookshop, and the result Welcome to the Riverside Bookshop.

How to Use Input Variables in OpenTofu.

An input variable lets you change a value in an OpenTofu® configuration without editing the configuration itself. You declare it once, use it wherever the value is needed, and set it from a file when you want something other than its default.

This guide declares a variable, uses it in a resource, and then gives it a new value.

Syntax

Syntax

variable "<NAME>" {
  description = "<TEXT>"
  type        = <TYPE>
  default     = <VALUE>
}

To read the value elsewhere in the configuration, write var.<NAME>. All of this is described in Input Variables in the OpenTofu docs.

Prerequisites

Step 1: Declare the Variable

In an empty folder, create variables.tf:

variables.tf

variable "shop_name" {
  description = "Name of the shop, used in the greeting."
  type        = string
  default     = "Bookshop"
}

Step 2: Use It in a Resource

Create main.tf. It writes a greeting to a file with the local_file resource, and the greeting includes the variable:

main.tf

terraform {
  required_providers {
    local = {
      source  = "hashicorp/local"
      version = "~> 2.9"
    }
  }
}

resource "local_file" "greeting" {
  filename = "greeting.txt"
  content  = "Welcome to the ${var.shop_name}!"
}

${var.shop_name} puts the variable’s value inside the text.

Step 3: Apply It

Download the local provider, then apply and type yes:

Terminal

tofu init
tofu apply

Output

  # local_file.greeting will be created
  + resource "local_file" "greeting" {
      + content              = "Welcome to the Bookshop!"
      + 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             = "greeting.txt"
      + id                   = (known after apply)
    }

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

Do you want to perform these actions?
  OpenTofu will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

local_file.greeting: Creating...
local_file.greeting: Creation complete after 0s [id=7acbf857459ea5fe9739305da206b2e6e1d84409]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

The plan shows the greeting with the default value, Bookshop. greeting.txt now contains Welcome to the Bookshop!.

Step 4: Give the Variable a New Value

Create terraform.tfvars in the same folder. OpenTofu reads a file with exactly this name by itself:

terraform.tfvars

shop_name = "Riverside Bookshop"

Apply again and type yes:

Terminal

tofu apply

Output (the lines that matter)

  # local_file.greeting must be replaced
-/+ resource "local_file" "greeting" {
      ~ content              = "Welcome to the Bookshop!" -> "Welcome to the Riverside Bookshop!" # forces replacement
      ~ content_base64sha256 = "jhgw8i0hFNIJYktZvSuYXcAcPxuoADS53KxJi6yFoi4=" -> (known after apply)
      ~ content_base64sha512 = "JA+bX2hXdAWPfcBBnPs8veADdtrzCP08Zc7zYQ5xvU7nrlysuumUPDCeOVFsM837r1bOic2vproVP23vino+xg==" -> (known after apply)
      ~ content_md5          = "23626f3583120d170e339fab250eca64" -> (known after apply)
      ~ content_sha1         = "7acbf857459ea5fe9739305da206b2e6e1d84409" -> (known after apply)
      ~ content_sha256       = "8e1830f22d2114d209624b59bd2b985dc01c3f1ba80034b9dcac498bac85a22e" -> (known after apply)
      ~ content_sha512       = "240f9b5f685774058f7dc0419cfb3cbde00376daf308fd3c65cef3610e71bd4ee7ae5cacbae9943c309e39516c33cdfbaf56ce89cdafa6ba153f6def8a7a3ec6" -> (known after apply)
      ~ id                   = "7acbf857459ea5fe9739305da206b2e6e1d84409" -> (known after apply)
        # (3 unchanged attributes hidden)
    }

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

The configuration didn’t change, only the value did. greeting.txt now contains Welcome to the Riverside Bookshop!.

Clean Up

Run tofu destroy and type yes to remove greeting.txt, then delete the folder.

Common Mistakes

MistakeWhat happensDo this instead
Writing shop_name instead of var.shop_name in main.tfOpenTofu doesn’t know what shop_name refers to and stops.Always read a variable as var.<NAME>.
Leaving out typeThe variable accepts any value, so a mistake shows up later, or not at all.Give every variable a type.

FAQ

Where should I put variable blocks?

Anywhere in the folder’s .tf files. Most projects keep them in variables.tf.

Can a variable’s default use another variable?

No. The docs say a default must be a literal value and can’t refer to other objects in the configuration.

Conclusion

Declare a variable with a variable block, read it with var.<NAME>, and set a new value in terraform.tfvars when the default isn’t right.

Related

Additional Resources

OpenTofu® is a registered trademark of LF Projects, LLC. Other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by the OpenTofu project.

Exit mobile version