I am using the Terraform archive_file provider to package multiple files into a zip file. It works fine when I define the archive like this:
data "archive_file" "archive" {
type = "zip"
output_path = "./${var.name}.zip"
source_dir = "${var.source_dir}"
}
However I don't want the archive to contain all of the files in var.source_dir
, I only want a subset of them. I notice the archive_file provider has a source_file
attribute so I was hoping I could supply a list of those files and package them into the archive like so:
locals {
source_files = ["${var.source_dir}/foo.txt", "${var.source_dir}/bar.txt"]
}
data "archive_file" "archive" {
type = "zip"
output_path = "./${var.name}.zip"
count = "2"
source_file = "${local.source_files[count.index]}"
}
but that doesn't work, the archive gets built for each file defined in local.source-files
hence I have a "last one wins" scenario where the archive file that gets built only contains bar.txt.
I tried this:
locals {
source_files = ["${var.source_dir}/main.py", "${var.source_dir}/requirements.txt"]
}
data "archive_file" "archive" {
type = "zip"
output_path = "./${var.name}.zip"
source_file = "${local.source_files}"
}
but unsurprisingly that failed with:
data.archive_file.archive: source_file must be a single value, not a list
Is there a way to achieve what I'm after here i.e pass a list of files to the archive_file provider and have it package all of them into the archive file?