79580428

Date: 2025-04-18 04:32:08
Score: 2
Natty:
Report link

In the system where you wish to generate these JSONs, do you have access to the terraform binary itself, and are you able to include the terraform.io/builtin/terraform provider to your configuration? If yes, you could do something elegant, like this:

First add the terraform provider:

terraform {
  required_providers {
    terraform = {
      source = "terraform.io/builtin/terraform"
    }
  }
}

Then use this to get your JSON, which you can then pipe to a file as required:

echo 'jsonencode(provider::terraform::decode_tfvars(file("${path.module}/PATH-TO-FILE.tfvars")))' | \
    terraform console | \
    jq -r | \
    jq

The usage of jq is optional - it is used twice as the output of terraform console is a stringified JSON, so the first jq -r converts the string to a parseable JSON.

This makes use of the decode_tfvars function provided by terraform, which converts the contents of a .tfvars file to a terraform object.

Reasons:
  • RegEx Blacklisted phrase (2.5): do you have a
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
Posted by: hexbioc