Welcome to this hands-on lab on defining and using variables in Terraform (OpenTofu). By the end of this tutorial, you’ll understand how to declare different variable types, fetch elements from maps and lists, and refactor resources to leverage variables for cleaner, reusable configurations.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
1. Variable Type Quiz
Question: Which of the following is not a valid Terraform variable type?- object
- item
- tuple
- list
- map
item is not a valid variable type in Terraform/OpenTofu.
| Variable Type | Description |
|---|---|
| object | Collection of named attributes with specific types |
| tuple | Ordered list of elements with potentially different types |
| list | Ordered homogeneous collection |
| map | Unordered key–value pairs with homogeneous values |
2. Data Type Quiz
Question: Which of the following is not a valid data type in OpenTofu?- list
- tuple
- map
- array
- set
array is not a valid data type in OpenTofu.
Setup: Navigate to the Variables Directory
3. Inspecting number
Open variables.tf and locate:
number belong to?Answer: It’s a boolean.
4. Fetching a Map Value
In variables.tf, you have:"slow" entry?
You can also use the dot notation if your key is a valid identifier, e.g.,
var.hard_drive.slow.5. List Indexing
Definition in variables.tf:- Lists are zero-indexed.
"Male"→ index 0"Female"→ index 1
"Female" is 1.
6. Identifying a Set Definition Error
- Type:
set(string) - Mistake: Sets must not contain duplicates;
"jerry"appears twice.
Defining duplicate elements in a
set will cause validation errors. Ensure each member is unique.7. Inspecting a Resource Block
In main.tf, you added:content argument?Answer:
"phanius"
8. Refactoring to Use Variables
-
Declare a map variable in variables.tf:
-
Update the resource in main.tf:
9. Initialize, Plan, and Apply
That’s it for this lab. Thank you for following along!