Skip to content

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

@researchgecko

Description

@researchgecko

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions