File functions (file, templatefile) in Terraform - Time & Space Complexity
We want to understand how the time to read and process files grows as the file size or number of files increases.
Specifically, how the file and templatefile functions behave when used in Terraform.
Analyze the time complexity of reading and processing files using Terraform functions.
locals {
content1 = file("./config1.txt")
content2 = templatefile("./template.tpl", { var1 = "value" })
}
This code reads a plain text file and a template file with variables to produce strings.
Look at what happens when these functions run.
- Primary operation: Reading file contents from disk and processing template variables.
- How many times: Once per file or templatefile call.
As the size of the file grows, the time to read and process it grows roughly in direct proportion.
| Input Size (file size in KB) | Approx. Api Calls/Operations |
|---|---|
| 10 | Reads 10 KB, processes 10 KB |
| 100 | Reads 100 KB, processes 100 KB |
| 1000 | Reads 1000 KB, processes 1000 KB |
Pattern observation: Time grows linearly with file size.
Time Complexity: O(n)
This means the time to read and process files grows directly with the size of the file.
[X] Wrong: "Reading a file is always instant regardless of size."
[OK] Correct: Larger files take more time to read and process, so time grows with file size.
Understanding how file reading scales helps you design efficient infrastructure code and avoid slow deployments.
"What if we read multiple files in a loop? How would the time complexity change?"
Practice
file() do?Solution
Step 1: Understand the purpose of
Thefile()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, butfile()just reads raw content.Final Answer:
Reads the entire content of a file as plain text -> Option CQuick Check:
file() reads file content [OK]
- Confusing file() with templatefile()
- Thinking file() modifies file content
- Assuming file() writes to files
templatefile() in Terraform?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 AQuick Check:
templatefile(path, vars map) syntax [OK]
- Using named parameters instead of positional
- Passing variables outside a map
- Omitting the variables map argument
message.tpl with content: Hello, ${name}!What is the output of this Terraform expression?
templatefile("message.tpl", { name = "Alice" })Solution
Step 1: Understand templatefile variable replacement
Thetemplatefile()function reads the file and replaces variables like${name}with the provided value.Step 2: Apply the variable substitution
Sincenameis set to "Alice", the output becomes "Hello, Alice!".Final Answer:
Hello, Alice! -> Option AQuick Check:
templatefile replaces variables [OK]
- Expecting raw template text output
- Confusing file() with templatefile() output
- Missing variable in map causing error
output "config" {
value = templatefile("config.tpl", { env = "prod" })
}But Terraform shows an error:
File not found: config.tpl. What is the likely cause?Solution
Step 1: Analyze the error message
The error says "File not found", which means Terraform cannot locate the fileconfig.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 fileconfig.tpldoes not exist in the working directory -> Option DQuick Check:
File not found error means missing file [OK]
- Assuming variable map causes file not found
- Thinking templatefile() is invalid in output
- Ignoring file path correctness
data.json and replaces a placeholder ${region} inside it with your variable var.region. Which approach is best?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}withvar.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 BQuick Check:
templatefile() replaces placeholders with variables [OK]
- Expecting file() to replace variables
- Passing empty variable map to templatefile()
- Using replace() manually instead of templatefile()
Ready to prove it?
Pass the test and this pattern moves unknown
