Is it possible to modify the data generated via templatefile function

We have this terraform project

# config.tpl
instance_name = "${name}"
environment = "${env}"
fullname="fullname"
# main.tf

variable "instance_name" {
  default = "my-instance"
}

variable "environment" {
  default = "staging"
}

data "template_file" "example" {
  template = file("${path.module}/config.tpl")

  vars = {
    name = var.instance_name
    env  = var.environment
  }
}

output "modified_template" {
  value = templatestring(data.template_file.example.rendered, {
    fullname = "new-value"
  })
}

terraform {
}

Is there any way to make this work. Tried templatestring but its not working. I need to update the fullname attribute later in the project as I can’t update that while using templatefile function.

Thanks

Hi @smartaquarius10,

If you wanted to do something like this you need to escape the interpolation sequence in the original template:

# config.tpl
instance_name = "${name}"
environment = "${env}"
fullname = "$${fullname}"

This way fullname is rendered as "${fullname}" after the initial template (which you can do with templatefile, there is no need for the deprecated data source).

2 Likes

Thank you so much… It worked :slight_smile: