Nathaniel Ford
Nathaniel Ford

Reputation: 21220

Terraform var-file option does not work

I'm attempting to get a Terraform build to work utilizing a file for variables that I don't want in the source tree, but are required inputs.

My directory structure is pretty simple:

/nford   # Unix system user; echo $HOME -> /home/nford
  /deployment
    /prod
      /module_01
      variables.tf
      main.tf
      output.tf
  /secrets
    terraform.tfvars

All of the rest is from the /home/nford/deployment/prod/ directory.

When I execute terraform init it has no complaints, saying "Terraform has been successfully initialized!"

I have tried to execute all of the following, and a lot more:

$ terraform validate -var-file=~/secrets/terraform.tfvars
$ terraform validate -var-file=$HOME/secrets/terraform.tfvars
$ terraform validate -var-file=/home/nford/secrets/terraform.tfvars
$ terraform plan -var-file=~/secrets/terraform.tfvars
$ terraform plan -var-file=$HOME/secrets/terraform.tfvars
$ terraform plan -var-file=/home/nford/secrets/terraform.tfvars

Each of these produces the same result: it displays the usage of terraform <validate|plan>. It gives me no other errors. It doesn't appear to run terraform at all. I have tried moving the file into the same directory and executed:

$ terraform validate -var-file=terraform.tfvars
$ terraform validate -var-file=./terraform.tfvars
$ terraform plan -var-file=terraform.tfvars
$ terraform plan -var-file=./terraform.tfvars

These produce the same result: it displays usage. I've tried dropping the -var-file flag altogether and it reports:

Error: Required variable not set: pub_key
... etc (there are a number of these)

terraform plan -var "pub_key=${PUBLIC_KEY_LOC}" ... works without any issue.

I've looked around and I can't see anyone else that has remotely this same problem: everything is around the variables not being set correctly or the like. I am not sure how to diagnose this issue, given the utter lack of useful error message. What am I doing wrong here?

Upvotes: 1

Views: 4974

Answers (2)

Fernando Lugo
Fernando Lugo

Reputation: 36

Sometimes terraform files are not completely defined, for example... Let's say that you created a module with this:

module "example" {
   resource_name = var.resource_name
}

Then in your variables file you have this:

variable "resource_name" {
   description = "example resource name"
}

variable "resource_location" {
   description = "example resource location"
}

And you assign values on your .tfvars file:

resource_name = "resource-example-east"

If you haven't defined a value in your .tfvars file for "resource_location" variable then Terraform will ask you to put it manually after terraform apply, it don't cares if you are not using it.

In some cases people is using double quotes or quotes for files... the correct syntax is without quotes, please ensure that you are executing the command correctly:

terraform apply -var-file=myfile.tfvars

I hope someone finds this useful!

Upvotes: 1

David Przybilla
David Przybilla

Reputation: 828

Terraform output doesn't tell you that terraform.tfvars might contain incorrect syntax, instead showing the usage description. The expected syntax for a .tfvars file is a list of properly-escaped name="value" pairs, where the double quotes around the value is needed.

Upvotes: 1

Related Questions