Using Environment Variable

I am using vsphere as my provider.

In order to abstract the username and password, I want to use the Environment Variable. I have mentioned my environment variable as TF_VAR_username and TF_VAR_password in my env variable file and I can echo it in the terminal.

How would I use the variable in the .tf file?

export TF_VAR_username=‘xxxx’
export TF_VAR_password=‘xxxx’
export TF_VAR_IP = ‘x.x.x.x’

provider “vsphere” {
user = “{var.username}" password = "{var.password}”
vsphere_server = “${var.ip}”

allow_unverified_ssl = true

When I plan I get the following error:

Error: Reference to undeclared input variable

And if I declare the empty variable it ask for the value in the Terminal. How is the community using the ENV variable in the .tf FIle?

It looks like it may be a variable capitalization issue.

Your shell command export TF_VAR_IP = ‘x.x.x.x’ sets the terraform variable with the name IP

But the terraform code is looking for a variable ip (lower case):
vsphere_server = “${var.ip}”

Also note, if you’re using terraform >= 0.12, in most cases you can reference variables without “${}”, for example vsphere_server = var.ip.

I hope this helps.

1 Like

As @robertpeteuil noted, the variable names are case sensitive for matching with the environment variables, so it’s important to use consistent case.

The other missing part here is that Terraform requires you to declare the input variables you intend to use. You can declare these three like this:

variable "username" {
  type = string

variable "password" {
  type = string

variable "ip" {
  type = string

Then be sure to set them all with the same case in the environment variables, TF_VAR_username, TF_VAR_password, and TF_VAR_ip.

With that said, note that the vSphere provider already has built-in support for three environment variables you can use to set these values without any additional configuration at all:

export VSPHERE_USER=xxxx
export VSPHERE_SERVER=x.x.x.x

Usually it’s preferable to set the credentials for a provider via its environment variables but to set the location for a provider (the vsphere_server argument in this case) via the configuration directly. That’s because the credentials describe who is running Terraform, and thus it makes sense for them to differ from one run to the next, while the vsphere_server argument is describing what Terraform is managing, and that sort of information usually belongs in our version-controlled configuration.

While there are certainly situations where dynamically setting the IP address is reasonable, for most cases I’d recommend giving the vSphere server a stable DNS name that describes its purpose and then setting it explicitly in the configuration, rather than via a variable, so that anyone else applying this configuration can see which server it’s intended to apply to, and thus remove the risk of mistakenly applying it to the wrong server:

provider "vsphere" {
  # credential arguments should be omitted when
  # setting them via environment variables.

  server = ""
1 Like

I did tried all the above things, nothing worked.

The issue was that I am running terraform as an container, mounting my PWD. I did not pass the ENV variable to the Docker shell, so it was not able to find it.

Thank you for your help.