The file() function reads the contents of a file from the local filesystem and returns it as a string. It's useful for including file content directly in Terraform configurations.
Click to reveal answer
beginner
How does templatefile() differ from file() in Terraform?
templatefile() reads a file and processes it as a template, replacing placeholders with provided variables. file() just reads the file content as-is without substitutions.
Click to reveal answer
intermediate
What is the correct way to pass variables to templatefile()?
You pass a map of variable names and values as the second argument to templatefile(). The template file uses ${varname} syntax to insert these values.
Click to reveal answer
beginner
Give an example use case for file() in Terraform.
Use file() to read a script file (like a shell script) and pass its content to a resource's user_data field to run on a virtual machine at startup.
Click to reveal answer
intermediate
What happens if the file path given to file() or templatefile() does not exist?
Terraform will return an error and stop the plan or apply process because it cannot read the required file.
Click to reveal answer
What does the Terraform file() function return?
AA map of variables
BThe file content as a string
CA list of file names
DA boolean indicating file existence
✗ Incorrect
file() reads the entire file content and returns it as a string.
Which function allows variable substitution inside a file's content?
Atemplatefile()
Bfile()
Cjsondecode()
Djoin()
✗ Incorrect
templatefile() processes templates and replaces placeholders with variables.
How do you provide variables to templatefile()?
AAs a map of key-value pairs
BNo variables can be passed
CAs a single string
DAs a list of strings
✗ Incorrect
Variables are passed as a map to templatefile() for substitution.
What syntax is used inside a template file for variable placeholders?
A#varname#
B<varname>
C{{varname}}
D${varname}
✗ Incorrect
Terraform templates use ${varname} syntax for variables.
What happens if the file path in file() is incorrect?
ATerraform ignores the error
BTerraform creates an empty file
CTerraform returns an error and stops
DTerraform retries reading the file
✗ Incorrect
Terraform fails with an error if the file cannot be found or read.
Explain how file() and templatefile() functions work in Terraform and when to use each.
Think about reading plain files versus files with placeholders.
You got /4 concepts.
Describe the process of passing variables to a template file in Terraform and how they are used inside the template.
Focus on the map argument and placeholder syntax.
You got /3 concepts.
Practice
(1/5)
1. What does the Terraform function file() do?
easy
A. Writes content to a file
B. Reads a file and replaces variables inside it
C. Reads the entire content of a file as plain text
D. Deletes a file from the system
Solution
Step 1: Understand the purpose of file()
The file() function reads the content of a file and returns it as plain text without any processing.
Step 2: Compare with other functions
templatefile() replaces variables inside a file, but file() just reads raw content.
Final Answer:
Reads the entire content of a file as plain text -> Option C
Quick Check:
file() reads file content [OK]
Hint: file() just reads text, no variable replacement [OK]
Common Mistakes:
Confusing file() with templatefile()
Thinking file() modifies file content
Assuming file() writes to files
2. Which of the following is the correct syntax to use templatefile() in Terraform?
easy
A. templatefile("path/to/file.tpl", { var1 = "value1" })
B. templatefile(path = "file.tpl", vars = { var1 = "value1" })
C. templatefile("file.tpl", var1 = "value1")
D. templatefile("file.tpl")
Solution
Step 1: Recall the correct function signature
templatefile() requires two arguments: the file path as a string and a map of variables.
Step 2: Match the syntax
templatefile("path/to/file.tpl", { var1 = "value1" }) correctly passes the file path and a map with variable assignments. Other options either use wrong parameter names or omit the variables map.
Final Answer:
templatefile("path/to/file.tpl", { var1 = "value1" }) -> Option A
Quick Check:
templatefile(path, vars map) syntax [OK]
Hint: templatefile needs path string and vars map [OK]
Common Mistakes:
Using named parameters instead of positional
Passing variables outside a map
Omitting the variables map argument
3. Given the file message.tpl with content:
Hello, ${name}!
What is the output of this Terraform expression? templatefile("message.tpl", { name = "Alice" })
medium
A. Hello, Alice!
B. Hello, ${name}!
C. templatefile("message.tpl", { name = "Alice" })
But Terraform shows an error: File not found: config.tpl. What is the likely cause?
medium
A. The file config.tpl is empty
B. The variable env is missing in the map
C. templatefile() cannot be used in output blocks
D. The file config.tpl does not exist in the working directory
Solution
Step 1: Analyze the error message
The error says "File not found", which means Terraform cannot locate the file config.tpl.
Step 2: Check file existence and path
Most likely, the file is missing or the path is incorrect. Variables missing or empty files do not cause this error.
Final Answer:
The file config.tpl does not exist in the working directory -> Option D
Quick Check:
File not found error means missing file [OK]
Hint: Check file path and existence if 'File not found' error [OK]
Common Mistakes:
Assuming variable map causes file not found
Thinking templatefile() is invalid in output
Ignoring file path correctness
5. You want to create a Terraform output that reads a JSON file data.json and replaces a placeholder ${region} inside it with your variable var.region. Which approach is best?
hard
A. Use file() to read data.json and then use replace() to substitute ${region}
B. Use templatefile() with data.json and pass { region = var.region } as variables
C. Use file() only, since it automatically replaces variables
D. Use templatefile() but pass an empty map for variables
Solution
Step 1: Understand the difference between file() and templatefile()
file() reads raw content without variable substitution. templatefile() reads and replaces variables inside the file.
Step 2: Choose the correct function and usage
Since you want to replace ${region} with var.region, templatefile() with a variables map is the best choice.
Final Answer:
Use templatefile() with data.json and pass { region = var.region } as variables -> Option B
Quick Check:
templatefile() replaces placeholders with variables [OK]
Hint: Use templatefile() to replace placeholders with variables [OK]
Common Mistakes:
Expecting file() to replace variables
Passing empty variable map to templatefile()
Using replace() manually instead of templatefile()
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.