gpt4 book ai didi

terraform - 如何将 .env 文件中的环境变量读入 terraform 脚本?

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

我正在使用 terraform 在 aws 上构建 lambda 函数。 terraform 脚本中用于上传 env var 的语法是:

resource "aws_lambda_function" "name_of_function" {

...

environment {
variables = {
foo = "bar"
}
}
}

现在我有一个 .env在我的仓库中使用一堆变量文件,例如 email='admin@example.com' ,并且我希望 terraform 从该文件中读取(其中一些)它们并将它们注入(inject)到空间中,以便上传环境变量并可供 lambda 函数使用。如何才能做到这一点?

最佳答案

这是一个纯 TerraForm-only 解决方案,解析 .env文件到本地 TerraForm map 中。

output "foo" {
value = { for tuple in regexall("(.*)=(.*)", file("/path/to/.env")) : tuple[0] => tuple[1] }
}
我已将其定义为通过 CLI 进行快速测试的输出,但您始终可以将其定义为本地或直接在参数上。

关于terraform - 如何将 .env 文件中的环境变量读入 terraform 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59789730/

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