aws lambda上载需要生成所需源代码和库的zip存档。对于使用nodejs作为lambda的语言,可能更典型的情况是希望源文件和node_modules目录包含在zip存档中。TyrFraseAccess提供程序提供了一个文件文件资源,当它可以被使用时它工作得很好。当您想要的不仅仅是1个文件或1个目录,它就不能使用。见
feature request
. 为了解决这个问题,我想出了下面的代码。它执行步骤,但不按要求的顺序执行。运行一次,它会更新zip文件,但不会将其上载到aws。我再次运行它并上传到aws。
# This resource checks the state of the node_modules directory, hoping to determine,
# most of the time, when there was a change in that directory. Output
# is a 'mark' file with that data in it. That file can be hashed to
# trigger updates to zip file creation.
resource "null_resource" "get_directory_mark" {
provisioner "local-exec" {
command = "ls -l node_modules > node_modules.mark; find node_modules -type d -ls >> node_modules.mark"
interpreter = ["bash", "-lc"]
}
triggers = {
always = "${timestamp()}" # will trigger each run - small cost.
}
}
resource "null_resource" "make_zip" {
depends_on = ["null_resource.get_directory_mark"]
provisioner "local-exec" {
command = "zip -r ${var.lambda_zip} ${var.lambda_function_name}.js node_modules"
interpreter = ["bash", "-lc"]
}
triggers = {
source_hash = "${sha1("${file("lambda_process_firewall_updates.js")}")}"
node_modules = "${sha1("${file("node_modules.mark")}")}" # see above
}
}
resource "aws_lambda_function" "lambda_process" {
depends_on = ["null_resource.make_zip"]
filename = "${var.lambda_zip}"
function_name = "${var.lambda_function_name}"
description = "process items"
role = "${aws_iam_role.lambda_process.arn}"
handler = "${var.lambda_function_name}.handler"
runtime = "nodejs8.10"
memory_size = "128"
timeout = "60"
source_code_hash = "${base64sha256(file("lambda_process.zip"))}"
}
其他相关讨论包括:
this question on code hashing
,(见我的回答)和
this GitHub issue
.