ThomasReggi
ThomasReggi

Reputation: 59365

Print terraform template rendered output in terminal?

In the terraform docs it shows how to use a template. Is there any way to log this rendered output the console?

https://www.terraform.io/docs/configuration/interpolation.html#templates

data "template_file" "example" {
  template = "${hello} ${world}!"
  vars {
    hello = "goodnight"
    world = "moon"
  }
}

output "rendered" {
  value = "${template_file.example.rendered}"
}

Upvotes: 18

Views: 26479

Answers (5)

Mack
Mack

Reputation: 1

Note: If you specify the template as a literal string instead of loading a file, the inline template must use double dollar signs (like $${hello}).

https://www.terraform.io/language/configuration-0-11/interpolation#templates

This is how it worked:

data "template_file" "example" {
  template = "$${v1} $${v2}!"
  vars = {
    v1 = "hello"
    v2 = "world"
  }
}

output "rendered" {
  value = data.template_file.example.rendered

}

Outputs: rendered = "hello world!"

Upvotes: 0

wasserholz
wasserholz

Reputation: 2433

The best answer is from Davos

  • terraform state list to find the resource name
  • terraform state show <resource name>

This will work also, even when the rendered template is invalid json

terraform output rendered works only, if no errors occur.

Upvotes: 0

Siki shen
Siki shen

Reputation: 53

watch carefully, it's data not resource

data "template_file" "example" {
template = "${file("templates/greeting.tpl")}"
  vars {
  hello = "goodnight"
  world = "moon"
  }
}

output "rendered" {
  value = "${data.template_file.example.rendered}"
}

Upvotes: 5

Jan-Hendrik Boll
Jan-Hendrik Boll

Reputation: 11

Is that code perhaps part of a module? If it is part of a module it will not be displayed. You have to put the output of the module where the module is being invoked.

Upvotes: 1

ThomasReggi
ThomasReggi

Reputation: 59365

You need to run terraform apply then terraform output rendered

$ terraform apply
 template_file.example: Creating...
   rendered:   "" => "<computed>"
   template:   "" => "${hello} ${world}!"
   vars.#:     "" => "2"
   vars.hello: "" => "goodnight"
   vars.world: "" => "moon"
 template_file.example: Creation complete

 Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

 The state of your infrastructure has been saved to the path
 below. This state is required to modify and destroy your
 infrastructure, so keep it safe. To inspect the complete state
 use the `terraform show` command.

 State path: terraform.tfstate

 Outputs:

   rendered = goodnight moon!
 $ terraform output rendered
 goodnight moon!

Upvotes: 10

Related Questions