How to "override" variables when using modules?

5,415 views
Skip to first unread message

bth...@elementum.com

unread,
Sep 12, 2017, 2:08:56 PM9/12/17
to Terraform
I've seen a few other posts on this topic but none that seem to address the issue specifically.

What I'm trying to do is use either 'terraform plan -var "foo=bar"' or set $TF_VAR_foo to "override" variables, which seems straightforward. However, with a simple setup involving modules this doesn't work at all.

Given these three files:

modules/foo/variables.tf

variable "region" {
  default = "us-east-1"
}

variable "itype" {
  default = "t2.micro"
}

modules/foo/example.tf

provider "aws" {
  region     = "${var.region}"
}

resource "aws_instance" "foo" {
  ami           = "ami-b374d5a6"
  instance_type = "${var.itype}"
  subnet_id     = "subnet-3ca6c952"
}

service/main.tf

module "example" {
  source = "../modules/foo"
  itype  = "c3.large"
}

Running "terraform plan" works as expected - the resource uses instance_type=c3.large. Also removing the "itype" line from main.tf also works as expected, setting instance_type to t2.micro, the variable's declared default.

What does not work as expected is trying to run, for example, 'terraform plan -var "itype=i2.xlarge"', nor setting TF_VAR_itype=i2.xlarge. The 'override' value is ignored. In fact, I can find no way to use modules and get envars or -var settings work at all - no matter what I do they're ignored. I don't understand why, and as a second issue, I can't figure out how to work around it. 

Has anyone gotten this to work successfully?

Philip Nelson

unread,
Sep 12, 2017, 3:01:00 PM9/12/17
to terrafo...@googlegroups.com
You just need to make the "c3.large" a variable in the root, i.e. corresponding to main.tf. passing -var only overrides variables in the root configuration. Good luck!

Legal Disclaimer: The information contained in this message may be privileged and confidential. It is intended to be read only by the individual or entity to whom it is addressed or by their designee. If the reader of this message is not the intended recipient, you are on notice that any distribution of this message, in any form, is strictly prohibited. If you have received this message in error, please immediately notify the sender and delete or destroy any copy of this message

--
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/7cedbd0f-6101-4a88-afda-0bcd021811a1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

bth...@elementum.com

unread,
Sep 12, 2017, 3:15:15 PM9/12/17
to Terraform
Not sure I understand. When you say "only overrides variables in the root configuration" do you mean "only overrides variables declared in the root configuration"? Is there a reason why, or is it documented anywhere? https://www.terraform.io/docs/configuration/variables.html makes no mention of it or indeed any caveats at all.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.

Philip Nelson

unread,
Sep 12, 2017, 3:25:57 PM9/12/17
to terrafo...@googlegroups.com
without a deep search, check this out: https://github.com/hashicorp/terraform/issues/2648


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/d4eebb24-8c5f-45b4-a2bd-f46e61f8bed8%40googlegroups.com.

bth...@elementum.com

unread,
Sep 12, 2017, 4:29:10 PM9/12/17
to Terraform
Thanks, appreciate the pointer. 
Reply all
Reply to author
Forward
0 new messages