Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terraform.tfvars not being read #2659

Closed
jmreicha opened this issue Jul 8, 2015 · 28 comments
Closed

terraform.tfvars not being read #2659

jmreicha opened this issue Jul 8, 2015 · 28 comments

Comments

@jmreicha
Copy link

jmreicha commented Jul 8, 2015

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.

@alexintel
Copy link

+1

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

@phinze
Copy link
Contributor

phinze commented Jul 10, 2015

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.

@phinze phinze closed this as completed Jul 10, 2015
@alexintel
Copy link

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?

@phinze
Copy link
Contributor

phinze commented Jul 14, 2015

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

@phinze phinze reopened this Jul 14, 2015
@catsby catsby added the core label Jul 14, 2015
@jayjhan8
Copy link

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.

@phinze
Copy link
Contributor

phinze commented Jul 27, 2015

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.

@alexintel
Copy link

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.

@phinze
Copy link
Contributor

phinze commented Jul 27, 2015

@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?

@mitchellh
Copy link
Contributor

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.

@gfysaris
Copy link

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 ?

@jmvbxx
Copy link

jmvbxx commented Oct 27, 2016

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

@mitchellh
Copy link
Contributor

You have to define the variable:

variable "access_key" {}

In your configuration

@michalmikolajczyk
Copy link

michalmikolajczyk commented Nov 28, 2016

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}"
  }
}

@jmvbxx
Copy link

jmvbxx commented Dec 10, 2016

Thanks to both @mitchellh and @michalmikolajczyk :-)

@butterflysky
Copy link

butterflysky commented Jan 27, 2017

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?

@AnthonyWC
Copy link

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

@DavidCardoso
Copy link

  • 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"
}

@UtkarshYeolekar
Copy link

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

@rjurney
Copy link

rjurney commented Jun 28, 2018

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.

@ashish235
Copy link

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

@v6
Copy link
Contributor

v6 commented Mar 10, 2019

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

Is .tfvars deprecated?

@v6
Copy link
Contributor

v6 commented Mar 12, 2019

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

Is .tfvars deprecated?

@rajetta
Copy link

rajetta commented Mar 12, 2019

@v6 Make sure you do 'terraform init'

@apparentlymart
Copy link
Contributor

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.

@v6
Copy link
Contributor

v6 commented Mar 13, 2019

Was it ever fixed?

@v6
Copy link
Contributor

v6 commented Mar 14, 2019

If so, new issue.

@appcoreopc
Copy link

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"

@ghost
Copy link

ghost commented Jul 24, 2019

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.

@ghost ghost locked and limited conversation to collaborators Jul 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests