0

Had some help in getting PowerShell command to run in Terraform local-exec See here for code. Just hit the next hurdle as that I can now run various 'get-az' commands with parameters but if I want to run

Update-AzFunctionAppSetting -ResourceGroupName "MyResourceGroup" -Name "MyFunctionApp" -SubscriptionId "MySubscriptionId" -AppSetting @{"testmw" = "2"}

it throws an error: Update-AzFunctionAppSetting : Cannot index into a null array.

1
  • If the answer was helpful, Please Accept it as an Answer, so that others who encounter the same issue can find this solution and fix their problem. Commented Jan 5, 2022 at 14:10

1 Answer 1

0

Please use the below code as per your requirement :

provider "azurerm" {
  features{}
}
data "azurerm_resource_group" "example"{
    name = "ansumantest"
}
variable "function_apps" {
  default = ["ansumanfunc1","ansumanfunc2"]
}
variable "Subscription" {
    default = "SubID"
}
resource "null_resource" "example2" {
  count = length(var.function_apps)
  provisioner "local-exec" {
    command = <<Settings
    $ResourceGroupName = "${data.azurerm_resource_group.example.name}"
    $FunctionAppName = "${var.function_apps[count.index]}"
    $SubscriptionId = "${var.Subscription}"
    Get-AzFunctionApp -ResourceGroupName $ResourceGroupName -Name $FunctionAppName -SubscriptionId $SubscriptionId
    Update-AzFunctionAppSetting -ResourceGroupName $ResourceGroupName -Name $FunctionAppName -SubscriptionId $SubscriptionID -AppSetting @{"testmw" = "2"}
    Settings

    interpreter = ["PowerShell", "-Command"]
  }
}

Output:

enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

9 Comments

No luck. Just using Get-AzFunctionApp -ResourceGroupName $ResourceGroupName -Name $FunctionAppName -SubscriptionId $SubscriptionId works but Update-AzFunctionAppSetting -ResourceGroupName $ResourceGroupName -Name $FunctionAppName -SubscriptionId $SubscriptionID -AppSetting @{"testmw" = "2"} fails with │ Update-AzFunctionAppSetting : Cannot index into a null array. │ At line:13 char:7 │ + Update-AzFunctionAppSetting -ResourceGroupName $ResourceGroupNa ... │ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
│ + CategoryInfo : InvalidOperation: (:) [Update-AzFunctionAppSetting], RuntimeException │ + FullyQualifiedErrorId : NullArray,Update-AzFunctionAppSetting
@MWOITC, can you please add the whole updated code that you are using now ? so that I can test the same code ..
Sure: code resource "null_resource" "az_command" { triggers = { uuid_trigger = "${uuid()}" } provisioner "local-exec" { interpreter = ["PowerShell", "-Command"] command = <<EOT $ResourceGroupName = "${module.rg.resource_group.name}" $FunctionAppName = "${var.function_apps.name}" $SubscriptionId = "${module.functionapp.function_app.id}"
code Get-AzFunctionApp -ResourceGroupName $ResourceGroupName -Name $FunctionAppName -SubscriptionId $SubscriptionId Update-AzFunctionAppSetting -ResourceGroupName $ResourceGroupName -Name $FunctionAppName -SubscriptionId $SubscriptionID -AppSetting @{"testmw" = "2"} EOT } }
|

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.