如何通过 terrarform 为 lambda 生成 zip 文件?

arv*_*rve 6 amazon-web-services terraform terraform-provider-aws

我正在开发 aws 堆栈,并有一些 lambda 和 s3 存储桶(下面的示例代码)。如何通过 terrarform 为 lambda 生成 zip 文件。我见过不同的风格,可能也取决于 terraform 的版本。

resource "aws_lambda_function" "my_lambda" {
              filename = "my_lambda_func.zip"
              source_code_hash = filebase64sha256("my_lambda_func.zip")
Run Code Online (Sandbox Code Playgroud)

Joã*_*itt 10

因此,为了提供更新且基于用例的答案,对于 terraform 版本 2.3.0,您可以应用以下内容:

data "archive_file" "dynamodb_stream_lambda_function" {
  type = "zip"
  source_file = "../../lambda-dynamodb-streams/index.js"
  output_path = "lambda_function.zip"
}
Run Code Online (Sandbox Code Playgroud)
resource "aws_lambda_function" "my_dynamodb_stream_lambda" {
  function_name = "my-dynamodb-stream-lambda"
  role = aws_iam_role.my_stream_lambda_role.arn
  handler = "index.handler"
  filename = data.archive_file.dynamodb_stream_lambda_function.output_path
  source_code_hash = data.archive_file.dynamodb_stream_lambda_function.output_base64sha256
  runtime = "nodejs16.x"
}
Run Code Online (Sandbox Code Playgroud)


Mar*_*cin 6

使用archive_file是最常见的。您可以压缩单个文件或整个文件夹,具体取决于 lambda 函数的开发方式。