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

≡放荡痞女 提交于 2021-01-28 05:40:21

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/53148252/terraform-command-powershell-exe-produced-invalid-json-unexpected-end-of-jso

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!