boolean variables in terraform.tfvars file not being used main.tf, but string variables work

1,718 views
Skip to first unread message

RGB

unread,
Feb 25, 2018, 11:37:41 AM2/25/18
to Terraform
Good morning.  Is there something special about setting variables with boolean values in a terraform.tfvars file?  I have string variables set there the exact same way and they're working in main.tf, but for some reason the boolean variables aren't working.  However, when I set them in main.tf they work as expected.  I would much rather have these set in the terraform.tfvars file, so any help would be greatly appreciated.  I've tried searching for the answer everywhere and came up empty.  Thank you.

Boolean Vars Not Working:
=====================

terraform.tfvars:
  var1 = false
  var2 = true
  var3 = "string"

  module "mod1" {
     source = "./modules/mod1"
     var1 = "${var.var1}"  # Throws an "unknown variable referenced: 'var1'; define it with a 'variable' block
     var2 = "${var.var2}"  # Throws an "unknown variable referenced: 'var2'; define it with a 'variable' block
     var3 = "${var.var3}"  # works as expected



Boolean Vars Working:
==================

terraform.tfvars:
  var3 = "string"

  variable "var1" {default = false}
  variable "var2" {default = true}
  module "mod1" {
     source = "./modules/mod1"
     var1 = "${var.var1}"  # works as expected
     var2 = "${var.var2}"  # works as expected
     var3 = "${var.var3}"  # works as expected

Raju Dawadi

unread,
Feb 25, 2018, 11:57:09 AM2/25/18
to Terraform
Did you try keeping 'boolean' values inside double quote?

RGB

unread,
Feb 25, 2018, 12:19:11 PM2/25/18
to Terraform
Hi Raju.  Thanks for the response.  I tried that as soon as I read your post, but it resulted in the same errors.

-Ryan

David Adams

unread,
Feb 25, 2018, 12:31:27 PM2/25/18
to terrafo...@googlegroups.com
You have to declare every variable in a tf file. Nothing is implicitly allowed to be made a variable.

I wasn't able to get your example to fail in the way you describe. They both complain about var3 being undeclared as well. You must have var3 declared somewhere else in your tf files. If you don't want to declare a default, you don't have to, but you do have to declare each variable. This is how it should work:

terraform.tfvars:
  var1 = false
  var2 = true
  var3 = "string"

  variable "var1" {}
  variable "var2" {}
  variable "var3" {}
  module "mod1" {
    source = "./modules/mod1"
    var1 = "${var.var1}"
    var2 = "${var.var2}"
    var3 = "${var.var3}"
  }



--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/b264a547-3bad-4dcf-ac57-aad43a6c2a58%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

RGB

unread,
Feb 26, 2018, 9:26:40 AM2/26/18
to Terraform
Thank you both for your help.  David was right; I accidentally had both a 'terraform.tfvars' and a 'variables.tf' in that directory; 'variables.tf' had the booleans and 'terraform.tfvars' didn't.  Oops. :)  That actually brings me to another question though.  I intended to use the 'terraform.tfvars' file, but it looks like that wasn't the one picked up automatically so I just switched everything over to the 'variables.tf' file.  I'm still not clear on when/why one file is used instead of the other (even after reading the docs).  Any insight on this?  Sorry for the add-on question.

-Ryan 
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.

marcell...@ocyan.com

unread,
Apr 3, 2019, 10:10:30 AM4/3/19
to Terraform
> I'm still not clear on when/why one file is used instead of the other (even after reading the docs).  Any insight on this?

I was under the impression that files were parsed according to their order in the directory (lexicographic order ascending).
Reply all
Reply to author
Forward
0 new messages