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?

1

5 Answers

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

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:

I see two things that could be causing the error you are seeing. Link to terraform plan documentation.

  1. When running terraform plan, it will automatically load any .tfvars files in the current directory. If your .tfvars file is in another directory you must provide it as a -var-file parameter. You say in your question that your variables are in a file variables.tf which means the terraform plan command will not automatically load that file. FIX: rename variables.tf to variables.tfvars

  2. When using the -var parameter, 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.

1

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

Check the terraform version. I had something similar , the module was written on version 1.0 and I was using terraform version 0.12.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like