有没有办法为Terraform存档提供程序定义多个source_file? [英] Is there a way to define multiple source_file for Terraform archive provider?
本文介绍了有没有办法为Terraform存档提供程序定义多个source_file?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用Terraform archive_file provider将多个文件打包为zip文件。当我这样定义归档文件时,它工作得很好:
data "archive_file" "archive" {
type = "zip"
output_path = "./${var.name}.zip"
source_dir = "${var.source_dir}"
}
但是,我不希望存档包含var.source_dir
中的所有文件,我只需要其中的一部分。我注意到ARCHIVE_FILE提供程序有一个source_file
属性,所以我希望我可以提供这些文件的列表,并将它们打包到存档中,如下所示:
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]}"
}
但这不起作用,将为local.source-files
中定义的每个文件构建存档,因此我有一个"最后一个获胜"的方案,构建的存档文件只包含bar.txt。
我尝试过:
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}"
}
但不足为奇的是,失败的原因是:
data.archive_file.archive:source_file必须是单个值,不能是列表
是否有办法实现我想要的目标,即将文件列表传递给ARCHIVE_FILE提供程序,并让它将所有文件打包到存档文件中?
推荐答案
-谢谢jamiet,我根据您的评论进行了修改-
- 将文件复制到临时目录并存档
locals {
source_files = ["${var.source_dir}/main.py", "${var.source_dir}/requirements.txt"]
}
data "template_file" "t_file" {
count = "${length(local.source_files)}"
template = "${file(element(local.source_files, count.index))}"
}
resource "local_file" "to_temp_dir" {
count = "${length(local.source_files)}"
filename = "${path.module}/temp/${basename(element(local.source_files, count.index))}"
content = "${element(data.template_file.t_file.*.rendered, count.index)}"
}
data "archive_file" "archive" {
type = "zip"
output_path = "${path.module}/${var.name}.zip"
source_dir = "${path.module}/temp"
depends_on = [
"local_file.to_temp_dir",
]
}
- 使用ARCHIVE_FILE的源
locals {
source_files = ["${var.source_dir}/main.py", "${var.source_dir}/requirements.txt"]
}
data "template_file" "t_file" {
count = "${length(local.source_files)}"
template = "${file(element(local.source_files, count.index))}"
}
data "archive_file" "archive" {
type = "zip"
output_path = "./${var.name}.zip"
source {
filename = "${basename(local.source_files[0])}"
content = "${data.template_file.t_file.0.rendered}"
}
source {
filename = "${basename(local.source_files[1])}"
content = "${data.template_file.t_file.1.rendered}"
}
}
- 创建shell脚本并使用外部数据资源调用它。
locals {
source_files = ["${var.source_dir}/main.py", "${var.source_dir}/requirements.txt"]
}
data "template_file" "zip_sh" {
template = <<EOF
#!/bin/bash
zip $* %1>/dev/null %2>/dev/null
echo '{"result":"success"}'
EOF
}
resource "local_file" "zip_sh" {
filename = "${path.module}/zip.sh"
content = "${data.template_file.zip_sh.rendered}"
}
data "external" "zip_sh" {
program = ["${local_file.zip_sh.filename}", "${var.name}", "${join(" ", local.source_files)}"]
depends_on = [
"data.template_file.zip_sh",
]
}
这篇关于有没有办法为Terraform存档提供程序定义多个source_file?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文