Bird
Raised Fist0
Terraformcloud~10 mins

File functions (file, templatefile) in Terraform - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - File functions (file, templatefile)
Start Terraform config
↓
Call file() function
↓
Read file content as string
↓
Call templatefile() function
↓
Read template file
↓
Replace placeholders with variables
↓
Return final string
↓
Use string in resource or output
Terraform reads a file as a string with file(), or reads a template file and replaces variables with templatefile(), then uses the result in configuration.
Execution Sample
Terraform
locals {
  content = file("example.txt")
  message = templatefile("greeting.tpl", { name = "Alice" })
}
Reads a text file into 'content' and processes a template file replacing 'name' with 'Alice' into 'message'.
Process Table
StepFunction CalledInputActionOutput
1file()"example.txt"Reads entire file content as string"Hello from example.txt\n"
2templatefile()"greeting.tpl", {name="Alice"}Reads template and replaces ${name} with 'Alice'"Hello, Alice! Welcome!\n"
3Use outputslocals.content, locals.messageAssigns strings to variables for later useVariables hold file content and rendered template
💡 All file reads complete, variables hold final string values for use in Terraform
Status Tracker
VariableStartAfter file()After templatefile()Final
locals.content"""Hello from example.txt\n""Hello from example.txt\n""Hello from example.txt\n"
locals.message"""""Hello, Alice! Welcome!\n""Hello, Alice! Welcome!\n"
Key Moments - 3 Insights
Why does file() return the whole file content as one string?
Because file() reads the entire file as plain text without processing, as shown in step 1 of the execution_table.
How does templatefile() replace variables inside the template?
templatefile() reads the template and substitutes placeholders like ${name} with provided values, as shown in step 2 of the execution_table.
Can file() process variables inside the file content?
No, file() only reads raw text. To process variables, use templatefile(), as shown by the difference between steps 1 and 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of file() at step 1?
A"Hello, Alice! Welcome!\n"
B"Hello from example.txt\n"
C"${name}"
D""
💡 Hint
Check the Output column in row with Step 1 in execution_table
At which step does templatefile() replace variables in the template?
AStep 2
BStep 1
CStep 3
DNo replacement occurs
💡 Hint
Look at the Function Called and Action columns in execution_table
If you want to read a file without variable substitution, which function should you use?
Atemplatefile()
Bjsondecode()
Cfile()
Dterraform()
💡 Hint
Refer to key_moments where file() reads raw text without processing
Concept Snapshot
file(path) reads entire file content as a string
templatefile(path, vars) reads a template file and replaces placeholders with vars
Use file() for raw text, templatefile() for variable substitution
Both return strings usable in Terraform configs
Files must exist and be readable for success
Full Transcript
This visual execution shows how Terraform's file() function reads a file's full content as a string, while templatefile() reads a template file and replaces variables inside it. First, file() reads example.txt and returns its text. Then, templatefile() reads greeting.tpl and replaces the placeholder ${name} with 'Alice'. The results are stored in local variables for use in Terraform resources or outputs. file() returns raw text without changes, while templatefile() processes variables. This helps beginners understand when to use each function and how Terraform handles file content.

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

  1. 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.
  2. Step 2: Compare with other functions

    templatefile() replaces variables inside a file, but file() just reads raw content.
  3. Final Answer:

    Reads the entire content of a file as plain text -> Option C
  4. 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

  1. Step 1: Recall the correct function signature

    templatefile() requires two arguments: the file path as a string and a map of variables.
  2. 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.
  3. Final Answer:

    templatefile("path/to/file.tpl", { var1 = "value1" }) -> Option A
  4. 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" })
D. Error: variable 'name' not found

Solution

  1. Step 1: Understand templatefile variable replacement

    The templatefile() function reads the file and replaces variables like ${name} with the provided value.
  2. Step 2: Apply the variable substitution

    Since name is set to "Alice", the output becomes "Hello, Alice!".
  3. Final Answer:

    Hello, Alice! -> Option A
  4. Quick Check:

    templatefile replaces variables [OK]
Hint: templatefile replaces ${var} with values from map [OK]
Common Mistakes:
  • Expecting raw template text output
  • Confusing file() with templatefile() output
  • Missing variable in map causing error
4. You wrote this Terraform code:
output "config" {
  value = templatefile("config.tpl", { env = "prod" })
}

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

  1. Step 1: Analyze the error message

    The error says "File not found", which means Terraform cannot locate the file config.tpl.
  2. 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.
  3. Final Answer:

    The file config.tpl does not exist in the working directory -> Option D
  4. 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

  1. Step 1: Understand the difference between file() and templatefile()

    file() reads raw content without variable substitution. templatefile() reads and replaces variables inside the file.
  2. 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.
  3. Final Answer:

    Use templatefile() with data.json and pass { region = var.region } as variables -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard