在我的
main.tf
中,我有以下内容:
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"
}
问题是,当我修改其中一个源文件(例如
lambda_script.py
)时,在新的 terraform apply
上,即使存档文件 (lambda_resources_zip
) 已更新,lambda 函数的脚本也不会更新(新存档文件未上传)。
我知道为了避免这种情况,我可以先运行
terraform destroy
但这不是我的用例的选项。
*我正在使用 Terraform v0.11.10
我通过添加以下行资源定义解决了该问题:
source_code_hash = "${data.archive_file.lambda_resources_zip.output_base64sha256}"
当源文件被修改时,哈希值会改变并触发源文件更新。
这对我有用 -
source_hash = "${data.archive_file.source.output_base64sha256}"
到
s3 lambda bucket
这将告诉您是否进行了任何更改。
source_code_hash = "${data.archive_file.source.output_base64sha256}"
所以你的代码应该是这样的 -
resource "aws_s3_object" "lambda_object" {
source_hash = "${data.archive_file.source.output_base64sha256}"
bucket = "${aws_s3_bucket.s3.bucket}"
key = "${var.key}"
source = data.archive_file.source.output_path
}
resource "aws_lambda_function" "lambda_" {
function_name = "lambda_name"
source_code_hash = "${data.archive_file.source.output_base64sha256}"
.......
.......
}
为我工作。最美好的祝愿。
你也可以尝试一下
publish = true