我在 Terraform 内工作配置 AWS Lambda - 最初,我在 Terraform 目录中有一个 JS 文件,它是整个 lambda 函数:
data "archive_file" "auth_requests" {
type = "zip"
source_file = "${path.module}/auth_requests/index.js"
output_path = "${path.module}/auth_requests.zip"
}
resource "aws_lambda_function" "auth_requests" {
function_name = "auth_requests"
filename = "${data.archive_file.auth_requests.output_path}"
role = "${aws_iam_role.auth_requests.arn}"
handler = "index.handler"
source_code_hash = "${data.archive_file.auth_requests.output_base64sha256}"
runtime = "nodejs8.10"
}
但是,很明显,Lambda 函数应该获得自己的 git 存储库,而不是存在于我们更广泛的 Terraform 存储库中。有没有办法使用 Terraform 从 git repo 获取源文件(然后带入生成的存档)?
我可以定义 lambda 的 GitHub 存储库 as a resource ,但是接下来要克隆/更新它的步骤是什么,所以
archive_file
可以引用吗?或者可以将 Terraform 模块重新用于此类用途吗?
最佳答案
假设您使用 Github 存储库来存储您的 JS 函数,您可以使用 Github Content API使用 curl
下载 repo 的 zip 文件:
curl -L -o <output zip filename> https://<github repo url>/zipball/master
您可以使用
External
在 Terraform 中实现此目的。提供者而不是 Archive
提供者:data "external" "download_function" {
program = ["curl", "-L", "-o",
"${path.module}/auth_requests.zip", "${var.github_repo_url}"]
}
缺点是你现在有一个外部依赖(curl)。
关于git - 如何在 Terraform 中使用 git 存储库作为 AWS Lambda 源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51436429/