Hello. I'm trying to follow this guide https://www.terraform.io/intro/getting-started/variables.html and seem to be hitting wall.
terraform -v
Terraform v0.9.1
variables.tf
variable "aws_access_key" {}
variable "aws_secret_key" {}
terraform.tfvars
aws_access_key = "MYKEY"
aws_secret_key = "MYSECRET"
terraform.tf
provider "aws" {
access_key = "${var.aws_access_key}"
secret_key = "${var.aws_secret_key}"
region = "us-east-2"
}
resource "aws_instance" "example" {
ami = "ami-0d729a60"
instance_type = "t2.micro"
}
The docs say "If a terraform.tfvars file is present in the current directory, Terraform automatically loads it to populate variables" Also " If you run terraform plan now, Terraform will prompt you for the values for unset string variables." Therefore, it seems like it should just run the command without errors.
terraform plan
Error asking for user input: 1 error(s) occurred:
* provider.aws: 1:3: unknown variable accessed: var.aws_access_key in:
${var.aws_access_key}
https://www.terraform.io/intro/getting-started/variables.html
Hey @seenickcode
Taking a look at the code, the variables are being defined (and used) as aws_access_key / aws_secret_key - but are set in the variable file without the aws_ prefix. If you update the variables in terraform.tfvars to include the aws_ prefix - I believe it should start working as intended, as otherwise the code looks good :)
Can you confirm if adding the aws_ prefix solves your issue? :)
Thanks!
Looks I made a mistake regarding my original post. I actually had the vars the correct name. I just edited my post. It seems like it's still a problem:
terraform plan
Error asking for user input: 1 error(s) occurred:
* provider.aws: 1:3: unknown variable accessed: var.aws_access_key in:
${var.aws_access_key}
same problem. Also no luck with passing a variable with '-var' flag
terraform plan -var 'aws_region=us-west-2'
Error asking for user input: 1 error(s) occurred:
* provider.aws: 1:3: unknown variable accessed: var.aws_region in:
${var.aws_region}
@seenickcode I ran terraform plan against your files without any changes and its working fine.
@seenickcode, I've also been unable to reproduce the error you're seeing. However, I fully believe that the error is real and potentially annoying.
In order to fully dig into the problem you're seeing, would you mind pasting the output of the following commands (either here or in a gist), omitting any secrets of course?
ll -a
cat ./*
Thanks!
renamed variable aws_region to be region and everything works as expected.
It looks like the cause is aws_ prefix. When it is used in a variable names this error occurs.
@niwza, variables can be named with an aws prefix. The attribute fields inside the provider configuration cannot be changed however.
See:
$ cat main.tf
provider "aws" {
region = "${var.aws_region}"
}
output "foo" { value = "bar" }
output "region" { value = "${var.aws_region}" }
$ cat vars.tf
variable "aws_region" { }
md5-615c8af00e5515232aef95f142cf747c
$ cat terraform.tfvars
aws_region = "us-west-2"
md5-615c8af00e5515232aef95f142cf747c
$ terraform apply
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
foo = bar
region = us-west-2
Using the variable name aws_region works. However, the provider configuration expects the attribute region.
I'm seeing the same error with version 9.2. The executable seems to completely ignore the tfvars file and all variables input via commandline.
Just a sidenote, in my case, this happens with all variables, not just ones prefixed with aws.
Hi @volkanunsal, would you be able to run an ls from the current working directory where you're running terraform? Also any variable declarations that you have in your terraform configs, and any error output would be great.
For instance, I have the following config:
variable "foo" { }
output "foo" { value = "${var.foo}"}
And the following terraform.tfvars file in the same directory:
foo = "bar"
And running terraform apply yields the correct output:
$ terraform apply
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
foo = bar
The same holds true if I supply a variable on the command line:
$ terraform apply -var foo=baz
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
foo = baz
There could definitely be a real issue here, however. Just attempting to eliminate any common errors before diving in further. Thanks!
I am also experiencing the same problem:
Microsoft Windows [Version 10.0.14393]
Terraform v0.9.3
example.tf
---------------------------------------------
provider "vsphere" {
user = "${var.user}"
password = "password"
vcenter_server = "server"
allow_unverified_ssl = true
}
resource "vsphere_folder" "frontend" {
path = "frontend"
datacenter = "datacenter"
}
md5-615c8af00e5515232aef95f142cf747c
example.tfvars
---------------------------------------------
user = "username"
md5-615c8af00e5515232aef95f142cf747c
Command
---------------------------------------------
D:\terraform\example> terraform plan -var-file="example.tfvars" -out=example.plan && terraform apply
md5-615c8af00e5515232aef95f142cf747c
Output
---------------------------------------------
Error asking for user input: 1 error(s) occurred:
* provider.vsphere: 1:3: unknown variable accessed: var.user in:
${var.user}
Same here, I want to define a default name if var is not set for example:
name = "${var.stack == "" ? "fabrik": format("fabrik-%s", var.stack)}"
Using: terraform plan -var 'stack=test' or only terraform plan and get in both cases:
1:3: unknown variable accessed: var.stack in
Terraform version: Terraform v0.10.0
When using workspaces works as expected, example:
name = "${terraform.workspace == "default" ? "fabrik": format("fabrik-%s", terraform.workspace)}"
The way is working for me is to create a variable block, something like:
variable "stack" {
default = ""
}
@Peter-J-Anderson apologies for the delayed response here, missed this!
As noted by @nbari - a variable can only be used (or specified) when there's a matching variable block. In this case you should be able to create the following:
variable "user" {}
.. which will allow it to be accessed via var.user and specified in a *.tfvars file / in the command line. Further information can be found on the Configuring Variables page.
Thanks!
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.
Most helpful comment
@Peter-J-Anderson apologies for the delayed response here, missed this!
As noted by @nbari - a variable can only be used (or specified) when there's a matching variable block. In this case you should be able to create the following:
.. which will allow it to be accessed via
var.userand specified in a*.tfvarsfile / in the command line. Further information can be found on the Configuring Variables page.Thanks!