how to generate zip file for lambda via terrarform?
Asked Answered
B

4

13

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")
Berthoud answered 24/4, 2022 at 22:10 Comment(0)
O
9

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

Outweigh answered 24/4, 2022 at 22:23 Comment(2)
when we use archive_file , what are the steps or example of terraform script/file in gitlab to destroy the lambda and the zip fileBerthoud
@Berthoud I'm not sure. What you are asking should be new question.Outweigh
D
16

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"
}
Draughtsman answered 7/2, 2023 at 15:53 Comment(0)
O
9

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

Outweigh answered 24/4, 2022 at 22:23 Comment(2)
when we use archive_file , what are the steps or example of terraform script/file in gitlab to destroy the lambda and the zip fileBerthoud
@Berthoud I'm not sure. What you are asking should be new question.Outweigh
N
5

This should work, output_base64sha256 gives exactly what you would need

data "archive_file" "lambda_function_file" {
  type = "zip"
  source_file = "index.js"
  output_path = "lambda_function.zip"
}

resource "aws_lambda_function" "message_filter_lambda" {
  filename         = "lambda_function.zip"
  function_name    = "failed-messages-filter"
  handler          = "index.handler"
  runtime          = "nodejs14.x"
  role             = aws_iam_role.lambda_role.arn
  source_code_hash = data.archive_file.lambda_function_file.output_base64sha256
 
}
Nelly answered 18/4, 2023 at 20:25 Comment(0)
P
1

When yo uneed to create ZIP with package and your Lambda code use this: Assume your Python packages are in ./package folder:

resource "local_file" "lambda_python_code" {
  content  = file("${path.module}/MyLambda.py")
  filename = "${path.module}/package/lambda_function.py"
}

data "archive_file" "lambda_zip" {
  type        = "zip"
  output_path = "${path.module}/lambda_function.zip"
  source_dir = "${path.module}/package"
  depends_on=[local_file.lambda_python_code]
}

resource "aws_lambda_function" "this" {
  function_name    = var.name
  description      = "Created by Terraform"
  role             = aws_iam_role.lambda.arn
  runtime          = "python3.9"
  handler          = "lambda_function.lambda_handler"
  filename         = data.archive_file.lambda_zip.output_path
  source_code_hash = data.archive_file.lambda_zip.output_base64sha256
  tags             = var.tags
}
Panarabism answered 21/3 at 21:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.