Terraform: "terraform template" command to render templates from state

Created on 1 Nov 2016  ยท  6Comments  ยท  Source: hashicorp/terraform

We have terraform show and terraform output for inspecting the current state in various ways, but it could be interesting to directly support rendering stuff from the current state into a template, to enable similar sorts of results as with consul-template querying consul:

$ cat foo.tmpl
{{range resource "aws_instance.foo"}}
server {{.id}} {{.private_ip}}:8080{{end}}
$ terraform template foo.tmpl
server i-12341234 10.1.2.1:8080
server i-98769876 10.1.2.2:8080

Precise template syntax is of course up for debate, but I was expecting there that the resource function is taking a resource address and returning all of the resource instances matching that address, which in this example was the two instances of a resource with count = 2.

Honestly it'd probably be useful to support such a thing inside Terraform too (a souped up template_file using Go template syntax?) but a command is probably easier to implement since template_file today, due to intentional limitations of the provider API, can't "see" enough of the Terraform state to implement something like the above.

cli enhancement

Most helpful comment

Hi all,

There is no active work on this right now, but we are starting to design various improvements to the configuration language that will likely include enhancements of Terraform's "template language" (really, its _interpolation_ language) that would make this sort of thing possible without introducing yet another template language into Terraform.

In the mean time, it's theoretically possible for such a thing to be implemented outside of Terraform by piping the result of terraform state pull into another program that can parse the JSON-based state format. The state format is not currently considered a stable interface with compatibility guarantees, but in practice it changes infrequently enough that it can be used in situations where people aren't averse to occasionally tweaking it as the state format changes in newer Terraform versions.

All 6 comments

I actually really like this idea. We have all the machinery to implement it.

It is much harder to implement within a resource, but I think not for the reasons you said above (but including, certainly): we have no way to verify that the interpolations are "valid" through the provider API from the core. Or... maybe we're saying the same thing.

There are a couple considerations here:

  1. I think we should use as much of the built-in Terraform templating as possible (expanding HIL perhaps).
  2. This must work with remote state, then it'd be ultimately powerful (imagine this running on a server pulling your state that you used from a local machine).

Nice! Yes must work with remote state. Can imagine some super cool things you could do with this for testing.

Any update on this feature?

Hi all,

There is no active work on this right now, but we are starting to design various improvements to the configuration language that will likely include enhancements of Terraform's "template language" (really, its _interpolation_ language) that would make this sort of thing possible without introducing yet another template language into Terraform.

In the mean time, it's theoretically possible for such a thing to be implemented outside of Terraform by piping the result of terraform state pull into another program that can parse the JSON-based state format. The state format is not currently considered a stable interface with compatibility guarantees, but in practice it changes infrequently enough that it can be used in situations where people aren't averse to occasionally tweaking it as the state format changes in newer Terraform versions.

Hi all!

While this was an interesting idea, and there are some use-cases that could be met with it, it seems like it hasn't been compelling enough to rise to the top of the priority pile.

Although not exactly the same as what was proposed here, it's possible to get a similar result of rendering a template to a file on disk from within terraform apply by using the local_file resource in conjunction with the templatefile function:

resource "local_file" "foo" {
    content     = templatefile("${path.module}/foo.txt.tmpl", {
      foo = "foo"
      bar = "bar"
    })
    filename = "${path.module}/foo.txt"
}

As of Terraform 0.12, the repetition and conditions are natively supported in the template language, so something like the example in the original comment here is easy to achieve and there are examples a lot like it in the template language documentation.

As part of some general issue gardening, we're going to close this out to represent that we're unlikely to implement it in the foreseeable future. If you need to render template files for integration with other software, consider something like the above idea to render the template as part of the terraform apply and write it to wherever it needs to go.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jszwedko picture jszwedko  ยท  77Comments

nevir picture nevir  ยท  82Comments

gwagner picture gwagner  ยท  81Comments

felnne picture felnne  ยท  133Comments

radeksimko picture radeksimko  ยท  80Comments