Introduction
Problem
When using the archive_file resource the following error is observed:
data.archive_file.test: Reading...
Planning failed. Terraform encountered an error while generating this plan.
╷
│ Error: Archive creation error
│
│ with data.archive_file.test,
│ on main.tf line 10, in data "archive_file" "test":
│ 10: data "archive_file" "test" {
│
│ error creating archive: error archiving directory: archive has not been created as it would be empty
Example code:
data "archive_file" "test" {
type = "zip"
source_dir = "to_zip"
output_path = "zipped/test.zip"
}
Prerequisites
- Using the Archive provider 2.4.2 or newer.
- Have an empty source directory.
Cause
In version 2.4.2 of the archive provider there was a bug fix to prevent zipping empty directories.
From the change log:
data-source/archive_file: Return error when generated archive would be empty
Solutions:
-
Have at least one file in the source directory to zip.
- Use an older version of the Archive provider, for example 2.4.0
terraform {
required_providers {
archive = {
source = "hashicorp/archive"
version = "2.4.0"
}
}
}
Extra:
Do not use the same path for output_path
and source_dir
. This will create a zip, in a zip, in a zip,.......