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

Variable interpolation syntax is confounding in non-trivial cases #744

Closed
bitglue opened this issue Jan 6, 2015 · 6 comments · Fixed by #785
Closed

Variable interpolation syntax is confounding in non-trivial cases #744

bitglue opened this issue Jan 6, 2015 · 6 comments · Fixed by #785

Comments

@bitglue
Copy link

bitglue commented Jan 6, 2015

Nowhere in a the documentation can I find a formal or comprehensive description of the variable interpolation syntax. There are lots of trivial examples which just expand one variable:

vpc_id = "${aws_vpc.vpc.id}"

Cool. What if I want to do something nontrivial? How about reading from a file. There's a function for that, and the documentation even gives an example: ${file("path.txt")}. Neat. But now, I want to include that in my program, where it's already quoted. So, is that:

user_data = "${file("user_data.txt")}"

or maybe...

user_data = ${file("user_data.txt")}

or...

user_data = "${file(\"user_data.txt\")}"

OK whatever, I'll just try them all until I find something that works. But hey, this is a module, and I want that to be relative to the module directory. There's an example:

script = "${path.module}/script.sh"

Neat...but I also want to use the file function. So is that:

user_data = "${file(${path.module}\"/user_data.txt\")}"

or maybe...

user_data = "${file(path.module\"/user_data.txt\")}"

Maybe something with that join function? No, that only works with the splat syntax.

Oh jeez. I could think of a lot of ways it might be. I'm not sure if it's even possible.

The mental problem I'm having here is that a variable reference is not a string, and a function is not a variable, and " isn't a "begin interpretation of half-baked programming language" operator, but the Terraform syntax seems to conflate all of these things. It's very reminiscent of Bash, which has taken me years to understand, and which people mess up all the time. Please don't be like Bash, which has pages and pages in the manual on expansion because it's so hard to understand.

@armon
Copy link
Member

armon commented Jan 6, 2015

@bitglue This is definitely something we are aware of and know needs re-thinking. For the initial TF release it was largely for variable references, with a few special cased functions. It has grown into a more general purpose "mini-language" but the implementation has not caught up. It currently uses some fairly bespoke regex and parser evaluators.

Our goal is to replace it with a more formally defined language (backwards compatible mostly) and use a proper lexer / parser and AST. That should fix most of the bizarre parsing errors and provide a more formal definition of the language.

Bare with us, as TF is a very young project still :)

@mitchellh
Copy link
Contributor

Note that we just opened #785 which makes the interpolation a lot more well defined, meaning you'll get nicer errors in a lot of cases. It is also now typed, so you'll get type errors in cases that don't make sense. So your one example is now like this:

user_data = "${file("${path.module}/user_data.txt")}"

@bitglue
Copy link
Author

bitglue commented Jan 15, 2015

I'm having some backwards compatibility issues. Here's an example of deploying CoreOS which worked with the v0.3.5 release, but does not work with this commit. terraform plan gives me

Errors:

  * module root: 3 error(s) occurred:

* output 'ip': multi-variable must be in a slice
* resource 'aws_instance.coreos' config: multi-variable must be in a slice
* resource 'aws_route_table_association.public' config: multi-variable must be in a slice

Not sure what a "multi-variable" is, but I guess it originates from the glob syntax? Shouldn't join and element functions un-multi a multi-variable?

@mitchellh
Copy link
Contributor

@bitglue That was just fixed this morning.

@bitglue
Copy link
Author

bitglue commented Jan 15, 2015

Oh sorry...should have updated first. Now I'm getting a crash. I'll see if I can reduce the reproduction case and open a new issue.

@ghost
Copy link

ghost commented May 4, 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 May 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants