terraform does not detect changes to lambda source files
Asked Answered
M

2

10

In my main.tf I have the following:

data "template_file" "lambda_script_temp_file" {
  template = "${file("../../../fn/lambda_script.py")}"
}

data "template_file" "library_temp_file" {
  template = "${file("../../../library.py")}"
}

data "template_file" "init_temp_file" {
  template = "${file("../../../__init__.py")}"
}

data "archive_file" "lambda_resources_zip" {
  type        = "zip"
  output_path = "${path.module}/lambda_resources.zip"

  source {
    content   = "${data.template_file.lambda_script_temp_file.rendered}"
    filename  = "lambda_script.py"
  }

  source {
    content   = "${data.template_file.library_temp_file.rendered}"
    filename  = "library.py"
  }

  source {
    content   = "${data.template_file.init_temp_file.rendered}"
    filename  = "__init__.py"
  }
}

resource "aws_lambda_function" "MyLambdaFunction" {
  filename          = "${data.archive_file.lambda_resources_zip.output_path}"
  function_name     = "awesome_lambda"
  role              = "${var.my_role_arn}"
  handler           = "lambda_script.lambda_handler"
  runtime           = "python3.6"
  timeout           = "300"
}

The problem is when I modify one of the source files, say lambda_script.py, upon a new terraform apply, even though the archive file (lambda_resources_zip) gets updated, the lambda function's script does not get updated (the new archive file does not get uploaded).

I know that in order to avoid this, I could first run terraform destroy but that is not an option for my use case.

*I'm using Terraform v0.11.10

Maishamaisie answered 26/11, 2018 at 8:50 Comment(1)
Can you confirm which version of Terraform you are using ?Cherubini
M
15

I resolved the issue by adding the following line the resource definition:

source_code_hash  = "${data.archive_file.lambda_resources_zip.output_base64sha256}"

when the source files are modified, the hashed value will change and trigger the source file to be updated.

Maishamaisie answered 29/11, 2018 at 6:12 Comment(1)
do you see changes when we do terraform plan? I am using the same param but I am not seeing any change when I change the source codeVolunteer
G
2

This worked for me -

  • add this line
source_hash     = "${data.archive_file.source.output_base64sha256}"

to s3 lambda bucket this will tell if any changes made.

  • then add this to lambda -
source_code_hash = "${data.archive_file.source.output_base64sha256}"

So your code should look like this -

resource "aws_s3_object" "lambda_object" {
  source_hash     = "${data.archive_file.source.output_base64sha256}"
  bucket          = "${aws_s3_bucket.s3.bucket}"
  key             = "${var.key}"
  source          = data.archive_file.source.output_path
}

resource "aws_lambda_function" "lambda_" {
  function_name   = "lambda_name"
  source_code_hash = "${data.archive_file.source.output_base64sha256}"
  .......
  .......
}

Worked for me. Best wishes.

Gemology answered 4/1, 2023 at 15:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.