Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global Variable Namespace #1742

Closed
wazoo opened this issue Apr 29, 2015 · 5 comments
Closed

Global Variable Namespace #1742

wazoo opened this issue Apr 29, 2015 · 5 comments

Comments

@wazoo
Copy link

wazoo commented Apr 29, 2015

Instead of passing variables in and out of modules it seems like it would be interesting if there was a global namespace, so I could do something like:

module "test_core" {
source = "./test"
subnet = "10.10.10.0/24"
vpc_id = "xxxxxxxxx"
}

Then in the module something maybe like:

resource "aws_subnet" "sub_public_a" {
  vpc_id                  = "${var.vpc_id}"
  availability_zone       = "us-west-1"
  cidr_block              = "${var.subnet}"
  map_public_ip_on_launch = false
  register_global = true
}

which might let me do this in another module:

resource "aws_instance" "is_test_n01" {
  .....
  subnet_id             = "${global.sub_public_a.id}"
  .....
}

This would save a lot of time and keep code modular, it would also make sure that you have some uniqueness globally. It also would be cool to be able to protect things from getting re-defined (or allow it), for example if I have a module that contains a firewall that re-defines the public subnet.

@wazoo
Copy link
Author

wazoo commented Apr 30, 2015

Thinking more about this, it might make more sense to just make a global variable scope option like such:

variable "sub_public_a_id" {
value = "${aws_subnet.sub_public_a.id}"
global = true
}

@phinze
Copy link
Contributor

phinze commented May 30, 2015

Hi @wazoo! This is a very interesting idea, but I think that ultimately it would be the wrong move for Terraform's modularity. We were intentional about our choice to force module inputs and outputs to be explicitly declared (via variable {} and output {}, respectively). While this does make for more verbose config, it has the benefit of making every module's dependencies very clear.

If it were possible to reference out to a global namespace on any line of config, it would mean the only way to be sure of a module's dependencies would be to read all its source code, which goes against the "black box of config" concept that modules were designed around.

I'm happy to chat more with you on this thread about the problems you're trying to solve with this idea to see if there's another strategy we can bring to bear - but in the meantime I'm going to close the issue because I don't believe a global namespace is the direction we'll take.

@phinze phinze closed this as completed May 30, 2015
@wazoo
Copy link
Author

wazoo commented May 31, 2015

That is a great point. I actually thing with the introduction of templates this solves my problem as I was having to use modules as templates previously (to build a prod/stage environment) but I think I can refactor now using templates and it will be perfect.

@jdblack
Copy link

jdblack commented Sep 28, 2016

@phinze I would be very happy to have a conversation with you on this issue! The current variable scoping for terraform has been very painful for us. We have a large number of cross dependencies, and it's just killing us....

@ghost
Copy link

ghost commented Apr 21, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Apr 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants