Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
HashiCorp · Terraform 004

Variables, outputs, and complex types

Terraform 004 · Lesson 7 of 18 · 50:58

YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗

Learning with these videos? Subscribe for new courses and exam-style questions.

In this lesson

Prepare for the HashiCorp Terraform Associate (004) exam. In this lesson: input variables, the ways to set their values and the order of precedence, terraform.tfvars and .auto.tfvars files, outputs and local values, and complex types such as list, set, map, object, and tuple. Includes three original practice questions, with Bluefin Logistics, a fictional parcel delivery company, as the example.

Study notes

The text of this lesson, to read at your own pace. Information as of November 2026.

Same code, two environments

Bluefin deploys the same tracking stack to test and to production, with a different Region and server size in each. Values written into the code would mean two copies to maintain. This lesson covers input variables, how to set them and which value wins, outputs and local values, and the complex types that describe richer data.

Input variables

An input variable is a parameter of a module, declared with a variable block, and used elsewhere as var dot its name. Its arguments include a type, a default value, a description, and sensitive, which hides the value in the output of Terraform. Nullable, set to false, makes sure that the value is never null, and validation and ephemeral get their own lessons later. A variable with a default is optional, and a variable without a default must get a value. If no value is set anywhere, Terraform asks for it when you run plan or apply in a terminal, and stops with an error when input is disabled, as in a pipeline.

Setting values and precedence

You can set variable values in several ways: the var option on the command line, a variable file passed with the var file option, or files that Terraform loads on its own. It loads terraform dot T F vars, terraform dot T F vars dot JSON, and every file ending in dot auto dot T F vars, in the working directory. Environment variables also work, named T F underscore VAR underscore, then the name of the variable. When a value is set in several places, precedence decides, and the last one loaded wins: the default first, then environment variables, then terraform dot T F vars, its JSON version, the auto files in lexical order, and finally the command line options, in the order given. Variables set in an HCP Terraform workspace have the same precedence as the command line.

Outputs and local values

An output block exposes a value from a module, like the address of a load balancer, with a value argument and an optional description. Terraform prints the outputs of the root module after an apply, and terraform output shows them again later. A child module returns values to its parent only through outputs, which the parent uses as module dot name dot output. Sensitive outputs are hidden in the output of plan and apply, but they are still stored in plain text in the state, and the JSON and raw options of terraform output show them. And a locals block names an expression inside a module, used as local dot its name, for values that callers must not set.

Complex types

Besides the primitive types, string, number, and bool, Terraform has complex types. Collection types hold values of one type: a list is ordered, by index from zero, a map has string keys, and a set holds unique values, with no order. Structural types mix types: an object has named attributes, each with its own type, and a tuple has positions, each with its own type. Inside an object type, optional marks an attribute that callers may leave out, and the keyword any accepts any type. And you access them with an index for a list, a key in square brackets for a map, and a dot for an object attribute.

Recap

Let's recap. A variable block declares an input, with a type, a default, and other arguments, and Terraform asks for a required value that is missing. Values come from the command line, variable files, the files loaded automatically, and environment variables, and the last one loaded wins. Outputs return values from a module, sensitive ones are hidden but stay in the state, and locals name expressions inside a module. And complex types are collections, list, map, and set, and structural types, object and tuple.

Quick checks for this lesson

The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.

  1. The variable instance_size has no default, and no value is set for it anywhere. What happens when an engineer runs terraform plan in a terminal?

    • It uses an empty string as the value
    • The plan stops with a syntax error
    • Terraform prompts you for the value
    • It skips the resources that use it
    Show the answer

    Terraform prompts you for the value

    When it runs interactively, Terraform prompts for every required variable that has no value. “It uses an empty string as the value” is wrong, because Terraform never invents a value. “The plan stops with a syntax error” is wrong, because a missing value is not a syntax error. And “It skips the resources that use it” is wrong, because Terraform never skips resources on its own.

  2. region has a default of eu-west-1, terraform.tfvars sets eu-west-2, and TF_VAR_region is eu-central-1. With nothing else set, which value does plan use?

    • eu-west-2, from terraform.tfvars
    • eu-central-1, from TF_VAR_region
    • eu-west-1, the default value
    • Terraform asks which one to use
    Show the answer

    eu-west-2, from terraform.tfvars

    Terraform dot T F vars takes precedence over environment variables, which only beat the default. “eu-central-1, from TF_VAR_region” is wrong, because environment variables come just after the default in precedence. “eu-west-1, the default value” is wrong, because the default applies only when nothing else sets a value. And “Terraform asks which one to use” is wrong, because Terraform never asks when a value is set: later sources simply override earlier ones.

  3. A variable must describe one warehouse: a name, a number of loading docks, and an active flag, each named and with its own type. Which type fits?

    • A tuple of a string, a number, and a bool
    • A map of strings, one per setting
    • A list of any type of values
    • An object with three typed attributes
    Show the answer

    An object with three typed attributes

    An object has named attributes, each with its own type, here a string, a number, and a bool. “A tuple of a string, a number, and a bool” has the right types, but positions instead of names. “A map of strings, one per setting” is wrong, because every value of a map has the same type. And “A list of any type of values” is wrong, because a list has no names, and its elements still share one type.