azurepowershelldeploymentazure-functions

How to create Azure Function and deploy content as zip deploy


I have made az cli scripts for creating function app, deploying content via zip deploy and setting app settings/environment variables. After running script the only file in wwwroot is 'FAILED TO INITIALIZE RUN FROM PACKAGE.txt'. No other content is found. After running the script again, content sometimes is present.

$exists = az functionapp list | ConvertFrom-Json -Depth 10 | Select name | Where {$_.Name -like $func_name}
If ($exists.length -eq 0) {
  Write-Host "Creating function '$func_name'"
  az functionapp create -g $ressource_group -n $func_name -s $storageaccount --os-type Windows --consumption-plan-location northeurope --functions-version 4 --runtime dotnet-isolated --runtime-version 8
}
    
Write-Host "Deploying $func_name"
az functionapp deployment source config-zip -g $ressource_group -n $func_name_azure --src $function_package_zip --build-remote true --timeout 120

Write-Host "Configuring $func_name"
az functionapp config appsettings set -g $ressource_group -n $func_name --settings `@func_settings.json

Deploying from VS 2022 in our dev environment works fine. I have compared my generated zip-file with the zip-file generated by VS 2022 in our dev setup and found no differences.

I guess, I'm missing some parameters or steps but have been unable to find further documentation.


Solution

  • You should add SCM_DO_BUILD_DURING_DEPLOYMENT= TRUE setting under FunctionApp=>Environment Variables=>App Settings before deploying the function to Azure.

    Navigate to root directory of the function project, select all the files inside the project and zip it. You can use this zip file to deploy the function to Azure.

    Used below code to create the Azure function app and deploy the function code using Power Shell script.

    function.ps1:

    $func_name = "functionApp_Name"
    $func_name_azure = $func_name 
    $resource_group = "<ResourceGroup_Name>"
    $storage_account = "<Storage_Account>"
    $location = "Location"
    $function_package_zip = "functionapp.zip"
    
    # Check if the functionapp exists 
    $check= az functionapp list --query "[?name=='$func_name']" | ConvertFrom-Json
    $exists = $check.Length -gt 0
    
    # Create a new functionapp if it doesn't exists
    
    If (!$exists) {
      Write-Host "Creating function '$func_name'"
      az functionapp create -g $resource_group -n $func_name -s $storage_account --os-type Windows --consumption-plan-location northeurope --functions-version 4 --runtime dotnet-isolated --runtime-version 8
    }
        
    Write-Host "Deploying $func_name"
    az functionapp deployment source config-zip -g $resource_group -n $func_name_azure --src $function_package_zip --build-remote true --timeout 120
    
    Write-Host "Configuring $func_name"
    az functionapp config appsettings set -g $resource_group -n $func_name --settings `@func_settings.json
    

    Able to create the function app and zip deploy the function to Azure.

    Portal:

    enter image description here