我有一个通过 terraform 文件定义的 lambda 函数。当我
terraform apply
它时,我收到一条错误消息,指出生成的 .zip 文件大于 lambda 文件允许的最大大小。最初,这很令人困惑,因为两个预期组件(定义函数的 .py
和定义基础设施的 .tf
)各只有几 KB,远小于 50 MB 的限制。 unzip -l lambda_function_add_user_post.zip
显示以下信息:
Length Date Time Name
--------- ---------- ----- ----
15144690 01-01-2049 00:00 .terraform/providers/registry.terraform.io/hashicorp/archive/2.4.2/darwin_arm64/terraform-provider-archive_v2.4.2_x5
444405970 01-01-2049 00:00 .terraform/providers/registry.terraform.io/hashicorp/aws/5.35.0/darwin_arm64/terraform-provider-aws_v5.35.0_x5
213 01-01-2049 00:00 .terraform.tfstate.lock.info
2466 01-01-2049 00:00 lambda-addUserPost.tf
891 01-01-2049 00:00 lambdaFunctions.py
99589452 01-01-2049 00:00 lambda_function_add_user_post.zip
第一个问题...为什么
unzip -l
列出.zip文件本身?它是否以某种方式递归嵌套?这怎么可能?
第二个问题:两个
darwin_arm64
文件显然导致了大小问题。将其完整路径硬编码到 excludes
中可能更简单,但将来了解如何排除整个目录会很有用。
网上的
fileset
文档指示我必须使用此功能,但我无法正确应用它。这是我的尝试:
data "archive_file" "lambda_function_add_user_post" {
type = "zip"
source_dir = "${path.module}"
output_path = "${path.module}/lambda_function_add_user_post.zip"
excludes =
[
fileset("${path.module}/.terraform", "**"),
".terraform.lock.hcl",
"terraform.tfstate",
"terraform.tfstate.backup",
"venv",
"venv/*"
]
}
这会产生以下结果:
Error: Invalid expression
│
│ on lambda-addUserPost.tf line 85, in data "archive_file" "lambda_function_add_user_post":
│ 85: excludes =
│ 86: [
│
│ Expected the start of an expression, but found an invalid expression token.
如果我注释掉
fileset
行,那么它就可以工作(但我收到一个错误:.zip 太大了)。
我怎样才能正确地写这个?
为什么不直接指定
lambdaFunctions.py
作为存档源。
data "archive_file" "lambda_function_add_user_post" {
type = "zip"
source_file = "${path.module}/lambdaFunctions.py"
output_path = "${path.module}/lambda_function_add_user_post.zip"
}
这应该生成仅包含单个文件的 zip。
为什么 unzip -l 列出 .zip 文件本身?
可能您在第二次运行时也获得了此文件。