terraform-provider-template icon indicating copy to clipboard operation
terraform-provider-template copied to clipboard

template_file needs the ability to use tfvars

Open hashibot opened this issue 7 years ago • 1 comments

This issue was originally opened by @digitalfiz as hashicorp/terraform#16863. It was migrated here as a result of the provider split. The original body of the issue is below.


For example right now we have a .json file for our task definitions and those task definitions have environment variables that change so we put them in the tfvars for each state.

The problem or more so the annoying thing is that we have to put the variable in the json file. Then make sure to put it in the vars for template_file and then also in the tfvars. So this exact same thing exists in 3 different places. Instead of 1 place for setting it and 1 place for using it.

It would be super useful if template_file had a flag or something to say just use whatever variables are available like all the other resources can do.

hashibot avatar Dec 11 '17 22:12 hashibot

An example of this would be like so:

myvars.tfvars

foo = "bar"

main.12.tf


locals {
  calculated_vars = {
    something = "calculated"
  }
}

resource "aws_ecs_task_definition" "service" {
  family                = "service"
  container_definitions = templatefile("mydef.json", local.calculated_vars, true)
                          # 3rd param would be "inherit variables", default: false
}

main.11.tf


locals {
  calculated_vars = {
    something = "calculated"
  }
}

data "template_file" "def" {
  template     = "${file("mydef.json")}"
  vars         = local.calculated_vars
  inherit_vars = true # default: false
}

resource "aws_ecs_task_definition" "service" {
  family                = "service"
  container_definitions = data.template_file.def.rendered
}

mydef.json (the var. prefix can be used to avoid collisions):

{
    "whatisfoo": "${var.foo}", 
    "whatiscalculated": "${something}"
}

digitalfiz avatar Dec 04 '19 18:12 digitalfiz