'Terraform outputs 'Error: Variables not allowed' when doing a plan
I've got a variable declared in my variables.tf like this:
variable "MyAmi" {
type = map(string)
}
but when I do:
terraform plan -var 'MyAmi=xxxx'
I get:
Error: Variables not allowed
on <value for var.MyAmi> line 1:
(source code not available)
Variables may not be used here.
Minimal code example:
test.tf
provider "aws" {
}
# S3
module "my-s3" {
source = "terraform-aws-modules/s3-bucket/aws"
bucket = "${var.MyAmi}-bucket"
}
variables.tf
variable "MyAmi" {
type = map(string)
}
terraform plan -var 'MyAmi=test'
Error: Variables not allowed
on <value for var.MyAmi> line 1:
(source code not available)
Variables may not be used here.
Any suggestions?
Solution 1:[1]
I see two things that could be causing the error you are seeing. Link to terraform plan documentation.
When running
terraform plan, it will automatically load any.tfvarsfiles in the current directory. If your.tfvarsfile is in another directory you must provide it as a-var-fileparameter. You say in your question that your variables are in a filevariables.tfwhich means theterraform plancommand will not automatically load that file. FIX: renamevariables.tftovariables.tfvarsWhen using the
-varparameter, you should ensure that what you are passing into it will be properly interpreted by HCL. If the variable you are trying to pass in is a map, then it needs to be parse-able as a map.
Instead of terraform plan -var 'MyAmi=xxxx' I would expect something more like terraform plan -var 'MyAmi={"us-east-1":"ami-123", "us-east-2":"ami-456"}'.
See this documentation for more on declaring variables and specifically passing them in via the command line.
Solution 2:[2]
This error can also occurs when trying to setup a variable's value from a dynamic resource (e.g: an output from a child module):
variable "some_arn" {
description = "Some description"
default = module.some_module.some_output # <--- Error: Variables not allowed
}
Using locals block instead of the variable will solve this issue:
locals {
some_arn = module.some_module.some_output
}
Solution 3:[3]
I had the same error, but in my case I forgot to enclose variable values inside quotes (" ") in my terraform.tfvars file.
This is logged as an issue on the official terraform repository here: https://github.com/hashicorp/terraform/issues/24391
Solution 4:[4]
I had the same issue, but my problem was the missing quotes around default value of the variable
variable "environment_name" {
description = "Enter Environment name"
default= test
}
This is how I resolved this issues,
variable "environment_name" {
description = "Enter Environment name"
default= "test"
}
Solution 5:[5]
Check the terraform version. I had something similar , the module was written on version 1.0 and I was using terraform version 0.12.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | rclement |
| Solution 2 | RtmY |
| Solution 3 | Ralph |
| Solution 4 | chetan mahajan |
| Solution 5 | ninohead |
