hashicorp / terraform-provider-azurerm

Terraform provider for Azure Resource Manager
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
Mozilla Public License 2.0
4.53k stars 4.6k forks source link

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

Closed mariussm closed 1 year ago

mariussm commented 3 years ago

Community Note

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)

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

adamrushuk commented 3 years ago

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

antonismladenis commented 3 years ago

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?

petersgiles commented 3 years ago

I would like to know how to do this too

bodemckenna commented 3 years ago

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

Any work arounds?

mariussm commented 3 years ago

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?

sayandaw commented 3 years ago

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

edomaur commented 3 years ago

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

DevOpsBoondoggles commented 3 years ago

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"
  }
}
sayandaw commented 3 years ago

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

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

srjennings commented 3 years ago

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/

bryrod commented 3 years ago

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.

honb commented 2 years ago

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

insomniacc commented 2 years ago

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

titoluyo commented 2 years ago

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

I got that this works

site_config {
  linux_fx_version = "PowerShell|7"
}
srjennings commented 2 years ago

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.

leandroscardua commented 2 years ago

Hi Team,

Do you have any update on that issue?

erwinkramer commented 2 years ago

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

ju-ge commented 2 years ago

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

Annesars90 commented 2 years ago

Same issue, both on azurerm_windows_function_app & the former azurerm_function_app.

tjgruber commented 1 year ago

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

erwinkramer commented 1 year ago

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

tjgruber commented 1 year ago

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

erwinkramer commented 1 year ago

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

tjgruber commented 1 year ago

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 commented 1 year ago

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.

ju-ge commented 1 year ago

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

github-actions[bot] commented 1 year ago

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.