What does the `templatefile()` function in Terraform do?

Direct Answer (Verified)Reviewed by Sunny (Senior Cloud Engineer)

Correct Answer: Reads a local file and renders it with variable substitutions using HCL template syntax

`templatefile(path, vars)` reads a file (e.g., a user data script or config template) and replaces `${var_name}` placeholders with values from the `vars` map. It returns the render...

Real Exam Scenario

What does the `templatefile()` function in Terraform do?

✓ Correct Answer & Explanation

Correct Answer

Reads a local file and renders it with variable substitutions using HCL template syntax

Why This Answer Is Correct

`templatefile(path, vars)` reads a file (e.g., a user data script or config template) and replaces `${var_name}` placeholders with values from the `vars` map. It returns the rendered string. Commonly used for EC2 user data scripts, Kubernetes manifests, and config files. The template file itself is NOT a Terraform-managed resource — it's just read at plan/apply time from your local filesystem.