Problem
When using the archive_file data source, a Terraform run fails during the planning phase with the following error message.
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 emptyThis error occurs with a configuration similar to the following example.
data "archive_file" "test" {
type = "zip"
source_dir = "to_zip"
output_path = "zipped/test.zip"
}Prerequisites
- Terraform Archive provider version 2.4.2 or newer.
- The directory specified in
source_diris empty.
Cause
Version 2.4.2 of the Terraform Archive provider introduced a change to prevent the creation of empty zip archives. This change returns an error if the source_dir contains no files, as noted in the provider's changelog.
data-source/archive_file: Return error when generated archive would be empty
Solutions
Solution 1: Ensure the Source Directory is Not Empty
The intended behavior of the provider is to archive content. Ensure that the directory specified in the source_dir argument contains at least one file before running Terraform.
Solution 2: Pin the Provider to an Older Version
If your use case requires creating an archive from an empty directory, you can pin the Archive provider to a version prior to the change, such as 2.4.0. Add a required_providers block to your configuration to specify the version.
terraform {
required_providers {
archive = {
source = "hashicorp/archive"
version = "2.4.0"
}
}
}Additional Information
Important Consideration
Do not use the same path for the output_path and source_dir arguments. This configuration can cause a recursive archiving loop where the output zip file is included in the source for the next operation.