terraform.tfvars not being read

Created on 8 Jul 2015  ·  28Comments  ·  Source: hashicorp/terraform

Just bumped to v0.6.0 and seeing the following error when attempting to use the var-file=terrafrom.tfvars command line flag.

here are warnings and/or errors related to your configuration. Please
fix these before continuing.

Errors:

  * 1 error(s) occurred:

* module root: 3 error(s) occurred:

* resource 'aws_instance.terraform-test' config: unknown variable referenced: 'public-1a'. define it with 'variable' blocks
* resource 'aws_instance.terraform-test' config: unknown variable referenced: 'prod_internal'. define it with 'variable' blocks
* resource 'aws_instance.terraform-test' config: unknown variable referenced: 'prod_public'. define it with 'variable' blocks

Here is the relevant content of the terraform.tfvars file:

prod_public = "XXX"
prod_internal = "XXX"
public-1a = "XXX"

Here is the full command I am running:

terraform plan -var-file=./terraform.tfvars -input=false

I can get this working by creating variables blocks in main tf file but was hoping to abstract those variables out so that they could be reused.

bug core

Most helpful comment

You have to define the variable:

variable "access_key" {}

In your configuration

All 28 comments

+1

This is a major regression from the previous versions. terraform.tfvars should be read by default as previously.

Sorry for the confusing behavior, but this was actually a correction of a bug we'd been harboring for several previous versions. Variables need to be declared with a variable {} block somewhere in your config.

The terraform.tfvars file will still be read for variable values, but declarations must appear in config.

I have variables.tf file with all variable declarations in the same directory as the main *.tf. And behavior for me as described. I though all *.tf files were being read at the same time and then final map is constructed out of those individual files.

Or do we need to put:
variable "xxx" {}

block in the main *.tf file?

@alexintel oh interesting! Reopening and I'll try to reproduce and follow up with you.

You're right @alexintel. I have been scratching my head about this for a while and it seems like the variables need to be read in first. What confused me was that terraform was applying fine locally but when I pushed, Atlas wouldn't be able to find these variables... anyway, I hope this helps.

Hey folks,

Having trouble reproducing this... here'e my test case, perhaps you can point out what I'm missing:

// ==> main.tf <==
resource "aws_vpc" "foo" {
  cidr_block = "${var.cidr}"
}

// ==> terraform.tfvars <==
cidr = "10.11.0.0/16"

// ==> variables.tf <==
variable "cidr" {}

With the above (and AWS provider config via env vars), both terraform plan and terraform plan -var-file=./terraform.tfvars work just fine for me.

Try the following:

skip terraform.tfvars file completely, and in variables.tf try

// ==> variables.tf <==
variable "cidr" {
  default = "10.11.0.0/16"
}

Don't pass: -var-file=./terraform.tfvars on the command line. From what I understand from the documentation terraform.tfvars should be read automatically (and it is, in versions <=0.5.3).

I only use terraform.tfvars for things like AWS keys so I can exclude it from git.

@alexintel yep that works for me too...

// ==> main.tf <==
resource "aws_vpc" "foo" {
  cidr_block = "${var.cidr}"
}

// ==> variables.tf <==
variable "cidr" {
  default = "10.11.0.0/16"
}

Perhaps you're hitting #2613 - which was fixed in 0.6.1?

There hasn't been a response on this in quite awhile and I haven't heard of this happening recently in new issues either. Going to close as fixed, perhaps it was just fixed in between at some point. All mentioned issues here are fixed as well.

I am facing the same problem..
Even though i have a terraform.tfvars file on my directory.. Atlas does not "read" it.

GitRepo: https://github.com/gfisaris/iac-terraform-aws-full_stack_template
Variables: https://github.com/gfisaris/iac-terraform-aws-full_stack_template/blob/master/terraform-variables.tf
Predefined Variables Values: https://github.com/gfisaris/iac-terraform-aws-full_stack_template/blob/master/terraform.tfvars

Could please anyone assist ?

Same problem here. terraform.tfvars in the same directory as *.tf.

aws-vpc.tf looks like this:

provider "aws" {
  access_key  = "${var.access_key}"
  secret_key  = "${var.secret_key}"
  region      = "${var.region}"
}

And terraform.tfvars looks like this:

access_key = "ABC..."
secret_key = "123..."

Running terraform plan generates the following:

* provider config 'aws': unknown variable referenced: 'access_key'. define it with 'variable' blocks
* provider config 'aws': unknown variable referenced: 'secret_key'. define it with 'variable' blocks

