This error occurs when Terraform cannot access, locate, or properly resolve a filesystem path referenced in your configuration. It commonly happens with the file() function, module sources, or backend configurations when paths are incorrect, relative paths are misused, or files don't exist.
In Terraform, the "Invalid filesystem path" error indicates that Terraform tried to reference a file or directory that it cannot find, read, or access. This typically happens when using the `file()` function to read local files, specifying module sources as local paths, or configuring backends that reference local state directories. The error can occur during `terraform init`, `terraform plan`, or `terraform apply` phases. Terraform expects all files referenced in configuration to exist at evaluation time, not to be created by resources during the run.
The most common fix is to replace relative paths with path.module (current module directory) or path.root (root module directory).
Instead of this (WRONG):
resource "aws_lambda_function" "example" {
filename = "lambda.zip" # Will fail - relative to CWD, not consistent
handler = "index.handler"
}Do this (CORRECT):
resource "aws_lambda_function" "example" {
filename = "${path.module}/lambda.zip"
handler = "index.handler"
}This ensures the file is located relative to the module file itself, not wherever terraform is run from.
Check that the file you're referencing actually exists at the specified path:
# From your terraform working directory:
ls -la ./lambda.zip
ls -la ./templates/policy.json
# Or use absolute path:
ls -la /home/user/project/lambda.zipThe file() function only works with files that are part of your configuration source code. It cannot reference files that will be created by resources.
When calling local modules, use paths relative to the root module:
Instead of this (may fail):
module "vpc" {
source = "./modules/vpc" # OK from root, but fragile from subdirectories
}Better approach (always works):
module "vpc" {
source = "${path.root}/modules/vpc"
}This is especially important in Terraform Cloud/Enterprise or when modules are called from different contexts.
If you have symlinks in your configuration directory, especially ones pointing outside the repository:
# Find all symlinks:
find . -type l -ls
# Resolve symlinks to actual paths:
ls -L ./config-fileSymlinks that point outside your configuration directory will break in Terraform Cloud/Enterprise because the worker doesn't have access to those external paths. Consider copying the file instead or using an absolute path that exists on the remote runner.
If you need to reference files in a user's home directory (like SSH keys), use the pathexpand() function:
# Instead of:
provisioner "remote-exec" {
private_key = file("~/.ssh/id_rsa") # May fail - ~ not expanded
}
# Do this:
provisioner "remote-exec" {
private_key = file(pathexpand("~/.ssh/id_rsa"))
}This ensures tilde (~) expansion works correctly on all platforms.
If running Terraform on Windows Subsystem for Linux (WSL), be aware of cross-filesystem path issues:
# Verify file is accessible from Terraform's perspective:
cat /path/to/file
# Check if path is on WSL filesystem (preferred) vs Windows drive:
pwd # Should show /home or /mnt paths
# If file is on Windows drive (/mnt/c/Users/...), consider:
# 1. Copy file to WSL home directory
# 2. Use absolute path with proper escapingWSL can have issues with path transformations between Windows and Linux filesystems. Keeping your terraform files and referenced files on the WSL side (/home/user) avoids most issues.
Path Functions Reference:
- path.module: Directory of the current module file
- path.root: Directory of the root module (where you run terraform init)
- path.cwd: Current working directory (less reliable, avoid in modules)
- abspath(): Returns absolute path of a file
- pathexpand(): Expands ~ in paths for home directory access
Terraform Cloud/Enterprise Considerations:
Files in your configuration must be accessible to the remote runner. Symlinks pointing outside the configuration directory or to the local filesystem won't work. Instead:
1. Include files directly in the configuration directory
2. Use variables or data sources to get file contents from external systems
3. Reference files by absolute paths only if they exist on the runner
Module Development Best Practice:
Always use ${path.module} in modules so they work correctly regardless of where they're called from. Never assume the working directory matches the module directory.
Error: Error installing helm release: cannot re-use a name that is still in use
How to fix "release name in use" error in Terraform with Helm
Error: Error creating GKE Cluster: BadRequest
BadRequest error creating GKE cluster in Terraform
Error: External program failed to produce valid JSON
External program failed to produce valid JSON
Error: Unsupported argument in child module call
How to fix "Unsupported argument in child module call" in Terraform
Error: network is unreachable
How to fix "network is unreachable" in Terraform