'Terraform throws an error "Error: invalid character 'w' looking for beginning of value"

i have some code in terraform

resource "vault_generic_secret" "endpoint" {
  count = var.elasticache-create ? 1 : 0
  path  = "platforms/${var.iad_platform}/${var.iad_region}/${var.iad_environment}/${var.iad_component}/elasticache_endpoint"
  data_json = data.template_file.es-endpoint.rendered
}


data "template_file" "es-endpoint" {
  template = file("templates/endpoint.tpl")

  vars = {
    elasticache_endpoint = aws_elasticache_replication_group.this[0].configuration_endpoint_address
  }
}

but when a apply that it says . i think the issue is when terraform parsing the json file, but still don't know what is wrong

data.template_file.es-endpoint: Refreshing state...

Error: invalid character 'w' looking for beginning of value

  on elasticache.tf line 88, in resource "vault_generic_secret" "endpoint":
  88: resource "vault_generic_secret" "endpoint" {

endpoint.tpl

{
  "ELASTICACHE_ENDPOINT": ${elasticache_endpoint}
}


Solution 1:[1]

as @Marko E said, better to use templatefile instead of template_file so i checked the difference and finde that the second didn't parse value to json format, so adding jsonencode() func helped me

vars = {
    elasticache_endpoint = jsonencode(aws_elasticache_replication_group.this[0].configuration_endpoint_address)
  }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 none