如何在 Hashicorp Terraform 中配置环境变量 [英] How to configure environment variables in Hashicorp Terraform
问题描述
我对 Terraform 还是很陌生,尽管我已经浏览了 Hashicorp 网站上提供的所有教学模块.
I'm quite new to Terraform, though I have gone through all of the instructional modules available on Hashicorp's site.
目前,我正在努力理解如何设置环境变量.我知道如何在 main.tf 配置 (access_key = "${var.access_key}"
) 中引用变量,并且我知道如何将该访问密钥保存到一个单独的文件并引用它,但是我不明白(也找不到任何文档/说明)是如何设置环境变量,这样我就不必将访问密钥保存到文件中.
Currently, I'm struggling with understanding how to set up environment variables. I know how to reference variables in the main.tf config (access_key = "${var.access_key}"
), and I know how to save that access key to a separate file and reference that, but what I don't understand (and can't find any documentation/instruction on) is how to set up environment variables so I don't have to save the access key to a file.
有谁知道如何最好地去做这件事?
Does anyone know how best to go about doing this?
推荐答案
Terraform 可以为 AWS 推断以下环境变量
Terraform can infer the following environment variables for AWS
export AWS_ACCESS_KEY_ID="anaccesskey"
export AWS_SECRET_ACCESS_KEY="asecretkey"
参考:https://www.terraform.io/docs/providers/aws/#environment-variables
但我建议尝试AWS Profile
.您可以将凭据添加到 ~/.aws/credentials
文件中,例如
But I would suggest trying the AWS Profile
.
You can add credentials to ~/.aws/credentials
file like
[myprofile]
aws_access_key_id = anaccesskey
aws_secret_access_key = asecretkey
然后你可以设置环境变量export AWS_PROFILE=myprofile
.现在,如果您从此 shell 运行 terraform,它应该选择 myprofile
下列出的凭据.
and then you can set environment variable export AWS_PROFILE=myprofile
. Now, if you run terraform from this shell, it should pick credentials listed under myprofile
.
此外,您可以让您的 AWS Provider
代码如下:
Also, you can have you AWS Provider
code as follows:
provider "aws" {
profile = "myprofile"
region = "${var.region}"
}
根据我的经验,使用 profile
与 AWS 交互比在每个 shell 上设置环境变量更容易且更好.
In my experience, interacting with AWS using profile
is easy and better than setting environment variables on each shell.
您可以在此处参考示例 https://github.com/pradeepbhadani/tf-examples/blob/master/ex2/provider.tf
You can refer an example here https://github.com/pradeepbhadani/tf-examples/blob/master/ex2/provider.tf
希望这会有所帮助.
这篇关于如何在 Hashicorp Terraform 中配置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!