Add Data to a Json File in Terraform

I am not a software guy, but I am preparing a small script for delarative onboarding work using terraform. The declaration is in a json file and it is send to the device through terraform. But I do not want to keep the sensitive data like passwords and secret keys in the json file. I want to inject them from a seperate file in a vault to the json file when I am executing it in terraform. I can use terraform to read the data from a vault or a s3 bucket, but I am not sure how to add it to the json declaration. I saw in terraform there is a functions like jsondecode and jsonencode. But I am not sure whether they will be any use to my requirement. Could someone help me on this please ?

Thansk.

2

1 Answer

Maybe this is something which could help you? Create a data source which loads your template file, here you can pass in some vars.

some examples for variales:

data "template_file" "my_template" {
  template = templatefile("template.json", {
    foo         = "bar"
    another_foo = aws_s3_bucket.my_bucket.id
    more_foo    = var.my_variable
  })
}

The template file could look like this:

{
  "Value":   "123",
  "value_2": "${foo}",
  "value_3": "${another_foo}",
  "value_4": ${more_foo},
}
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest