terraform无法检测到lambda源文件的更改 [英] terraform does not detect changes to lambda source files
问题描述
在我的main.tf
中,我有以下内容:
In my main.tf
I have the following:
data "template_file" "lambda_script_temp_file" {
template = "${file("../../../fn/lambda_script.py")}"
}
data "template_file" "library_temp_file" {
template = "${file("../../../library.py")}"
}
data "template_file" "init_temp_file" {
template = "${file("../../../__init__.py")}"
}
data "archive_file" "lambda_resources_zip" {
type = "zip"
output_path = "${path.module}/lambda_resources.zip"
source {
content = "${data.template_file.lambda_script_temp_file.rendered}"
filename = "lambda_script.py"
}
source {
content = "${data.template_file.library_temp_file.rendered}"
filename = "library.py"
}
source {
content = "${data.template_file.init_temp_file.rendered}"
filename = "__init__.py"
}
}
resource "aws_lambda_function" "MyLambdaFunction" {
filename = "${data.archive_file.lambda_resources_zip.output_path}"
function_name = "awesome_lambda"
role = "${var.my_role_arn}"
handler = "lambda_script.lambda_handler"
runtime = "python3.6"
timeout = "300"
}
问题是,当我在新的terraform apply
上修改一个源文件(例如lambda_script.py
)时,即使存档文件(lambda_resources_zip
)得到了更新,lambda函数的脚本也没有得到更新(新的存档文件无法上传).
The problem is when I modify one of the source files, say lambda_script.py
, upon a new terraform apply
, even though the archive file (lambda_resources_zip
) gets updated, the lambda function's script does not get updated (the new archive file does not get uploaded).
我知道为了避免这种情况,我可以先运行terraform destroy
,但这不是我的用例的选择.
I know that in order to avoid this, I could first run terraform destroy
but that is not an option for my use case.
*我正在使用Terraform v0.11.10
*I'm using Terraform v0.11.10
推荐答案
我通过在资源定义中添加以下行来解决了该问题:
I resolved the issue by adding the following line the resource definition:
source_code_hash = "${data.archive_file.lambda_resources_zip.output_base64sha256}"
修改源文件时,哈希值将更改并触发要更新的源文件.
when the source files are modified, the hashed value will change and trigger the source file to be updated.
这篇关于terraform无法检测到lambda源文件的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!