Kailash P
Kailash P

Reputation: 81

Terraform: command "Powershell.exe" produced invalid JSON: unexpected end of JSON input

I am trying to use Terraform's external provider to run a PowerShell script.

I am not able to pass parameters to the script from Terraform.

I am using the following code to run the script:

variable "file_path" {
  type        = "string"
  description = "Enter the powershell file path"
  default     = "../../Services/azurerm_iam_role/Powershell_Scripts/test.ps1"
}

data "external" "powershell_test" {
  program = ["Powershell.exe", "${var.file_path}"]

  query = {
    foo = "asdf"
    bar = "Hardcoded"
  }
}

The following is my PowerShell script file code:

# Read stdin as string
$jsonpayload = [Console]::In.ReadLine()

# Convert to JSON
$json = ConvertFrom-Json $jsonpayload

# Access JSON values
$foo = $json.foo
$bar = $json.bar

while executing terraform apply I got the following error:

command "Powershell.exe" produced invalid JSON: unexpected end of JSON input

Is there a fix or alternate solution?

Upvotes: 1

Views: 1627

Answers (1)

ydaetskcoR
ydaetskcoR

Reputation: 56877

A data source needs to output something which you don't currently appear to be doing. The external data source in particular needs JSON output on stdout which could then be accessed under the result attribute of the data source.

So to continue your example PowerShell script you might want something like this (untested):

# Read stdin as string
$jsonpayload = [Console]::In.ReadLine()

# Convert to JSON
$json = ConvertFrom-Json $jsonpayload

# Access JSON values
$foo = $json.foo
$bar = $json.bar

# Set foobar based on foo and bar
$foobar = "$foo$bar"

# Return foo and foobar
@{
    foobar=$foobar;
    foo=$foo;
} | ConvertTo-Json

Upvotes: 2

Related Questions