The Terraform archive_file data source cannot find the source file or directory specified in your configuration. This happens when the path is incorrect, uses the wrong base directory, or references a file that does not exist at plan time.
Terraform's archive_file data source attempts to create a compressed archive (ZIP, TAR, etc.) from local files or directories during terraform plan. When the source_file, source_dir, or source block references a path that cannot be found on the filesystem, Terraform fails with a 'file not found' error. This typically occurs because the path is relative to the wrong directory, uses incorrect relative paths, or the file simply doesn't exist yet.
Check that the file or directory you're trying to archive physically exists on your filesystem at the path specified in your Terraform configuration.
# For a file
ls -la path/to/your/file.txt
# For a directory
ls -la path/to/your/directory/If the path doesn't exist, create it first or adjust the path in your Terraform configuration.
When using the archive_file data source within a Terraform module, always use ${path.module} to construct relative paths instead of ${path.root}. This ensures paths are resolved relative to the module directory, not the root module.
# WRONG - resolves to root module
data "archive_file" "example" {
type = "zip"
source_dir = "./src"
output_path = "./build/app.zip"
}
# CORRECT - resolves relative to module directory
data "archive_file" "example" {
type = "zip"
source_dir = "${path.module}/src"
output_path = "${path.module}/build/app.zip"
}Ensure your paths use forward slashes (/) even on Windows, as Terraform normalizes paths. Verify there are no typos in directory or file names, including capitalization.
# CORRECT - forward slashes, explicit path
data "archive_file" "lambda_code" {
type = "zip"
source_dir = "${path.module}/lambda/src"
output_path = "${path.module}/build/lambda.zip"
}If the file is generated by another Terraform resource (like local_file or null_resource), add an explicit depends_on to ensure the file exists before Terraform attempts to archive it.
resource "local_file" "lambda_handler" {
filename = "${path.module}/build/index.js"
content = file("${path.module}/src/handler.js")
}
data "archive_file" "lambda_zip" {
type = "zip"
source_file = local_file.lambda_handler.filename
output_path = "${path.module}/build/lambda.zip"
depends_on = [local_file.lambda_handler]
}In automated pipelines, ensure source files are created or downloaded before running terraform plan. If files are generated by previous build steps, add those steps before Terraform runs.
# Example GitHub Actions workflow
- name: Build application
run: npm run build
- name: Terraform init and plan
run: |
terraform init
terraform planOlder versions of the archive provider had different behavior with file paths and timing. Update both Terraform and the archive provider to the latest versions, or pin to a known-good version.
terraform {
required_providers {
archive = {
source = "hashicorp/archive"
version = "~> 2.4"
}
}
}The archive_file data source is evaluated during terraform plan, not terraform apply. This means all source files must exist before planning, even though the archive itself may not be used until apply. In CI/CD environments, this can cause issues if files are generated as part of the apply phase. Use explicit dependencies (depends_on) or reorganize your workflow to generate files before planning. On Windows, ensure the zip utility is available in your PATH or configure Terraform to use an alternative compression tool.
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