Vikram
Vikram

Reputation: 33

AWS Lambda not updating using Terraform source_code_hash property

I have a Terraform configuration for creating a Lambda resource and am using the source_code_hash property to detect changes to the zip. I am also uploading a separate file that contains the SHA256 hash of the file along with the zip file to S3.

I am able to do the deploy once, but then the problem is that the running Lambda is not getting updated after I update the zip and in the build log I am seeing "Still creating..."

How can I see the value of the source_code_hash property? I just see + source_code_hash = (known after apply) both in the plan output and the apply output so I don't know if the value is getting updated or not.

My code is below:

data "aws_s3_object" "source_hash" {
  bucket = "dap-bucket-2"
  key    = "lambda.zip.sha256"
}

resource "aws_lambda_function" "lambda" { 
  function_name    = "lambda_function_name"
  s3_bucket        = "dap-bucket-2"
  s3_key           = "lambda.zip"
  handler = "template.handleRequest"
  runtime = "java11"
  role    = aws_iam_role.lambda_exec.arn
  source_code_hash = "${data.aws_s3_object.source_hash.body}"
  publish = true
} 

Upvotes: 1

Views: 1832

Answers (1)

Marcin
Marcin

Reputation: 238597

For s3 objects, ususually you would use etag:

source_code_hash = data.aws_s3_object.source_hash.etag

Upvotes: 3

Related Questions