Terraform Lambda source_code_hash使用相同的代码更新 [英] Terraform lambda source_code_hash update with same code

查看:122
本文介绍了Terraform Lambda source_code_hash使用相同的代码更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Terraform成功部署了AWS Lambda:

I have an AWS Lambda deployed successfully with Terraform:

resource "aws_lambda_function" "lambda" {
  filename                       = "dist/subscriber-lambda.zip"
  function_name                  = "test_get-code"
  role                           = <my_role>
  handler                        = "main.handler"
  timeout                        = 14
  reserved_concurrent_executions = 50
  memory_size                    = 128
  runtime                        = "python3.6"
  tags                           = <my map of tags>
  source_code_hash               = "${base64sha256(file("../modules/lambda/lambda-code/main.py"))}"
  kms_key_arn                    = <my_kms_arn>
  vpc_config {
    subnet_ids         = <my_list_of_private_subnets>
    security_group_ids = <my_list_of_security_groups>
  }
  environment {
    variables = {
      environment = "dev"
    }
  }
}

现在,当我运行terraform plan命令时,它说我的lambda资源需要更新,因为source_code_hash已更改,但是我没有更新lambda Python代码库(版本位于同一存储库的文件夹中) :

Now, when I run terraform plan command it says my lambda resource needs to be updated because the source_code_hash has changed, but I didn't update lambda Python codebase (which is versioned in a folder of the same repo):

  ~ module.app.module.lambda.aws_lambda_function.lambda
  last_modified:                     "2018-10-05T07:10:35.323+0000" => <computed>
  source_code_hash:                  "jd6U44lfe4124vR0VtyGiz45HFzDHCH7+yTBjvr400s=" => "JJIv/AQoPvpGIg01Ze/YRsteErqR0S6JsqKDNShz1w78"

我想是因为它每次都会压缩我的Python源,并且源会更改.如果Python代码没有变化,如何避免这种情况?如果我不更改Python代码库,我的假设是否连贯(我的意思是为什么哈希值会随之变化)?

I suppose it is because it compresses my Python sources each time and the source changes. How can I avoid that if there are no changes in the Python code? Is my hypothesis coherent if I didn't change the Python codebase (I mean, why then the hash changes)?

推荐答案

这是因为您只是对 main.py 进行哈希处理,但上传的是 dist/subscriber-lambda.zip . Terraform将哈希值与将文件上传到lambda时计算得出的哈希值进行比较.由于哈希是在两个不同的文件上完成的,因此最终会得到不同的哈希值.尝试在完全相同的文件上运行哈希.

This is because you are hashing just main.py but uploading dist/subscriber-lambda.zip. Terraform compares the hash to the hash it calculates when the file is uploaded to lambda. Since the hashing is done on two different files, you end up with different hashes. Try running the hash on the exact same file that is being uploaded.

这篇关于Terraform Lambda source_code_hash使用相同的代码更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