Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compute source code hash on pre-built ZIP file for lambda_module?

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.
like image 281
ChrisRTech Avatar asked Jan 01 '23 14:01

ChrisRTech


1 Answers

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)}"
like image 137
Martin Atkins Avatar answered May 05 '23 12:05

Martin Atkins