terraform-provider-azurerm icon indicating copy to clipboard operation
terraform-provider-azurerm copied to clipboard

Support for setting CURRENT_STACK for Azure Functions, enabling durable Azure Functions on PowerShell

Open mariussm opened this issue 4 years ago • 18 comments

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Description

I am trying to create a azurerm_function_app with PowerShell 7 based durable Azure Functions. When the function app is created, the following setting is blank, which makes the durable functions return 500 internal server error:

image

New or Affected Resource(s)

  • azurerm_function_app

Potential Terraform Configuration

It seems that behind the scenes for the Azure Portal, the following is sent when updating the blank field to PowerShell 7.0 (as a part of siteConfig):

"metadata": [
    {
        "name": "CURRENT_STACK",
        "value": "powershell"
    }
]
resource "azurerm_function_app" "function" {
  name                       = "superfunction"
  location                   = var.location
  resource_group_name        = azurerm_resource_group.rg.name
  app_service_plan_id        = azurerm_app_service_plan.function_plan.id
  storage_account_name       = azurerm_storage_account.function_storage.name
  storage_account_access_key = azurerm_storage_account.function_storage.primary_access_key
  version                    = "~3"
  https_only                 = true

  app_settings = {
    ApplicationInsightsAgent_EXTENSION_VERSION = "~2"
    FUNCTIONS_WORKER_RUNTIME                   = "powershell"
    FUNCTION_APP_EDIT_MODE                     = "readonly"
    WEBSITE_NODE_DEFAULT_VERSION               = "10.14.1"
  }

  identity {
    type = "SystemAssigned"
  }

  site_config {
    metadata = [
      {
        name = "CURRENT_STACK"
        value = "powershell"
      }
    ]
  }
}

References

mariussm avatar Oct 13 '20 08:10 mariussm

I also have to manually update that PowerShell Core Version setting, and restart the function app to make it work, so looking forward to this fix.

I've seen other people define siteConfig.powerShellVersion = "~7" , eg: https://github.com/neumanndaniel/armtemplates/blob/aa14255f3a2be592ac60dc8ff541fda7cb10c649/aks-snapshot-backup/aks-snapshot-backup-infrastructure.json#L97

adamrushuk avatar Nov 04 '20 09:11 adamrushuk

Hi I am facing the same issue with Terraform. I am trying to create a Powershell Function App in Azure with azurerm_function_app and I want to set Powershell Core Version to 7. The issue is that after terraform apply the Function App is created, but the Powershell Core Version is blank. I was able to set the Python version for a Linux Function App, but not for Powershell.

Screenshot 2021-04-09 at 10 55 24

I tried something like that, but with no success:

resource "azurerm_function_app" "example" {
  name                       = "functionapptest"
  location                   = azurerm_resource_group.example.location
  resource_group_name        = azurerm_resource_group.example.name
  app_service_plan_id        = azurerm_app_service_plan.example.id
  storage_account_name       = azurerm_storage_account.example.name
  storage_account_access_key = azurerm_storage_account.example.primary_access_key
  version = "~3"
  app_settings = {
        FUNCTIONS_WORKER_RUNTIME = "powershell"
        FUNCTIONS_WORKER_RUNTIME_VERSION = "~7"
       
  }
}

I know that in a Linux Function App you can set the the Python version using:

site_config {
    linux_fx_version = "PYTHON|3.9"
}

Is there any update on this issue?

antonismladenis avatar Apr 09 '21 08:04 antonismladenis

I would like to know how to do this too

petersgiles avatar Apr 12 '21 07:04 petersgiles

Hi I am facing the same issue with Terraform. I am trying to create a Powershell Function App in Azure with azurerm_function_app and I want to set Powershell Core Version to 7. The issue is that after terraform apply the Function App is created, but the Powershell Core Version is blank. I was able to set the Python version for a Linux Function App, but not for Powershell.

Screenshot 2021-04-09 at 10 55 24

I tried something like that, but with no success:

resource "azurerm_function_app" "example" {
  name                       = "functionapptest"
  location                   = azurerm_resource_group.example.location
  resource_group_name        = azurerm_resource_group.example.name
  app_service_plan_id        = azurerm_app_service_plan.example.id
  storage_account_name       = azurerm_storage_account.example.name
  storage_account_access_key = azurerm_storage_account.example.primary_access_key
  version = "~3"
  app_settings = {
        FUNCTIONS_WORKER_RUNTIME = "powershell"
        FUNCTIONS_WORKER_RUNTIME_VERSION = "~7"
       
  }
}

I know that in a Linux Function App you can set the the Python version using:

site_config {
    linux_fx_version = "PYTHON|3.9"
}

Is there any update on this issue?

I'm seeing the same issue with powershell Core Version.

app_settings = { FUNCTIONS_WORKER_RUNTIME = "powershell" FUNCTIONS_WORKER_RUNTIME_VERSION = "~7"

}

