How to fix Error: Error creating archive: file not found in Terraform

TerraformINTERMEDIATEHIGHUpdated Dec 31, 2025ยท 104 views

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.

Quick fix

Fixes Error: Error creating archive: file not found

# For a file
ls -la path/to/your/file.txt

# For a directory
ls -la path/to/your/directory/
  1. Verify the source file or directory exists locally
  2. Use path.module for relative paths in modules
  3. Check path format and separators
  4. Add explicit depends_on if source is created by another resource
terraform โ€” error
# For a file
ls -la path/to/your/file.txt
# For a directory
ls -la path/to/your/directory/
Error: Error creating archive: file not found

What this error means

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.

Typical symptoms

  • terraform plan fails immediately with 'Error creating archive: file not found'
  • Error message includes the path Terraform was trying to access
  • Works locally but fails in CI/CD pipelines
  • Changes to source path resolve the error

Common causes

  1. Source file or directory does not exist at the path specified
  2. Using relative paths that are resolved relative to the root module instead of the current module (use path.module instead of path.root)
  3. File path contains typos or incorrect directory separators
  4. In CI/CD, source file is created by a previous step but Terraform plan runs before the file exists
  5. Using hardcoded absolute paths that differ between environments
  6. Source path references a file that should be created by another resource but has not been applied yet

How to fix "Error: Error creating archive: file not found"

1Verify the source file or directory exists locally

Check that the file or directory you're trying to archive physically exists on your filesystem at the path specified in your Terraform configuration.

bash
# 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.

2Use path.module for relative paths in modules

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.

hcl
# 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"
}
3Check path format and separators

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.

hcl
# 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"
}
4Add explicit depends_on if source is created by another resource

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.

hcl
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]
}
5For CI/CD, ensure files exist before terraform plan

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.

bash
# Example GitHub Actions workflow
- name: Build application
  run: npm run build

- name: Terraform init and plan
  run: |
    terraform init
    terraform plan
6Check Terraform and archive provider versions

Older 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.

hcl
terraform {
  required_providers {
    archive = {
      source  = "hashicorp/archive"
      version = "~> 2.4"
    }
  }
}

Advanced notes

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.

Frequently asked questions

What does "Error: Error creating archive: file not found" mean?
) 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.
How do I fix "Error: Error creating archive: file not found"?
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.
What causes "Error: Error creating archive: file not found"?
Source file or directory does not exist at the path specified Using relative paths that are resolved relative to the root module instead of the current module (use path.module instead of path.root) File path contains typos or incorrect directory separators
Is the "Error: Error creating archive: file not found" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading