0

I am trying to pass a list of objects to a powershell script via an process level environment variable but facing issues with the encoding between terraform jsonencode & Powershell on Windows.

Using something similar to

data "template_file" "script" {
  template = file("${path.module}\\script.ps1")
  vars = {
    "tfListOfObjects" = jsonencode(var.list-of-objects)
  }
}
resource "null_resource" "example1" {
  triggers = {
    template_rendered = data.template_file.script.rendered
  }
  provisioner "local-exec" {
    command = "PowerShell -file script.ps1"
  }
}
variable "list-of-objects" {
  type = list(object({
    first_name = string
    last_name  = string
    region   = string
  }))
}

Passing in the following as the list of objects

list-of-objects = [
  {
    first_name = "User"
    last_name  = "1"
    region     = "USNE"
  },
  {
    first_name = "User"
    last_name  = "2"
    region     = "USNW"
  },
  {
    first_name = "User"
    last_name  = "3"
    region     = "EUNE"
  }
]

The script is very simple as its only for proof it works at this point,

$testObjects = "${tfListOfObjects}"

[System.Environment]::SetEnvironmentVariable('testObjects', $testObjects, [System.EnvironmentVariableTarget]::Process)


write-host "${testObjects}"
Write-Host "Does this work?" 
Write-Host "Does this work?" >> C:\Temp\terraformresult.txt
write-Host ""
write-host $env:testObjects | ConvertFrom-JSON 

So far the only output I see is in the console once I run the terraform and all I see is 'Does this work?' The other lines are blank suggesting I'm attempting to view empty variables. The only error I've been able to generate so far is below which suggests it doesn't like the escaping that terraform jsonencode is doing. Has anyone faced issues with the escaping that terraform uses and powershell on windows specifically?

char:26\r\n+ ... ounts = \"[{\"first_name\":\"User\",\"last_name\":\"1\",\"region\":\"USNE ...\r\n+
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\nUnexpected token 'first_name\":\"User\",\"last_name\":\"1\",...'

Ultimately I'm expecting to be able to deconstruct the list(object{}) in powershell by using convertfrom-json and then utilize the variable throughout a script etc. The script will be passed to VM's as part of their initialization so templatefile isn't appropriate here

2
  • Sadly its not clear what you are doing. Your error says you have some variable which name ends with ounts, yet your code does not show any such variable. Commented Dec 5, 2022 at 5:06
  • Hi Marcin, sadly that is all the output gives me, with the ... being terraform cutting the message down for formatting I guess. Is there a way to get a stacktrace/full error from terraform or just what it prints at the end of attempting to apply the plan? Commented Dec 5, 2022 at 5:12

1 Answer 1

1

I think you should use ' and $$ in the template:

$testObjects = '${tfListOfObjects}'

[System.Environment]::SetEnvironmentVariable('testObjects', $testObjects, [System.EnvironmentVariableTarget]::Process)


write-host "$${testObjects}"
Write-Host "Does this work?"
Write-Host "Does this work?" >> C:\Temp\terraformresult.txt
write-Host ""
write-host $env:testObjects | ConvertFrom-JSON
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @Marcin, It seems if I use single quotes in Powershell its taking it as a literal string and there is no substitution occuring from terraform at all. i.e. the variable is set to the string: ${tfListOfObjects} and not the json coming from the jsonencode() in terraform.
@LordBoBCUP You have to double check. It should all work as expected.
@LordBoBCUP If you are observing that behavior, then either the question is different than your code, or the answer is not being used as written.
@LordBoBCUP Did you double check what code you are actually running and what you posted in the question already?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.