gpt4 book ai didi

amazon-web-services - 如何触发Terraform上传新的Lambda代码

转载 作者:行者123 更新时间:2023-12-04 14:23:06 25 4
gpt4 key购买 nike

我按如下方式使用Terraform部署lambda,但存在以下问题:

1)我希望始终调用null_resource.lambda或更改stop_ec2.py时调用它,以便stop_ec2_upload.zip不会过时。我应该在触发器{}中写些什么?

2)更改stop_ec2_upload.zip时,如何使aws_lambda_function.stop_ec2将新的stop_ec2_upload.zip更新到云?

现在,我必须销毁aws_lambda_function.stop_ec2,然后再次创建它。我可以在代码中写什么,以便当我运行terraform apply时,1)和2)会自动发生吗?

resource "null_resource" "lambda" {
triggers {
#what should I write here?
}

provisioner "local-exec" {
command = "mkdir -p lambda_func && cd lambda_py && zip
../lambda_func/stop_ec2_upload.zip stop_ec2.py && cd .."
}
}
resource "aws_lambda_function" "stop_ec2" {
depends_on = ["null_resource.lambda"]
function_name = "stopEC2"
handler = "stop_ec2.handler"
runtime = "python3.6"
filename = "lambda_func/stop_ec2_upload.zip"
source_code_hash =
"${base64sha256(file("lambda_func/stop_ec2_upload.zip"))}"
role = "..."
}

最佳答案

我阅读了Chandan提供的链接,并弄清楚了。
这是我的代码,它可以完美运行。

实际上,有了“archive_file”和source_code_hash,我不需要触发器。每当我创建一个新文件stop_ec2.py或对其进行修改时。当我运行terraform时,文件将被重新压缩并上传到云中。

data "archive_file" "stop_ec2" {
type = "zip"
source_file = "src_dir/stop_ec2.py"
output_path = "dest_dir/stop_ec2_upload.zip"
}

resource "aws_lambda_function" "stop_ec2" {
function_name = "stopEC2"
handler = "stop_ec2.handler"
runtime = "python3.6"
filename = "dest_dir/stop_ec2_upload.zip"
source_code_hash =
"${data.archive_file.stop_ec2.output_base64sha256}"
role = "..."
}

关于amazon-web-services - 如何触发Terraform上传新的Lambda代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48577727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com