我的 terraform 模块有一个存储库,其中我使用存档文件创建一个 zip 文件,然后将其用作 lambda 的文件源
data "archive_file" "example" {
type = "zip"
output_path = "./files/example.zip"
source {
content = templatefile("${path.module}/files/example.py", {})
filename = "example.py"
}
}
resource "aws_lambda_function" "example_lambda" {
filename = data.archive_file.example.output_path
source_code_hash = data.archive_file.example.output_base64sha256
etc...
}
在此存储库中存在 example.py 文件,文件路径为 ./files/example.zip
然后在我的项目仓库中我调用该模块:
module "my_example_module" {
source = "git::https://github.com/my-module-repo"
project_name = var.project_name
etc...
}
但是当我尝试应用此方法时,会出错,提示找不到 zip 文件,但我认为它正在查找我的项目目录而不是模块目录:
module.example.aws_lambda_function.example_lambda: Creating...
Error: reading ZIP file (./files/example.zip): open ./files/example.zip: no such file or directory
${path.module} 在这里使用不正确吗?我如何让 terraform 应用于在模块中查找 zip 文件?
我尝试努力输入路径只是为了看看它是否会克服错误,但我不认为我输入了模块中目录的正确路径 - 它会像
module.my_example_module../files/example.py
吗?
编辑
所以我想我现在更了解这个问题了。我稍微更改了代码,因此输出路径使用 path.module:
data "archive_file" "example" {
type = "zip"
output_path = "${path.module}/files/example.zip"
source {
content = templatefile("${path.module}/files/example.py", {})
filename = "example.py"
}
}
现在,当我在项目中调用模块时,它正在寻找路径:
".terraform/modules/my_example_module/files/example.zip"
,这是正确的。
但是由于模块中的 lambda 函数设置为使用数据存档块中的
output_path
,它还会在模块存储库中的 ".terraform/modules/my_example_module/files/example.zip"
中查找,而该目录不存在(除非 terraform 只是创建这些目录,如果它们尚不存在?)
所以现在的问题是,我该怎么做:
".terraform/modules/my_example_module/files/example.zip"
./files/example.zip
我的项目文件结构是:
project
│ README.md
│
└───terraform
│ └───files
│ │ lambda.tf
│ │ main.tf
│ │ ...
好吧,事实证明,原因是我通过管道运行我的 terraform 作业,并且计划和批准阶段是单独的作业。抱歉,我没有在原始帖子中包含此信息,但我根本没有想到它与管道相关
存档提供商将出于某种原因在计划阶段创建 zip 文件,但不会申请,并且 zip 文件不会在作业之间传递。
这里是对此问题的讨论以及一些解决方法:https://github.com/hashicorp/terraform-provider-archive/issues/39
我遇到了类似的问题,我使用管道来运行 terraform 计划并通过审批门申请工作。
这对我有用:https://github.com/hashicorp/terraform-provider-archive/issues/39#issuecomment-815021702
data "archive_file" "zip" {
type = "zip"
source_file = "${path.module}/textfile.txt"
output_path = "${path.module}/myfile-${random_string.r.result}.zip"
}
resource "random_string" "r" {
length = 16
special = false
}