Challenge - 5 Problems
File Functions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ service_behavior
intermediate1:30remaining
Output of file() function reading a text file
Given a file named message.txt containing the text Hello, Terraform!, what will be the output of the following Terraform expression?
file("message.txt")Attempts:
2 left
💡 Hint
The file() function reads the entire content of a file as a string.
✗ Incorrect
The file() function returns the exact content of the file as a string. Since the file contains 'Hello, Terraform!', that string is returned.
❓ Configuration
intermediate1:30remaining
Using templatefile() with variables
Consider a template file greeting.tmpl with content:
Which Terraform expression correctly renders this template with the variable
Hello, ${name}!Which Terraform expression correctly renders this template with the variable
name set to "Alice"?Attempts:
2 left
💡 Hint
templatefile() expects a map of variable names to values.
✗ Incorrect
templatefile() requires the second argument to be a map with keys matching template variables. Option A correctly provides a map with key 'name' and value 'Alice'.
🧠 Conceptual
advanced1:30remaining
Behavior of file() with a missing file
What happens when Terraform runs the expression
file("missing.txt") but the file missing.txt does not exist in the working directory?Attempts:
2 left
💡 Hint
Terraform expects the file to exist when using file().
✗ Incorrect
If the file does not exist, Terraform raises an error and stops because it cannot read the file content.
❓ security
advanced2:00remaining
Security risk of using templatefile() with untrusted input
Why is it risky to use
templatefile() with variables that come directly from user input without validation?Attempts:
2 left
💡 Hint
Untrusted input can alter the output in unexpected ways.
✗ Incorrect
Using unvalidated user input in templates can inject harmful content, causing security vulnerabilities in generated infrastructure code.
❓ Architecture
expert2:30remaining
Choosing between file() and templatefile() for configuration generation
You need to generate multiple configuration files with dynamic values for a large infrastructure project. Which approach is best to maintain clarity, reusability, and ease of updates?
Attempts:
2 left
💡 Hint
Think about maintainability and automation within Terraform.
✗ Incorrect
templatefile() allows dynamic generation of files with variables, making it easier to maintain and update configurations compared to manual replacements or external scripts.