I have written a terraform module to create a Lambda and I'm having trouble figuring out how to compute the source_code_hash on a PRE-built ZIP file. This will be in a pipeline so the ZIP file will get built each time and perhaps be different before I reach the terraform step.
I'm building the ZIP file with gulp (this is a NodeJS App) and assume it is pre-built in directory
build/myLambda.zip
Essentially I want to do this. The filename is a terraform variable and I want the source_code_hash computation has to reference that file.
module my_lambda {
filename = "${var.my_zip_file}"
}
The relevant parts of the module are:
resource "aws_lambda_function" "lambda" {
filename = "${var.filename}"
source_code_hash = "${filebase64sha256(file("${var.filename}"))}"
}
However when I run terraform plan I get this error:
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.