You have to define the variable:

variable "access_key" {}

In your configuration

just to clarify what @mitchellh suggested above, because it works:

in your terraform.tfvars file, you can have the variables defined, using the key=value structure:

foo = "bar"

but you still need to define the variables in your configuration file, e.g. infra.tf. An example:

variable "foo" {}

resource "null_resource" "action1" {
  provisioner "local-exec" {
    command = "echo ${var.foo}"
  }
}

Thanks to both @mitchellh and @michalmikolajczyk :-)

Please tell me if this belongs somewhere else; I don't want to annoy or do the wrong thing here.

I'm new to terraform and trying it out for the first time, modeling my layout on the best-practices repo somewhat. I'm using terraform v0.8.2

I have a terraform.tfvars file in a directory alongside my provider config. Something like:

terraform/
terraform/providers/vcloud/lab/lab.tf
terraform/providers/vcloud/lab/terraform.tfvars
terraform/lab.tf

in terraform/lab.tf I'm including "terraform/providers/vcloud/lab" as a module source. When I run terraform get in the top level directory, I get an error that one of my required variables is unset, even though that variable is defined in the terraform.tfvars file. That variable is defined in the nested lab.tf file; it's a provider-specific setting that seemed to make sense to keep with the nested module.

To test to see if the terraform.tfvars file was being read, I added a default to the problem variable in the nested tf file in the module and re-ran. Now the error highlights another required variable being unset. So it really seems like the nested terraform.tfvars file isn't being read. Is this expected?

This bug is not fixed (as of v0.11.2) and looks like it has been around for awhile; I think ticket should be re-open but I will open another one to reference this one.

There are multiple ticket/reference of this issue in different manifestation, see:

15894

15733

This workaround is to use -var-file to explicitly state tfvars file

  • Still facing this problem:

    • Error: provider config 'aws': unknown variable referenced: 'aws_region; define it with a 'variable' block
  • I have a _terraform.tfvars_ with:

aws_region = "us-west-2"
  • And in _./main.tf_ have this:
variable "aws_region" {
    default = "us-west-2"
}

@anilybba: I am also, facing the same issue, did you find any work around for this ?

I also have this issue.

dev.tfvars:

aws_profile = "default"

variables.tf:

variable "aws_profile" {
  description = "AWS profile to use for access control"
}

main.tf:

provider "aws" {
  profile = "${var.aws_profile}"
  ...
}

terraform validate:

Error: Required variable not set: aws_profile

It is as though tfvars are not working for me at all.

Facing the same issue.

In my Variables.tf, I have

variable "aws_vpc_cidr_block" {
}

In my prod.tfvars, I have
aws_vpc_cidr_block = "172.35.0.0/16"

In my root.tf I have

module "vpc" {
source = "./vpc"
}

in my vpc/main.tf, I have

resource "aws_vpc" "production" {

cidr_block = "172.35.0.0/16"

cidr_block           = "${var.aws_vpc_cidr_block}"
enable_dns_hostnames = true
enable_dns_support   = true
instance_tenancy     = "default"

tags {
    "Environment" = "${terraform.workspace}"
    "Terraform" = "true"
    "Name" = "production"
}

}

Now when I do plan, I get the below error

➜ Terraform git:(master) ✗ terraform plan -var-file=prod.tfvars

Error: resource 'aws_vpc.production' config: unknown variable referenced: 'aws_vpc_cidr_block'; define it with a 'variable' block

My friend and I both reproduced this issue after following https://www.hashicorp.com/resources/hangout-terraform-azure-for-beginners

Is .tfvars deprecated?

My friend and I both reproduced this issue after following https://www.hashicorp.com/resources/hangout-terraform-azure-for-beginners

Is .tfvars deprecated?

@v6 Make sure you do 'terraform init'

Hi all,

Whatever problem you are seeing now is something different than what this bug was about because the codepaths in question have changed considerably since 0.6.0. Please open new issues and complete the bug report template in full so we can understand what is going on and whether there is a new bug to fix.

Was it ever fixed?

If so, new issue.

what about forcing it by passing in an argument when you run terraform, eg.

terraform plan -var-flle="myvariable.tfvar"
terraform appy -var-flle="myvariable.tfvar"

I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

franklinwise picture franklinwise  ·  3Comments

ketzacoatl picture ketzacoatl  ·  3Comments

carl-youngblood picture carl-youngblood  ·  3Comments

jrnt30 picture jrnt30  ·  3Comments

rjinski picture rjinski  ·  3Comments