Does not work for me.

Terraform v0.15.4 on darwin_amd64

  • provider registry.terraform.io/hashicorp/azurerm v2.60.0

Any work arounds?

bodemckenna avatar May 27 '21 10:05 bodemckenna

Here is my workaround in an Azure DevOps pipeline PowerShell step:

    # Change function app to PS 7
    Write-Host '##[group]Change function app to PS 7'
    $function = az functionapp show --name $functionappname --resource-group $rgname | ConvertFrom-Json
    if($function.siteConfig.powerShellVersion -ne "~7") {
        Write-Host "Updating powershell version to ~7..."
        az functionapp update --name $functionappname --resource-group $rgname --set "siteConfig.powerShellVersion=~7"
    } else {
        Write-Host "Powershell version already set to to ~7"
    }
    Write-Host '##[endgroup]'

Edit:

The powerShellVersion was previsously not returned from Azure CLI at all, but it was still setable. Now it is indeed returned, so maybe that Go SDK also returns this value and makes it possible to set?

mariussm avatar May 27 '21 18:05 mariussm

I am also facing the same issue !! Any Update on when this will be fixed?

sayandaw avatar May 30 '21 08:05 sayandaw

same issue here, would like any update on this :-)

edomaur avatar Jun 07 '21 06:06 edomaur

Heya, just to build on the great work by @mariussm and as an alt take.

I did this as a work around and it worked okay running in ADO pipeline but would work anywhere the cli is installed I think I'm pretty novice at Terraform so there's likely much more robust ways of doing this.

resource "null_resource" "ucf_fa" {
  provisioner "local-exec" {
    command = <<-EOT
      az login --service-principal --username '${var.sp_appid}' -p='${var.sp_appsecret}' --tenant "${var.tenant}"
      
 az functionapp update --name ${azurerm_function_app.ucf_fa.name} --resource-group ${azurerm_function_app.ucf_fa.resource_group_name} --set siteConfig.powerShellVersion=~7
    EOT
  }
  depends_on = [azurerm_function_app.ucf_fa]
  triggers = {
        build_number = "1"
  }
}

DevOpsBoondoggles avatar Jun 25 '21 18:06 DevOpsBoondoggles

@gabrielmccoll, Yes .. thank you so much. I am also very new to terraform.

  • In my 1st approach i was executing the az cli commands from the pipeline to set the PowerShellVersion.
  • Then one of my colleagues recommended that in Terraform we should use a null resource to achieve this, exactly 💯 like you have shown.

Thank you so much ! This workaround is good for me now 😌

sayandaw avatar Jun 27 '21 04:06 sayandaw

PowerShell 6 Support is ending September 30, 2022... would be great to have this feature added!

https://azure.microsoft.com/en-us/updates/azure-functions-support-for-powershell-6-is-ending-on-30-september-2022/

srjennings avatar Sep 09 '21 01:09 srjennings

I'm also running into this issue. Hard to advocate for IaC when there's a need for manual changes via console.

Update: Found the Azure Functions Core Tool. It can initialize the function locally with the required app files. In this scenario, I initialized my function in PowerShell and published it to the function app. Here's a video that I followed.

Working on a tf local-exec resource that will create the directory and initialize the function. After that, I'll need to update my azure cli docker image to include the azure functions tool, and change the CI to publish the function if changes in the function's directory have occurred.

bryrod avatar Sep 20 '21 20:09 bryrod

I'm also getting this issue. Have to update it manual in Azure Portal. It would be nice if this can be done by terraform script

honb avatar Oct 14 '21 09:10 honb

+1 also having the same issue. Feels like this should have been updated some time ago as it's quite common config for functions.

insomniacc avatar Oct 25 '21 14:10 insomniacc

I found the workaround: siteConfig.powerShellVersion=~7 doesn't work for me.

I got that this works

site_config {
  linux_fx_version = "PowerShell|7"
}

titoluyo avatar Dec 02 '21 16:12 titoluyo

I found the workaround: siteConfig.powerShellVersion=~7 doesn't work for me.

I got that this works

site_config {
  linux_fx_version = "PowerShell|7"
}

This only works with Linux App Service Plans and Linux Function Apps. A solution is still needed for Windows-based apps and plans.

srjennings avatar Dec 03 '21 15:12 srjennings

Hi Team,

Do you have any update on that issue?

leandroscardua avatar Apr 20 '22 12:04 leandroscardua

This is supported through the powershell_core_version application_stack element, you have to move to azurerm_windows_function_app first.

erwinkramer avatar Aug 08 '22 14:08 erwinkramer

This is supported through the powershell_core_version application_stack element, you have to move to azurerm_windows_function_app first.

Hello.

Terraform version : 0.14.1 AzureRM version : 3.17.0

Still Ko PowerShell 7 & 7.2 with azurerm_windows_function_app, applying a second time does not solve the problem

