terraform icon indicating copy to clipboard operation
terraform copied to clipboard

misleading tfvars error message: "Variables not allowed"

Open steinybot opened this issue 5 years ago • 18 comments

Terraform Version

❯ terraform -v
Terraform v0.12.23

Terraform Configuration Files

variables.tf:

variable "foo" {
  type = string
}

terraform.tfvars:

foo = bar

Debug Output

https://gist.github.com/steinybot/6d6fed5c27d7eb919a1c939521d57c20

Crash Output

Expected Behavior

Running terraform plan should have read the variables from terraform.tfvars.

Actual Behavior

❯ terraform plan

Error: Variables not allowed

  on terraform.tfvars line 1:
   1: foo = bar

Variables may not be used here.


Error: No value for required variable

  on variables.tf line 1:
   1: variable "foo" {

The root module input variable "foo" is not set, and has no default value. Use
a -var or -var-file command line argument to provide a value for this
variable.

Steps to Reproduce

  1. terraform plan

Additional Context

References

steinybot avatar Mar 17 '20 05:03 steinybot

So the underlying issue is that I forgot to quote the value.

It should be:

foo = "bar"

not:

foo = bar

But this is a really terrible error message to get for this type of mistake.

steinybot avatar Mar 17 '20 05:03 steinybot

Same issue experienced here as well, posting my specific error to help future googlers (my output is slightly different due to me wrapping my config with Terragrunt):

Error: Variables not allowed

  on <value for var.ids> line 1:
  (source code not available)

Variables may not be used here.

Below is the variable definition:

variable "ids" {
  type = list(string)
}

The following produced the similar error as @steinybot

ids = "foo"

Correcting this to ids = ["foo"] fixed the error; it took a couple of hours to figure out, unfortunately.

calebtote avatar Mar 25 '21 15:03 calebtote

Experiencing this too when I try to pass input a file to plan. I'd expect this to be a bit more verbose. It was failing as I had not encapsulated a variable with quotes when passing a secret variable from CI/CD.

triwats avatar Apr 19 '21 16:04 triwats

I'm getting a similar error. I have provider "azurerm" { features {} } variable "aad_allowed_tenants" { type = list default = ["blah"] } output "tenantid" { value = var.aad_allowed_tenants[0] } If I run terraform plan on this, I get the expected output (blah).

but from commandline, I try to overwrite it using terraform plan -var='aad_allowed_tenants=["aasdfad"]' ╷ │ Error: Variables not allowed │ │ on line 1: │ (source code not available) │ │ Variables may not be used here. ╵

Just installed the latest version (1.0.0). same error.

marc-gmail-towersap avatar Jun 16 '21 00:06 marc-gmail-towersap

hah, this is a powershell problem. If I flip to bash, using the exact same terraform.exe, it works.

marc-gmail-towersap avatar Jun 16 '21 01:06 marc-gmail-towersap

I'm having problems with this using terratest. Bits of relevant code:

func TestTerraformHelloWorldExample(t *testing.T) {
	terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
		TerraformDir: "../examples",
		Upgrade:      true,
		VarFiles: []string{"test.tfvars"},
	})
}
variable "key1" {
  type = string
}

variable "key2" {
  type = string
}
key1=foo
key2=bar

tibbon avatar Jan 18 '22 20:01 tibbon

Truly confusing error message. Thanks for posting this issue, without it, it would of taken me a long time to figure out whats going on.

I got it by providing a list variable with following input: [name1,name2,name3] ...when its expecting: ["name1","name2","name3"]

pintoflager avatar Feb 16 '22 04:02 pintoflager

The same also happens when you forget to put quotes around a string default value like this:

variable "foo" {
    type = string
    default = bar
}

erpel avatar Mar 29 '22 13:03 erpel

I just hit this on Windows Terminal using a list variable as part of the command. Escaping the double quotes seemed to work:

terraform plan -var 'some_list=[\"blah1\", \"blah2\"\]'

