1

I have a terraform script something like below.

resource "aws_sfn_state_machine" "sfn_state_machine" {
  name     = "my-state-machine"
  role_arn = aws_iam_role.iam_for_sfn.arn

  definition = <<EOF
{
  "Comment": "A Hello World example of the Amazon States Language using an AWS Lambda Function",
  "StartAt": "HelloWorld",
  "States": {
    "HelloWorld": {
      "Type": "Task",
      "Resource": "${aws_lambda_function.lambda.arn}",
      "End": true
    }
  }
}
EOF
}

Now, I want to update the definition from my java code.

so 1st I'm able to parse the code with HCL4j library and gave me the data is Map Structure. below is the code to parse and get the data in Map:

File terraformFile = new File("./terraform.tf");
Map<String, Map<String, Map<String, Map<String, Object>>>> map =  new HCLParser().parse(terraformFile, "UTF-8");

After this I have updated the content little bit, but don't have any clue to update back to terraform script.

So any Idea, how to convert back from Java object to terraform script again?

anij
  • 1,322
  • 5
  • 23
  • 39
  • What is your thinking regarding the "Map>>>" type of your `map` variable? Isn't it true that at each level of the HCL file structure, you will have both submaps and simple string values associated with each key? In any case, the line of code that defines that variable shouldn't even compile, as `HCLParser().parse` returns a `Map` value. You seem to suggest that that code is working. If it is, then what am I missing? – CryptoFool Dec 17 '20 at 00:35
  • 1
    I've been looking at the HCL4j documentation and googling around. I can't find any suggestion that HCL4j allows to write the data structures it manipulates back to disk as a valid `.tf` file. I'd like to be wrong, because I see this sort of thing as something I might want to do some day. I can't figure out how to do it. Maybe someone else can. – CryptoFool Dec 17 '20 at 00:44

0 Answers0