Bird
Raised Fist0
Terraformcloud~30 mins

File functions (file, templatefile) in Terraform - Mini Project: Build & Apply

Choose your learning style10 modes available
Using File Functions in Terraform
📖 Scenario: You are setting up infrastructure using Terraform. You want to include configuration data stored in external files. Terraform provides functions to read file contents and templates from files.
🎯 Goal: Learn how to use Terraform's file() and templatefile() functions to read static files and render templates with variables.
📋 What You'll Learn
Create a Terraform variable to hold the path to a static file
Create a Terraform variable to hold the path to a template file
Use the file() function to read the static file content
Use the templatefile() function to render the template with variables
💡 Why This Matters
🌍 Real World
Reading configuration files and rendering templates is common when provisioning cloud infrastructure with Terraform. This helps keep configurations modular and reusable.
💼 Career
Cloud engineers and DevOps professionals often use Terraform file functions to manage infrastructure as code efficiently.
Progress0 / 4 steps
1
Create variables for file paths
Create two Terraform variables: static_file_path with value "./config.txt" and template_file_path with value "./template.tpl".
Terraform
Hint

Use variable blocks with default values for the file paths.

2
Create variables for template values
Create a Terraform variable called template_vars with a map containing "app_name" = "MyApp" and "env" = "production".
Terraform
Hint

Use a variable block with a default map for template variables.

3
Use file() and templatefile() functions
Create two local variables: static_content using file(var.static_file_path) and rendered_template using templatefile(var.template_file_path, var.template_vars).
Terraform
Hint

Use a locals block to define static_content and rendered_template using the file() and templatefile() functions.

4
Output the file contents and rendered template
Create two outputs: output_static with value local.static_content and output_template with value local.rendered_template.
Terraform
Hint

Use output blocks to expose the contents of the static file and the rendered template.

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