Skip to content

004 · Domain 4: Terraform configuration · 13% of the exam

Task 4.3: Use variables and outputs

Input variables with types, defaults, validation and every way to set them, the precedence order, locals for repeated expressions, and outputs for the CLI, other configurations and parent modules.

Study it

  • Input variables, precedence, locals and outputs

    Lesson coming

Sample questions

Try each one before opening the answer. Every option is explained, with the HashiCorp documentation page that proves it.

Question 1 · choose 1

A pipeline exports TF_VAR_region=us-east-2, and the working directory also has a terraform.tfvars file with region = "us-west-2". No -var options are passed. Which value does Terraform use?

  1. Aus-east-2, because environment variables take precedence over files
  2. Bus-west-2, since terraform.tfvars outranks environment variables
  3. CNeither; Terraform stops with an error about two conflicting values
  4. Dus-east-2, because the variable is read from the shell before any file
Show the answer and why
  • Aus-east-2, because environment variables take precedence over files

    Incorrect

    Environment variables are near the bottom of the order, above only the default.

  • Bus-west-2, since terraform.tfvars outranks environment variables

    Correct

    In the precedence order, terraform.tfvars is above environment variables, which are only above the variable's default.

  • CNeither; Terraform stops with an error about two conflicting values

    Incorrect

    Multiple sources are allowed; the precedence order resolves them.

  • Dus-east-2, because the variable is read from the shell before any file

    Incorrect

    Load order does not decide; the precedence order does.

Environment variables sit near the bottom of the precedence order, above only the variable's default.

Question 2 · choose 1

A pipeline must set the root module variable db_name without writing any file. Which environment variable does Terraform read?

  1. ATF_VAR_db_name
  2. BTERRAFORM_DB_NAME
  3. CTF_CLI_ARGS_db_name
  4. DVAR_db_name
Show the answer and why
  • ATF_VAR_db_name

    Correct

    Terraform reads environment variables named TF_VAR_ followed by the variable name.

  • BTERRAFORM_DB_NAME

    Incorrect

    Terraform does not read that naming pattern.

  • CTF_CLI_ARGS_db_name

    Incorrect

    TF_CLI_ARGS variables add command-line arguments, not variable values by name.

  • DVAR_db_name

    Incorrect

    The prefix is TF_VAR_.

TF_VAR_<name> sets root module variables from the environment.

Question 3 · choose 1

A variable has no default, and no value is set by any file, flag or environment variable. What happens when terraform plan runs interactively?

  1. ATerraform uses an empty string
  2. BTerraform skips every resource that uses the variable and plans the rest
  3. CTerraform prompts for a value
  4. DTerraform reads the last value from state
Show the answer and why
  • ATerraform uses an empty string

    Incorrect

    There is no implicit empty value.

  • BTerraform skips every resource that uses the variable and plans the rest

    Incorrect

    Terraform does not skip resources for missing inputs.

  • CTerraform prompts for a value

    Correct

    If a variable has no default and no value, Terraform prompts the user to supply one before planning.

  • DTerraform reads the last value from state

    Incorrect

    Variables are not read from state.

Required variables are prompted for interactively; automation must supply them.

Question 4 · choose 1

A module author wants callers to understand what each input and output is for, both in the code and in generated documentation. What should be added?

  1. AA comment above each block, which Terraform shows in plans
  2. BA locals block that repeats each name with an explanation
  3. CA description argument on each variable and output block
  4. DA sensitive argument on each block
Show the answer and why
  • AA comment above each block, which Terraform shows in plans

    Incorrect

    Comments are ignored by Terraform.

  • BA locals block that repeats each name with an explanation

    Incorrect

    Locals are for expressions, not documentation.

  • CA description argument on each variable and output block

    Correct

    Variable and output blocks accept a description that documents their purpose.

  • DA sensitive argument on each block

    Incorrect

    sensitive hides values; it documents nothing.

description is the documented place to explain a module's interface.

Question 5 · choose 1

An engineer struggles to pass a list of subnet IDs with -var because of shell quoting. What does the documentation recommend?

  1. APass each list element as a separate -var option
  2. BChange the variable's type to string and split it later
  3. CUse PowerShell, which passes quotes to Terraform unchanged
  4. DPut the value in a .tfvars file and pass it with -var-file
Show the answer and why
  • APass each list element as a separate -var option

    Incorrect

    Each -var sets a whole variable; repeated options override each other.

  • BChange the variable's type to string and split it later

    Incorrect

    Changing types to avoid quoting makes the module harder to use.

  • CUse PowerShell, which passes quotes to Terraform unchanged

    Incorrect

    The docs specifically advise against PowerShell for this.

  • DPut the value in a .tfvars file and pass it with -var-file

    Correct

    For complex values the docs recommend variable definition files, which Terraform parses directly instead of relying on the shell.

Variable files avoid shell quoting problems entirely.

Question 6 · choose 1

A root module declares output "lb_dns" { value = aws_lb.web.dns_name }. Where can the value be seen after apply?

  1. AOnly in the cloud provider's web console, next to the load balancer
  2. BAfter apply, and with terraform output
  3. COnly inside child modules
  4. DOnly in the plan file
Show the answer and why
  • AOnly in the cloud provider's web console, next to the load balancer

    Incorrect

    Outputs are a Terraform feature shown by the CLI.

  • BAfter apply, and with terraform output

    Correct

    Root module outputs are displayed after apply and can be queried later with terraform output.

  • COnly inside child modules

    Incorrect

    This is a root module output, shown by the CLI.

  • DOnly in the plan file

    Incorrect

    Outputs are also stored in state and printed after apply.

Root outputs are how a configuration reports results to people and tools.

Practise domain 4 →Practise all domains →