Bird
Raised Fist0
Terraformcloud~10 mins

Output declaration syntax in Terraform - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - Output declaration syntax
Start Terraform Config
↓
Declare output block
↓
Assign value expression
↓
Terraform processes output
↓
Outputs available after apply
↓
Use output value elsewhere or display
Terraform reads the output block, evaluates the value expression, and makes the output available after applying the configuration.
Execution Sample
Terraform
output "instance_ip" {
  value = aws_instance.web.public_ip
}
Declares an output named 'instance_ip' that shows the public IP of the AWS instance named 'web'.
Process Table
StepActionEvaluationResult
1Terraform reads output blockoutput "instance_ip" with value expressionOutput block registered
2Evaluate value expressionaws_instance.web.public_ipGets public IP address, e.g., "54.12.34.56"
3Store output valueinstance_ip = "54.12.34.56"Output ready for display/use
4After terraform applyDisplay outputOutputs: instance_ip = 54.12.34.56
5Use output in other modules or scriptsReference output by nameValue "54.12.34.56" accessible
💡 Output value is available after terraform apply completes successfully
Status Tracker
VariableStartAfter Step 2After Step 3Final
instance_ipundefined"54.12.34.56""54.12.34.56""54.12.34.56"
Key Moments - 3 Insights
Why do we need to declare outputs in Terraform?
Outputs let Terraform show or share important values after applying changes, as seen in step 4 where the output is displayed.
Can the output value be any expression?
Yes, but it must be a valid Terraform expression that can be evaluated, like a resource attribute, shown in step 2.
When are output values available?
Only after 'terraform apply' finishes successfully, as shown in step 4 where outputs are displayed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output value of 'instance_ip' after step 3?
Aaws_instance.web.public_ip
Bundefined
C"54.12.34.56"
Dnull
💡 Hint
Check the 'Result' column in row for step 3 in the execution_table.
At which step does Terraform make the output value available for use?
AStep 3
BStep 4
CStep 2
DStep 1
💡 Hint
Look for when outputs are displayed after apply in the execution_table.
If the value expression was invalid, what would happen during execution?
ATerraform would error during evaluation at step 2
BTerraform would still display the output value
COutput would be empty string
DOutput would be null
💡 Hint
Refer to step 2 where evaluation happens; invalid expressions cause errors here.
Concept Snapshot
Terraform output declaration syntax:
output "name" {
  value = expression
}
Outputs show values after apply.
Value must be valid expression.
Outputs help share info between configs or users.
Full Transcript
Terraform output declaration defines a named output block with a value expression. Terraform reads this block during configuration processing. It evaluates the value expression, typically referencing resource attributes. After 'terraform apply' runs successfully, Terraform displays the output values. These outputs can be used by other modules or scripts. Outputs must be valid expressions and are only available after apply completes.

Practice

(1/5)
1. What is the purpose of an output block in a Terraform configuration?
easy
A. To display useful information after Terraform applies changes
B. To define variables for input values
C. To create resources in the cloud
D. To delete resources automatically

Solution

  1. Step 1: Understand Terraform output blocks

    Output blocks are used to show important information after Terraform finishes applying infrastructure changes.
  2. Step 2: Differentiate from other blocks

    Variables define inputs, resources create cloud items, and outputs only display results.
  3. Final Answer:

    To display useful information after Terraform applies changes -> Option A
  4. Quick Check:

    Output block = display info [OK]
Hint: Outputs show info after apply, not inputs or resource creation [OK]
Common Mistakes:
  • Confusing outputs with variables
  • Thinking outputs create resources
  • Assuming outputs delete resources
2. Which of the following is the correct syntax to declare an output named instance_ip with value aws_instance.web.private_ip?
easy
A. output "instance_ip" { value = aws_instance.web.private_ip }
B. output instance_ip = aws_instance.web.private_ip
C. output "instance_ip" : aws_instance.web.private_ip
D. output instance_ip { value: aws_instance.web.private_ip }

