Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable choosing between preserving or excluding parent directory when using source_dir with archive_file resource #398

Open
1 task done
researchgecko opened this issue Jan 14, 2025 · 1 comment

Comments

@researchgecko
Copy link

Terraform CLI and Provider Versions

Terraform v1.10.4
on linux_amd64

  • provider registry.terraform.io/hashicorp/archive v2.7.0

Use Cases or Problem Statement

I'm currently using the archive_file resource to create a zip of a folder with multiple nested folders.

Example structure of folder:

parent_folder/
 | - folder1/
 | | - file1_in_folder1
 | | - file2_in_folder1
 | - folder2/
 | | - file1_in_folder2
 | - file1
 | - file2
 | - file3
 | - ...

Terraform:

resource "archive_file" "mydir" {
  type        = "zip"
  output_path = "mydir.zip"
  source_dir  = "./parent_folder/"
}

archive_file includes the parent_folder, with all the files within it, i.e., when the archive is extracted, there is a parent_folder at the root. However, for my usecase, the requirement is that the parent_folder should not be included, i.e., file1 should be at the root when the file is unzipped.

I tried source_dir = "parent_folder/*", but that threw an error.

I'm working around this by specifying each file in its own source block instead, but this become unscalable with complex directory structures.

Proposal

Perhaps something like:

resource "archive_file" "mydir" {
  type        = "zip"
  output_path = "mydir.zip"
  source_dir  = "./parent_folder"
  skip_parent = true
}

where if skip_parent is true, the archive includes only the files within the directory and not the directory itself.

====

An alternative would be to preserve parent if there is no trailing forward-slash, and exclude parent if there is (as suggested in this related issue: #66 (comment)).

resource "archive_file" "mydir" {
  type        = "zip"
  output_path = "mydir.zip"
  source_dir  = "./parent_folder"     // preserves parent folder
}
resource "archive_file" "mydir" {
  type        = "zip"
  output_path = "mydir.zip"
  source_dir  = "./parent_folder/"     // excludes parent folder
}

How much impact is this issue causing?

Medium

Additional Information

This seems to be the opposite problem to that mentioned in #66, so it seems like things worked differently in a previous version.

Code of Conduct

  • I agree to follow this project's Code of Conduct
@researchgecko researchgecko changed the title Requesting modification to archive_file resource when using source_dir, to enable choosing between preserving or excluding parent directory Enable choosing between preserving or excluding parent directory when using source_dir with archive_file resource Jan 14, 2025
@researchgecko
Copy link
Author

Issue was with the extraction, not archive_file. Apologies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant