🧠 The Concept: Input vs. Output
In the 004 exam, HashiCorp expects you to know how data flows through a Terraform configuration.
- Input Variables: Like function arguments. They allow users to customize the deployment without touching the main code.
- Output Values: Like return values. They highlight important information (like an IP address) after an
apply.
📋 The Challenge
The goal is to refactor our “Random Pet” lab. Instead of hardcoding the length of the pet name, we will use a variable.
Requirements:
- Define a variable for
prefix. - Define a variable for
separator. - Output the final generated name to the terminal.
🚀 The Solution
View the full code here: Challenge 03 Repo
1. Defining the Input
Instead of hardcoding the prefix “Mrs”, let’s create a variable. This allows other team members to use your code without editing the logic.
| |
2. Use the variable
| |
3. The Output Anatomy
When you run terraform plan then you will see:
| |
Why does it look like that? You likely used an output that exported the entire resource object. While useful for debugging, it’s “noisy.” In the output above:
tomap(null): This is Terraform’s way of saying, “This attribute is a Map type, but it’s currently empty.”
/* of string */: This is a hint that if you did provide values, they would need to be strings.
4. Refining the Output
When you run terraform apply then you will see:
| |
🧠 Knowledge Check
Test your understanding of Terraform State and Outputs before moving to the next lesson.
⚠️ Quiz data missing.
📚 004 Exam: Official Documentation Reference
To master the variables and outputs section of the Terraform Associate (004) exam, I utilized the following official resources:
1. Variables & Types
- Input Variables Overview: Covers how to define variables, use default values, and set descriptions.
- Type Constraints: Crucial for 004. Explains the difference between primitive types (
string,number,bool) and complex types (list,map,object).
2. The Data Flow
- Output Values: Documentation on how to expose information about your infrastructure to the command line or other configurations.
- Variable Precedence: A favorite 004 exam topic. This page explains which value “wins” when a variable is defined in multiple places (e.g.,
.tfvarsvs environment variables).
3. CLI Interaction
- Command: output: How to extract specific values from your state file after a successful apply.