为什么我会得到“zip 的内容不是有效的 UTF-8;"在 Terraform 中使用 filebase64sha256 时? [英] Why do I get "contents of zip are not valid UTF-8;" when using filebase64sha256 in Terraform?
问题描述
我正在尝试使用 Terraform 创建一个 AWS lambda 函数,如指定 这里:
I am trying to create an AWS lambda function using Terraform, as specified here:
resource "aws_lambda_function" "dev" {
role = aws_iam_role.dev.arn
handler = var.handler
runtime = var.runtime
filename = "lambda.zip"
function_name = var.function_name
source_code_hash = filebase64sha256(file("lambda.zip"))
}
但是它会抛出一个错误:
But it throws an error:
Call to function "file" failed: contents of lambda.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.
我也尝试过 "${base64sha256(file("lambda.zip"))}"
但我仍然遇到同样的错误.
I've also tried "${base64sha256(file("lambda.zip"))}"
but I still get the same error.
推荐答案
Terraform 中所有以 file
开头的函数都接受文件路径,而不是内容.
All functions in Terraform starting with file
take in the file path, not the contents.
这里有两个问题:
您正在使用
file
读取二进制文件 - 它只接受 UTF-8 文本
You're using
file
to read a binary file - it only accepts UTF-8 text
当 filebase64sha256
已经处理了 file
(file
前缀是它接受一个文件路径)
You're using file
when that is already handled by filebase64sha256
(the file
prefix is the hint that this accepts a file path)
文档突出了这两个方面:
这类似于 base64sha256(file(filename)),但因为 file 只接受 UTF-8 文本,它不能用于为二进制文件创建哈希.
This is similar to base64sha256(file(filename)), but because file accepts only UTF-8 text it cannot be used to create hashes for binary files.
删除对 file(...)
的调用,它应该可以工作:
Remove the call to file(...)
and it should work:
resource "aws_lambda_function" "dev" {
role = aws_iam_role.dev.arn
handler = var.handler
runtime = var.runtime
filename = "lambda.zip"
function_name = var.function_name
source_code_hash = filebase64sha256("lambda.zip")
}
这篇关于为什么我会得到“zip 的内容不是有效的 UTF-8;"在 Terraform 中使用 filebase64sha256 时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!