'How add terraform variables in a JSON file

Hey team I’m having trouble finding in the documentation on how to add terraform variables in a JSON file,

I need inject this variable in this JSON,

enter image description here

In this JSON of this shape but not it works,

enter image description here

I did try with var and locals, I tried it with var and locals, but it does not work, it is by default



Solution 1:[1]

You could use templatefile function [1]:

locals {
  mystring = "Test"
}

resource "grafana_dashboard" "metrics" {
  config_json = templatefile("${path.root}/EC2.json.tpl", {
    mystring = local.mystring
  })
}

For this to work, you would have to change the JSON to be:

"datasource": {
  "type": "CloudWatch"
  "uid": "${mystring}" 
}

The file with JSON data should also be renamed to EC2.json.tpl.


[1] https://www.terraform.io/language/functions/templatefile

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 Marko E