Solution

  1. Step 1: Recall Terraform output block syntax

    The correct syntax uses the keyword output, a quoted name, and a block with value assignment using =.
  2. Step 2: Check each option

    output "instance_ip" { value = aws_instance.web.private_ip } matches the correct syntax exactly. Others use invalid assignment or missing quotes.
  3. Final Answer:

    output "instance_ip" { value = aws_instance.web.private_ip } -> Option A
  4. Quick Check:

    Output syntax = output "name" { value = ... } [OK]
Hint: Output name must be quoted and value assigned with = inside braces [OK]
Common Mistakes:
  • Omitting quotes around output name
  • Using = outside braces
  • Using colon instead of =
3. Given this output block:
output "db_password" {
  value     = var.db_password
  sensitive = true
}

What will Terraform do when you run terraform apply?
medium
A. Cause a syntax error because sensitive is not allowed
B. Show the db_password value in the output after apply
C. Hide the db_password value in the output to keep it secret
D. Ignore the output block completely

Solution

  1. Step 1: Understand the sensitive attribute

    Setting sensitive = true tells Terraform to hide the output value from the terminal to protect secrets.
  2. Step 2: Predict Terraform behavior on apply

    Terraform will not display the actual value but will note that a sensitive output exists.
  3. Final Answer:

    Hide the db_password value in the output to keep it secret -> Option C
  4. Quick Check:

    sensitive = true hides output value [OK]
Hint: Sensitive outputs hide values after apply to protect secrets [OK]
Common Mistakes:
  • Expecting sensitive outputs to show values
  • Thinking sensitive causes errors
  • Assuming outputs are ignored
4. Identify the error in this output block:
output "server_port" {
  value = 8080
  sensitive = "false"
}
medium
A. The value 8080 must be a string, not a number
B. The sensitive attribute should be a boolean, not a string
C. Output names cannot be quoted
D. Missing a comma after the value line

Solution

  1. Step 1: Check attribute types in output block

    The sensitive attribute expects a boolean (true or false), not a string in quotes.
  2. Step 2: Validate other parts

    Value can be a number without quotes, output names must be quoted, and commas are not used between lines in HCL.
  3. Final Answer:

    The sensitive attribute should be a boolean, not a string -> Option B
  4. Quick Check:

    sensitive = true/false boolean, not "false" string [OK]
Hint: Boolean attributes must not be quoted strings [OK]
Common Mistakes:
  • Using quotes around boolean values
  • Thinking output names are unquoted
  • Adding commas between lines
5. You want to output a list of public IPs from multiple AWS instances stored in aws_instance.web list. Which output block correctly returns their public IPs?
hard
A. output "public_ips" { value = for i in aws_instance.web { i.public_ip } }
B. output "public_ips" { value = aws_instance.web.public_ip }
C. output "public_ips" { value = aws_instance.web.public_ip[*] }
D. output "public_ips" { value = [for i in aws_instance.web : i.public_ip] }

Solution

  1. Step 1: Understand how to extract values from a list of resources

    Terraform uses for-expressions inside output value to map over lists and extract attributes.
  2. Step 2: Evaluate each option

    output "public_ips" { value = [for i in aws_instance.web : i.public_ip] } uses correct for-expression syntax. output "public_ips" { value = aws_instance.web.public_ip } tries direct attribute access on list (invalid). output "public_ips" { value = aws_instance.web.public_ip[*] } uses incorrect splat syntax. output "public_ips" { value = for i in aws_instance.web { i.public_ip } } has invalid block syntax.
  3. Final Answer:

    output "public_ips" { value = [for i in aws_instance.web : i.public_ip] } -> Option D
  4. Quick Check:

    Use for-expressions to map list attributes [OK]
Hint: Use for-expressions with [for x in list : x.attr] to extract lists [OK]
Common Mistakes:
  • Trying to access attribute directly on list
  • Using invalid syntax for list comprehension
  • Confusing for-expression with block syntax