0
0
Terraformcloud~20 mins

File functions (file, templatefile) in Terraform - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Functions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
service_behavior
intermediate
1: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")
Anull
B"Hello, Terraform!"
CSyntaxError
D"file(message.txt)"
Attempts:
2 left
💡 Hint
The file() function reads the entire content of a file as a string.
Configuration
intermediate
1:30remaining
Using templatefile() with variables
Consider a template file greeting.tmpl with content:
Hello, ${name}!

Which Terraform expression correctly renders this template with the variable name set to "Alice"?
Atemplatefile("greeting.tmpl", { name = "Alice" })
Btemplatefile("greeting.tmpl", ["name", "Alice"])
Ctemplatefile("greeting.tmpl", name = "Alice")
Dtemplatefile("greeting.tmpl", { "name": "Alice" })
Attempts:
2 left
💡 Hint
templatefile() expects a map of variable names to values.
🧠 Conceptual
advanced
1: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?
ATerraform returns an empty string ""
BTerraform creates an empty file named missing.txt
CTerraform raises a runtime error and stops execution
DTerraform returns null
Attempts:
2 left
💡 Hint
Terraform expects the file to exist when using file().
security
advanced
2: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?
AIt can lead to injection of malicious code into generated configuration files
BIt can cause Terraform to crash due to syntax errors in the template
CIt will automatically sanitize the input, so no risk exists
DIt causes Terraform to ignore the variables and use defaults
Attempts:
2 left
💡 Hint
Untrusted input can alter the output in unexpected ways.
Architecture
expert
2: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?
AEmbed all configuration content directly in Terraform variables as strings
BUse file() to read static files and manually replace variables in Terraform code
CUse file() to read templates and parse them with external scripts outside Terraform
DUse templatefile() with parameterized templates to generate files dynamically
Attempts:
2 left
💡 Hint
Think about maintainability and automation within Terraform.