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.
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"
}
}
# 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""}"
* 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