Terraform cannot find a file specified in your configuration using the `file()` function or `local_file` data source. This usually happens due to incorrect file paths, timing issues with resource creation, or module dependency problems.
When Terraform tries to read a local file using the `file()` function or the `local_file` data source, it expects the file to exist on disk at the time of plan or apply. This error occurs because the file path Terraform is looking for doesn't exist at that location. Terraform's file reading functions are evaluated early in the planning phase, before many resources are created. If your configuration tries to read a file that will be created by another resource, Terraform fails because the file doesn't exist yet. The file system path resolution can also be affected by how you specify paths (relative vs absolute) and whether you're running Terraform in a module or from the root directory. This is a common issue in configurations that reference dynamically-generated files, module-relative paths, or files that depend on other resource outputs.
Before running Terraform, confirm the file actually exists where your configuration expects it:
# Check if the file exists
ls -la /path/to/your/file.txt
# Or use Terraform to show the evaluated path
terraform console
> abspath("./relative/path/to/file.txt")If the file doesn't exist, create it first before running Terraform plan or apply. Make sure the filename and path are spelled correctly.
If you're reading a file from within a module, use path.module instead of relative paths:
# ❌ Incorrect - relative path won't work from other modules
data "local_file" "config" {
filename = "./config.txt"
}
# ✅ Correct - uses module's directory
data "local_file" "config" {
filename = "${path.module}/config.txt"
}path.module automatically resolves to the directory containing your current module's .tf files, which works correctly regardless of where you run Terraform from.
If another resource creates the file you're trying to read, use depends_on to ensure correct ordering:
# Resource creates the file
resource "local_file" "generated" {
filename = "${path.module}/output.txt"
content = "generated content"
}
# Data source reads it - but only after resource creates it
data "local_file" "read_generated" {
filename = local_file.generated.filename
depends_on = [local_file.generated] # Explicit dependency
}Reference the resource output directly instead of using a hardcoded path. This tells Terraform the data source depends on the resource being created first.
Ensure Terraform is running from the correct directory and understand where paths are relative to:
# See where Terraform resolves paths from
terraform console
> path.cwd # Current working directory
> path.root # Root module directory
> path.module # Current module directory
> abspath("file.txt") # Shows where a relative path resolves toIf using Terraform Cloud or Enterprise, verify the workspace's "Terraform Working Directory" setting matches where your files are located. Don't use path.cwd in configurations - use path.module or path.root instead.
If you need to convert a relative path to an absolute path, use the abspath() function:
# Convert relative path to absolute
local {
config_path = abspath("${path.module}/config.txt")
}
data "local_file" "config" {
filename = local.config_path
}This ensures the path is fully resolved and portable across different execution environments.
Don't use absolute paths like /home/user/project/file.txt in production:
# ❌ Avoid - breaks on other machines/environments
data "local_file" "bad" {
filename = "/home/user/project/file.txt"
}
# ✅ Better - works on any machine/environment
data "local_file" "good" {
filename = "${path.module}/file.txt"
}Use path.module, path.root, or relative paths with abspath() to make configurations portable across environments.
For Terraform Cloud/Enterprise users, note that the working directory setting in workspace configuration determines where paths are resolved relative to. If you're using modules with local file references, ensure the module structure and file locations are consistent across all environments.
When using path.cwd be cautious - it returns an absolute path that can change depending on where Terraform is executed from. This can cause unexpected changes on subsequent runs. Prefer path.module (for module-relative paths) or path.root (for root module directory) instead.
For testing scenarios where files don't exist yet, consider using try() or can() functions combined with conditional logic to handle missing files gracefully, or create placeholder files during testing setup.
Error: Error rendering template: template not found
How to fix "template not found" error in Terraform
Error: Error generating private key
How to fix 'Error generating private key' in Terraform
Error creating Kubernetes Service: field is immutable
How to fix "field is immutable" errors in Terraform
Error: Error creating local file: open: permission denied
How to fix "Error creating local file: permission denied" in Terraform
Error: line endings have changed from CRLF to LF
Line endings have changed from CRLF to LF in Terraform