Configuration => General settings : image Template ARM : image image

However, stack PowerShell 7 & 7.2 work well with azurerm_linux_function_app

If anyone has a solution to the problem, I'm interested. Thanks

ju-ge avatar Aug 08 '22 14:08 ju-ge

Same issue, both on azurerm_windows_function_app & the former azurerm_function_app.

Annesars90 avatar Aug 12 '22 12:08 Annesars90

I was able to do it successfully by putting the application_stack block inside of the site_config block, for example:

  site_config {
    use_32_bit_worker = false
    ftps_state = "FtpsOnly"
    application_stack {
      powershell_core_version = 7.2
    }
  }

After applying the above terraform plan, I confirmed it was in fact using 7.2 by running the following azcli command: az functionapp config show --name myFunctionAppName --resource-group myResourceGroupName and the output showed the following result: "windowsFxVersion": "PowerShell|7.2",

I re-confirmed by changing the version in the terraform config to 7, reran the command to show the configuration, and the output shown as: "windowsFxVersion": "PowerShell|7",

So it appears to be functioning correctly, even though the PowerShell Core Version in the general app settings web page is blank. image

References: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/windows_function_app#application_stack

tjgruber avatar Oct 20 '22 18:10 tjgruber

@tjgruber did you confirm that your function app is actually working by calling a function inside your function app?

erwinkramer avatar Oct 20 '22 18:10 erwinkramer

@erwinkramer I tested a simple PowerShell HTTP Trigger, and it was successful. I also tested creating a Durable Function, and it let me.

tjgruber avatar Oct 20 '22 19:10 tjgruber

I think Microsoft made Powershell 7 default so it would work now no matter what, maybe confirm the Powershell version inside the function by calling $PSVersionTable.PSVersion? Then we know for sure sure it's actually using 7.2 and not falling back to 7

erwinkramer avatar Oct 20 '22 19:10 erwinkramer

The function itself is actually defaulting to 7.0.12.

image

I just did a comparison, of before and after setting it manually in the Web GUI. Everything stays the same, except these changes:

image

So I did yet some more digging through the Terraform documentation and Microsoft App Settings documentation, found the powerShellVersion supported there all the way at the bottom, and added those recommended changes to the Terraform config, which it accepts:

  app_settings = {
    "powerShellVersion" = "~7",
  }

  site_config {
    remote_debugging_version = "VS2019"
    application_stack {
      powershell_core_version = 7.2
    }
  }

I used Microsoft's suggested "~7", along with all other combinations, none of which work. When I output the settings config, it always shows blank "powerShellVersion": "",. The only way I've seen to change it so far is manually in the Web GUI.

The Terraform config doesn't accept "powerShellVersion" = "~7" anywhere else, as it's an app setting.

I also tried without the application_stack and using the following app_settings, with no luck:

  app_settings = {
    "FUNCTIONS_WORKER_RUNTIME" = "powershell",
    "FUNCTIONS_WORKER_RUNTIME_VERSION" = "~7",
  }

  app_settings = {
    "WEBSITE_RUN_FROM_PACKAGE" = "",
    "netFrameworkVersion" = "v6.0",
    "FUNCTIONS_WORKER_RUNTIME" = "node",
    "powerShellVersion" = "~7",
    "FUNCTIONS_WORKER_RUNTIME_VERSION" = "~7",
  }

...and different combinations, not exactly what's above, but including what's there.

https://learn.microsoft.com/en-us/azure/azure-functions/functions-app-settings#powershellversion

https://learn.microsoft.com/en-us/azure/azure-functions/functions-app-settings#netframeworkversion

etc... I tried all kinds of combinations from the documentation, and it's not clear anywhere how to specify this in Terraform using the Terraform resource azurerm_windows_function_app.

The way the Terraform resource is interacting with the Azure API or however it's doing it, seems to need fixed. If there is a way to set the PowerShell Core Version in the Terraform config, it's certainly not clear.

I'm going to continue to try to get this working through Terraform config, because I don't want to have to manually set things through Azure Web GUI, that defeats the whole purpose of Terraform. I'll come back here if I figure it out. Otherwise, hopefully someone from Terraform can comment on this.

tjgruber avatar Oct 20 '22 22:10 tjgruber

In addition to the above, I haven't even been successful in changing the PowerShell version from 7 to 7.2 via Azure CLI. It may be a limitation in Azure CLI, therefore the azurerm_windows_function_app resource. It does look possible to upgrade from 7 to 7.2 via Azure PowerShell and ARM templates... but I'd really like to do this with Terraform.

tjgruber avatar Oct 21 '22 20:10 tjgruber

Hello. Since azurerm v3.39.0, the problem does not seem to be present anymore. this issue can be closed.

ju-ge avatar Feb 02 '23 13:02 ju-ge

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

github-actions[bot] avatar Mar 10 '23 02:03 github-actions[bot]