我已经编写了一个 terraform 模块来创建一个 Lambda,但我无法弄清楚如何在预构建的 ZIP 文件上计算 source_code_hash。这将在管道中,因此每次都会构建 ZIP 文件,并且在我到达 terraform 步骤之前可能会有所不同。
我正在使用 gulp 构建 ZIP 文件(这是一个 NodeJS 应用程序)并假设它是预建在目录 build/myLambda.zip
基本上我想这样做。文件名是一个 terraform 变量,我希望 source_code_hash 计算必须引用该文件。
module my_lambda {
filename = "${var.my_zip_file}"
}
该模块的相关部分是:
resource "aws_lambda_function" "lambda" {
filename = "${var.filename}"
source_code_hash = "${filebase64sha256(file("${var.filename}"))}"
}
但是,当我运行 terraform plan 时,出现此错误:
Error: Error in function call
on modules\lambda\main.tf line 16, in resource "aws_lambda_function" "lambda":
16: source_code_hash = "${filebase64sha256(file("${var.filename}"))}"
|----------------
| var.filename is "build/myLambda.zip"
Call to function "file" failed: contents of
build/myLambda.zip are not valid UTF-8;
use the filebase64 function to obtain the Base64 encoded contents or the other
file functions (e.g. filemd5, filesha256) to obtain file hashing results
instead.