Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to generate zip file for lambda via terrarform?

I am working on a aws stack and have some lambdas and s3 bucket ( sample code below) . how to generate zip file for lambda via terrarform. I have seen different styles and probably depends on the version of terraform as well.

resource "aws_lambda_function" "my_lambda" {
              filename = "my_lambda_func.zip"
              source_code_hash = filebase64sha256("my_lambda_func.zip")
like image 972
arve Avatar asked Dec 28 '25 20:12

arve


2 Answers

So to give a more up-to-date and use-case based answer, for terraform version 2.3.0, you can apply the following:

data "archive_file" "dynamodb_stream_lambda_function" {
  type = "zip"
  source_file = "../../lambda-dynamodb-streams/index.js"
  output_path = "lambda_function.zip"
}
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"
}
like image 156
João Pedro Schmitt Avatar answered Dec 30 '25 12:12

João Pedro Schmitt


Using archive_file would be most common. You can zip individual files or entire folders, depending how your lambda function is developed.

like image 31
Marcin Avatar answered Dec 30 '25 13:12

Marcin