简体   繁体   English

terraform 未检测到 lambda 源文件的更改

[英]terraform does not detect changes to lambda source files

In my main.tf I have the following:在我的main.tf中,我有以下内容:

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).问题是当我修改其中一个源文件时,比如lambda_script.py ,在新的terraform apply上,即使存档文件( lambda_resources_zip )得到更新, lambda 函数的脚本也没有得到更新(新的存档文件没有得到上传)。

I know that in order to avoid this, I could first run terraform destroy but that is not an option for my use case.我知道为了避免这种情况,我可以先运行terraform destroy但这不是我的用例的选项。

*I'm using Terraform v0.11.10 *我正在使用 Terraform v0.11.10

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. 修改源文件后,哈希值将更改并触发要更新的源文件。

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.s3 lambda bucket ,这将告诉您是否进行了任何更改。

  • then add this to lambda -然后将其添加到 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.最良好的祝愿。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM