0
0
Terraformcloud~5 mins

File functions (file, templatefile) in Terraform - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the Terraform file() function do?
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
Which function allows variable substitution inside a file's content?
Atemplatefile()
Bfile()
Cjsondecode()
Djoin()
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
What syntax is used inside a template file for variable placeholders?
A#varname#
B<varname>
C{{varname}}
D${varname}
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
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.