Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform outputs 'Error: Variables not allowed' when doing a plan

Tags:

terraform

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?

like image 905
Snowcrash Avatar asked Nov 05 '19 14:11

Snowcrash


2 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
}
like image 140
RtmY Avatar answered Oct 22 '22 21:10

RtmY


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

like image 41
Ralph Avatar answered Oct 22 '22 20:10

Ralph