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.
The filebase64sha256
function is similar to base64sha256(file(...))
, but by combining the two functions together it avoids the need to create an intermediate string of the file contents and thus avoids the requirement that the file be UTF-8 encoded.
Therefore you don't need the file
function call, because reading the file is built in to that function:
source_code_hash = "${filebase64sha256(var.filename)}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With