I’m having trouble configuring my AWS Lambda feature in Terraform. Right now, the following configuration works so that the function deployment flies every time, even when I don’t have any changes made to it. This is because the source_code_hash
is generated different from the previous one.
Do you have any idea how to make it better so that terraform detects changes in the code and only when it finds them does it then do its deployment?
data "archive_file" "parser_service" {
type = "zip"
source_dir = "${path.module}/xxx"
output_path = "${path.module}/xxx.zip"
}
resource "aws_s3_object" "s3_object" {
bucket = var.lambda_bucket
key = "lambda_parser_${var.node_env}.zip"
source = "${path.module}/lambda_parser_${var.node_env}.zip"
etag = filemd5(data.archive_file.parser_service.output_path)
}
resource "aws_lambda_function" "parser_function" {
function_name = var.lambda_name
handler = "xxx/main.handler"
role = var.iam_role
runtime = var.runtime
memory_size = var.memory_size
timeout = var.timeout
s3_bucket = var.lambda_bucket
s3_key = aws_s3_object.s3_object.key
source_code_hash = filemd5("${path.module}/lambda_parser_${var.node_env}.zip")
depends_on = [aws_s3_object.s3_object]
}
Thanks for any help!