Terraform: -var-file= option not working

Created on 9 Mar 2017  路  11Comments  路  Source: hashicorp/terraform

Hi there,

Thank you for opening an issue. Please note that we try to keep the Terraform issue tracker reserved for bug reports and feature requests. For general usage questions, please see: https://www.terraform.io/community.html.

Terraform Version

Terraform v0.8.8

Affected Resource(s)

all - working with AWS

Does this -var-file= option work? My configuration only seems to read variables when named with the .tf extension. It would be much better if I can manage different configuration with different variable files. What is the Terraform approach to managing different configurations that require different variables? For example I would like to stand up VPC in all of our accounts using this but it seems like I have to make separate copies of the directory containing all of the .tf files to do this.

Thank you.

Most helpful comment

Hmm, looks like I may have misinterpreted the docs. Looks like if I do a var.tf with the following:

$ more vars.tf
variable "aws_region" {}
variable "shared_credentials_file" {}
variable "profile" {}
variable "ami" {}
variable "sshkey" {}
variable "subnet_id" {}

And then populate a hostname.tfvars file with the info such as:

aws_region = "us-west-1"
shared_credentials_file = "/my/creds/file/credentials"
profile = "myprofile"
ami = "ami-f173cc91"
sshkey = "mykey"
subnet_id = "subnet-"12312321
instance_type = "t2.micro"

Now this works as expected. Sorry for the misunderstanding.

All 11 comments

Hi @flowinh2o,

Can you show an example that's not working? A variable file should contain the key value pairs you want to set, and that same file can't be interpreted as a valid .tf config file.

See the "Variable Files" section in the docs for reference: https://www.terraform.io/docs/configuration/variables.html

@jbardin
Hi James. For example I have the following:

example.tf:
provider "aws" {
region = "${var.aws_region}"
shared_credentials_file = "${var.shared_credentials_file}"
profile = "${var.profile}"
}

resource "aws_instance" "example" {
ami = "ami-f173cc91"
instance_type = "t2.micro"
subnet_id = "subnet-12314142"
key_name = "mykey"
}

And another file vars.tfvars:

variable "aws_region" {
default = "us-west-1"
}

variable "shared_credentials_file" {
default = "/my/creds/file/credentials"
}

variable "profile" {
default = "myprofilename"
}

When I run this I get:

$ terraform plan -var-file=vars.tfvars
invalid value "vars.tfvars" for flag -var-file: multiple map declarations not supported for variables
Usage: terraform plan [options] [DIR-OR-PLAN]

Hmm, looks like I may have misinterpreted the docs. Looks like if I do a var.tf with the following:

$ more vars.tf
variable "aws_region" {}
variable "shared_credentials_file" {}
variable "profile" {}
variable "ami" {}
variable "sshkey" {}
variable "subnet_id" {}

And then populate a hostname.tfvars file with the info such as:

aws_region = "us-west-1"
shared_credentials_file = "/my/creds/file/credentials"
profile = "myprofile"
ami = "ami-f173cc91"
sshkey = "mykey"
subnet_id = "subnet-"12312321
instance_type = "t2.micro"

Now this works as expected. Sorry for the misunderstanding.

@jbardin On a related note how would you recommend managing multiple ec2-instances here. For example I have a bunch of different account and systems that I would like to like to use the same terraform scripts. For example in issue above what if I had another variables file example_machine2.vars and wanted to manage it seperately with the same terraform script. Would I have to create separate directories for each system to maintain the state files?

You don't need separate directories, you can specify the var file and state file with flags.

That said, fist class support for named state files are coming via Terraform "environments" in 0.9, which may help your use case somewhat.

@jbardin Got it. Thanks! Looking forward to that new feature in 0.9.

I seem to be running into this issue with TF v0.8.8

$聽make pre-render-ignition-yaml
# this it to avoid fuze failing because we have TF vars defined for paths in the file
cd infrastructure/templates/userdata && terraform apply -var-file=/Users/johnt/jvm-profiling-demo/infrastructure/demo_vars.tf.json -var-file=/Users/johnt/jvm-profiling-demo/infrastructure/deployment_vars.tf.json
module root: 4 error(s) occurred:

* output 'var_volume_path': unknown variable referenced: 'demo_instance'. define it with 'variable' blocks
* resource 'data.template_file.user-data' config: unknown variable referenced: 'demo_instance'. define it with 'variable' blocks
* resource 'data.template_file.user-data' config: unknown variable referenced: 'deployment'. define it with 'variable' blocks
* output 'data_volume_path': unknown variable referenced: 'demo_instance'. define it with 'variable' blocks
make: *** [pre-render-ignition-yaml] Error 1
$聽ls -l /Users/johnt/jvm-profiling-demo/infrastructure/demo_vars.tf.json /Users/johnt/jvm-profiling-demo/infrastructure/deployment_vars.tf.json
-rw------- 1 johnt staff 839 Mar 14 19:58 /Users/johnt/jvm-profiling-demo/infrastructure/demo_vars.tf.json
-rw------- 1 johnt staff 399 Mar 10 06:31 /Users/johnt/jvm-profiling-demo/infrastructure/deployment_vars.tf.json
$聽cp infrastructure/{deployment,demo}_vars.tf.json infrastructure/templates/userdata/
$聽make pre-render-ignition-yaml
# this it to avoid fuze failing because we have TF vars defined for paths in the file
cd infrastructure/templates/userdata && terraform apply -var-file=/Users/johnt/jvm-profiling-demo/infrastructure/demo_vars.tf.json -var-file=/Users/johnt/jvm-profiling-demo/infrastructure/deployment_vars.tf.json
data.template_file.ssh_private_key: Refreshing state...
data.template_file.password: Refreshing state...
data.template_file.ssh_public_key: Refreshing state...
data.template_file.user-data: Refreshing state...
null_resource.generator: Refreshing state... (ID: 7562233543287676521)

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

data_volume_path = /dev/xvde
file = /Users/johnt/jvm-profiling-demo/infrastructure/templates/userdata/ignition.yaml
var_volume_path = /dev/xvdd

@jbardin Can we re-open this item, or would you prefer I create a new issue and reference this one?

Hi @johnt337,

Yes, please open a new issue with the information to reproduce it. This issue was because of an invalid tfvars file, not a bug.

@jbardin OK thanks, will do now

hi i am new to terraform can anyone help me?

can i write variable of terraform in yaml file?? can terraform understand yaml?

Was this page helpful?
0 / 5 - 0 ratings