Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: source_code_hash not getting persisted to tf statefile - causes lambda deployment even if there is no change in etag #29921

Closed
iammukeshm opened this issue Mar 11, 2023 · 5 comments · Fixed by #31887
Labels
bug Addresses a defect in current functionality. service/lambda Issues and PRs that pertain to the lambda service. service/s3 Issues and PRs that pertain to the s3 service.
Milestone

Comments

@iammukeshm
Copy link

iammukeshm commented Mar 11, 2023

Terraform Core Version

1.3.9

AWS Provider Version

4.58.0

Affected Resource(s)

aws_lambda_function

Expected Behavior

Based on source_code_hash, lambda should not be re-deployed / changed if there is no difference in the etag or source_code_hash.

Actual Behavior

The source_code_hash isnt getting stored to the statefile for some reason. Because of this everytime I run a terraform plan or apply, the lambda gets re-deployed even if there is no new zip upload my s3 bucket.

Ran terraform apply 1st time.

Terraform will perform the following actions:

  # module.catalog_lambda.aws_lambda_function.lambda will be updated in-place
  ~ resource "aws_lambda_function" "lambda" {
        id                             = "catalog-lambda"
      ~ last_modified                  = "2023-03-11T04:53:26.000+0000" -> (known after apply)
      ~ source_code_hash               = "Wy8Y63xIwbF/VPCbNo3m374Sec6xepIwxqr1wdpAZ+Y=" -> "66121cb6a3e928951f671916547d622f"   
        tags                           = {}
        # (21 unchanged attributes hidden)

        # (3 unchanged blocks hidden)
    }

Ran terraform apply second time

Terraform will perform the following actions:

  # module.catalog_lambda.aws_lambda_function.lambda will be updated in-place
  ~ resource "aws_lambda_function" "lambda" {
        id                             = "catalog-lambda"
      ~ last_modified                  = "2023-03-11T04:53:46.000+0000" -> (known after apply)
      ~ source_code_hash               = "Wy8Y63xIwbF/VPCbNo3m374Sec6xepIwxqr1wdpAZ+Y=" -> "66121cb6a3e928951f671916547d622f"   
        tags                           = {}
        # (21 unchanged attributes hidden)

        # (3 unchanged blocks hidden)
    }

You can see that the source_code_hash is not getting persisted to the tf statefile.

Relevant Error/Panic Output Snippet

No response

Terraform Configuration Files

N/A

Steps to Reproduce

resource "aws_lambda_function" "lambda" {
  s3_bucket        = data.aws_s3_object.package.bucket
  s3_key           = data.aws_s3_object.package.key
  source_code_hash = data.aws_s3_object.package.etag
  function_name    = var.function_name
  role             = aws_iam_role.lambda_role.arn
  handler          = var.function_handler
  runtime          = var.runtime
  timeout          = var.timeout_in_seconds
  depends_on = [
    aws_iam_role_policy_attachment.lambda_policy_attachment,
    aws_iam_role.lambda_role,
    data.aws_s3_object.package
  ]

  environment {
    variables = {
      ASPNETCORE_ENVIRONMENT = "Development"
    }
  }
}

data "aws_s3_object" "package" {
  bucket = var.s3_bucket_name
  key    = var.s3_key
}

Debug Output

No response

Panic Output

No response

Important Factoids

No response

References

No response

Would you like to implement a fix?

None

@iammukeshm iammukeshm added bug Addresses a defect in current functionality. needs-triage Waiting for first response or review from a maintainer. labels Mar 11, 2023
@github-actions
Copy link

Community Note

Voting for Prioritization

  • Please vote on this issue by adding a 👍 reaction to the original post to help the community and maintainers prioritize this request.
  • Please see our prioritization guide for information on how we prioritize.
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request.

Volunteering to Work on This Issue

  • If you are interested in working on this issue, please leave a comment.
  • If this would be your first contribution, please review the contribution guide.

@github-actions github-actions bot added service/lambda Issues and PRs that pertain to the lambda service. service/s3 Issues and PRs that pertain to the s3 service. labels Mar 11, 2023
@iammukeshm iammukeshm changed the title [Bug]: [Bug]: source_code_hash not getting persisted to tf statefile - causes lambda deployment even if there is no change in etag Mar 11, 2023
@justinretzolk justinretzolk removed the needs-triage Waiting for first response or review from a maintainer. label Mar 14, 2023
@justinretzolk
Copy link
Member

Related: #7385
Related: #17884
Related: #25647

Potentially related: #30029

Copy link

This functionality has been released in v5.32.0 of the Terraform AWS Provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

@SalmaanPV
Copy link

I think the issue still persists, i have used the latest provider version 5.32.0. Still it's the same issue.Terraform re-plan or re-appy is causing the hash values change.
Expected output: No infra change
Actual output: hash value update
Why it's trying to update the source code hash?
Screenshot 2024-01-18 at 5 51 35 PM

Below is my source code:

Check if the object (zip file) exists in the S3 bucket

data "aws_s3_object" "lambda_zip" {
bucket = var.bucket_name
key = "${var.team_name}/${var.project_name}/${var.zip_file_name}.zip"
}

Create or update Lambda function based on the existence of the Lambda package in S3

resource "aws_lambda_function" "lambda_function" {
function_name = var.function_name
handler = var.handler
runtime = var.runtime
s3_bucket = var.bucket_name
s3_key = "${var.team_name}/${var.project_name}/${var.zip_file_name}.zip"
role = aws_iam_role.custom_lambda_execution_role.arn
timeout = 30
tags = var.tags

Configure Lambda to use VPC

vpc_config {
subnet_ids = var.vpc_subnet_ids
security_group_ids = var.vpc_security_group_ids
}

environment {
variables = var.environment_variables
}

Trigger an update only if the S3 object (zip file) content changes or Lambda doesn't exist

source_code_hash = data.aws_s3_object.lambda_zip != null ? data.aws_s3_object.lambda_zip.etag : null
}

Can someone help me out?

Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Addresses a defect in current functionality. service/lambda Issues and PRs that pertain to the lambda service. service/s3 Issues and PRs that pertain to the s3 service.
Projects
None yet
3 participants