External script in powershell - problem

669 views
Skip to first unread message

Michał Cybula

unread,
Jan 24, 2018, 6:34:51 AM1/24/18
to Terraform
Hi. 
I'am using terraform to deploy azure infrastructure and a few things I must configure via external powershell script. Everything else i.e.: app service, app service plan are deploying fine.


In main.tf I have:

data "external" "powershell_test" {
  program = ["Powershell.exe", "./testScript.ps1"]

  query = {
   resourceGroupName  = "${var.resource_group_name}"
   webAppName = "${var.app_name}-apps-${lookup(var.regions, var.location)}-${var.environment_name}"
   location = "${var.location}"
   subsID = "sampleIDnumber"
  }
}

And in testScript.ps1 I have:

# Read the JSON payload from stdin
$jsonpayload = [Console]::In.ReadLine()

# Convert JSON to a string
$json = ConvertFrom-Json $jsonpayload
$ResourceGroupName = $json.resourceGroupName
$WebAppName = $json.webAppName
$Location = $json.location
$Subscription_id = $json.subsID
$Target_resource_id = "/subscriptions/$Subscription_id/resourceGroups/$ResourceGroupName/providers/Microsoft.Web/sites/$WebAppName"

 $TestArray = $ResourceGroupName, $WebAppName, $Location, $Subscription_id, $Target_resource_id
 $TestArray | Out-File C:\fileOUT.txt     #this is working fine, I have fileOUT.txt with correct variables from terraform 


if ($Location -eq "westeurope") {
   
  $TestArray2 = $ResourceGroupName, $WebAppName, $Location, $Subscription_id, $Target_resource_id
  $TestArray2 | Out-File C:\fileOK.txt  #this is working fine, I have fileOK.txt with correct variables from terraform 

    Write-Output "{ ""first"" : ""Ok""}" 

}
else { 
    $TestArray3 = $ResourceGroupName, $WebAppName, $Location, $Subscription_id, $Target_resource_id
    $TestArray3 | Out-File C:\fileNotOK.txt
    Write-Output "{ ""first"" : ""NotOK""}"
}


# Write output to stdout
Write-Output "{ ""first"" : ""out""}"

But in terraform I have an error:

* module.appservice.2018-01-24T12:23:48.692+0100 [DEBUG] plugin: plugin process exited: path=C:\Documents\.terraform\plugins\windows_amd64\terraform-provider-random_v1.1.0_x4.exe
2018-01-24T12:23:48.692+0100 [DEBUG] plugin: plugin process exited: path=C:\Documents\.terraform\plugins\windows_amd64\terraform-provider-azurerm_v1.0.1_x4.exe
a2018-01-24T12:23:48.692+0100 [DEBUG] plugin: plugin process exited: path=C:\Documents\.terraform\plugins\windows_amd64\terraform-provider-external_v1.0.0_x4.exe
zurerm_app_service.app-service: 1 error(s) occurred:

* module.appservice.data.external.powershell_test: data.external.powershell_test: command "Powershell.exe" produced invalid JSON: invalid character '{' after top-level value

When I remove "if (...) {...}" - it will work correct. Whre is a mistake? Why powershell or azure thinks that if {} is a JSON structure?
When I would like to add some azurerm command for example (and remove if): Login-AzureRmAccount I will have azure login pop-up and error in terraform:

Error: Error refreshing state: 1 error(s) occurred:
2018-01-24T12:30:41.072+0100 [DEBUG] plugin: plugin process exited: path=C:\Documents\.terraform\plugins\windows_amd64\terraform-provider-external_v1.0.0_x4.exe
2018-01-24T12:30:41.072+0100 [DEBUG] plugin: plugin process exited: path=C:\Documents\.terraform\plugins\windows_amd64\terraform-provider-random_v1.1.0_x4.exe
2018-01-24T12:30:41.072+0100 [DEBUG] plugin: plugin process exited: path=C:\Documents\.terraform\plugins\windows_amd64\terraform-provider-azurerm_v1.0.1_x4.exe

* module.appservice.data.external.powershell_test: 1 error(s) occurred:

* module.appservice.data.external.powershell_test: data.external.powershell_test: command "Powershell.exe" produced invalid JSON: invalid character 'E' looking for beginning of value




Javier Ruiz Jiménez

unread,
Jan 26, 2018, 6:16:00 AM1/26/18
to Terraform
Hi Michal,

Please run the powershell script in a command line and check the output, it has to be json. 

Example of a correct Output:

{ "internet_ip": "123.123.45.67" }

And you later use that value in Terraform as 

cidr_blocks = ["${data.external.whatismyip.result["internet_ip"]}/32"]

I suggest that the output (for stdout) should be produced using ConvertTo-Json cmdlet, see examples:

Michał Cybula

unread,
Jan 26, 2018, 8:52:20 AM1/26/18
to Terraform
But I don't have a problem with input/output data from and to terraform.
Problem is with executing script in Powershell by terraform. 
As I said, when I add in PS if (.....) {.....} then I have:

* module.appservice.data.external.powershell_test: data.external.powershell_test: command "Powershell.exe" produced invalid JSON: invalid character '{' after top-level value
Powershell script works correctly in the background but in terraform log I have error.
Reply all
Reply to author
Forward
0 new messages