micmania1 avatar Apr 07 '22 01:04 micmania1

I have the same problem for objects:

This is my variables.tf:

variable "smtp" {
  type = object({
    username = string
    port = string
    sender_address = string
    server_name = string
  })
  default = {
    username = ""
    port = ""
    sender_address = ""    
    server_name = ""
  }
}

And then when running:

terraform plan -var-file=environments/weu-dev.tfvars "-var=smtp={"username":"hej", "port":"1234", "sender_address":"prutprut.dk", "server_name":"facebookcom"}"

I just get this:

image

Funny thing is when I do it with another variable, that has the same structure, I don't get this error.

variable "sql_database" {
  type = object({
    create_environmental = bool
    optional_token = string
  })
  default = {
    create_environmental = false
    optional_token = ""
  }
}

Here I just use:

terraform plan -var-file=environments/weu-dev.tfvars "-var=sql_database={"create_environmental": true, "optional_token": "1123444"}"

And it works perfectly.

I can't see what the difference is, other than the names and the fact that one of the attributes are a boolean. But otherwise they are very alike, but the first one fails, while the last one doesn't.

What am I not seeing??

linak-lukasmendez avatar Feb 14 '23 09:02 linak-lukasmendez

I just hit this on Windows Terminal using a list variable as part of the command. Escaping the double quotes seemed to work:

terraform plan -var 'some_list=[\"blah1\", \"blah2\"\]'

this worked for me

rahulguptaft9 avatar Jul 23 '23 09:07 rahulguptaft9

I got the same issue by using Terraform Cloud, I have to mention that you must add "" when you add variables in Workspace variables, or it will report "Variables not allowed"! Must be very careful about adding Workspace variables.

drleowang avatar Oct 19 '23 17:10 drleowang

I came across this issue in Terraform Cloud too. Turns out I needed to enclose the string in quotes. The error message is misleading!

simoncpu avatar Oct 23 '23 19:10 simoncpu

Another +1 for issues with not putting quotes around the value in Terraform Cloud variable definition.

nick-holmquist avatar Nov 14 '23 16:11 nick-holmquist

Just hit this when setting variables via the cloud API. Without adding the double quotes to the values I kept getting this cryptic error message. Python snippet below:

 "variables": [
    {"key": "VULTR_PLAN", "value": f'"{VULTR_PLAN}"'},
    {"key": "VULTR_REGION", "value": f'"{VULTR_REGION}"'},
],

petersandersen avatar Dec 06 '23 17:12 petersandersen

Just hit this as well, turns out my variable type was wrong, a simple edit of "variable type is incorrect/cannot be accepted" will be much clearer!

Tiana125 avatar Mar 15 '24 14:03 Tiana125

I am also getting the same error.

I have two terraform files

I have two terraform files

list.tf

variable "users" {
  type = list
}

get_user_list.tf

output "get_user_list" {
  value = var.users
}

When i am running
terraform plan -var 'users=["ankit","gaurav","kapil"]'

It throwing below error

╷
│ Error: Variables not allowed
│
│   on <value for var.users> line 1:
│   (source code not available)
│
│ Variables may not be used here.
╵
╷
│ Error: Variables not allowed
│
│   on <value for var.users> line 1:
│   (source code not available)
│
│ Variables may not be used here.
╵
╷
│ Error: Variables not allowed
│
│   on <value for var.users> line 1:
│   (source code not available)
│
│ Variables may not be used here.
╵
╷
│ Error: No value for required variable
│
│   on list.tf line 1:
│    1: variable "users" {
│
│ The root module input variable "users" is not set, and has no default value. Use a -var or -var-file command line
│ argument to provide a value for this variable.

aniketpant1 avatar Jul 14 '24 11:07 aniketpant1

Same problem occurs when not putting spaces around the equals sign in tfvars file.

Fails:

name="Production"

Works:

name = "Production"

stuffaboutpete avatar Oct 20 '24 12:10 stuffaboutpete