After my last webcast about Azure Functions, I received an interesting question: how can I quickly deploy a function to Azure? Or maybe to different Azure subscriptions or regions?
We saw in the webcast how you can deploy an Azure Function by using Visual Studio or Visual Studio Code directly. But Azure Functions have also a full range of continuous deployment and integration options provided by Azure App Service and you can deploy an Azure Function directly from a CI/CD pipeline in Azure DevOps. But there’s also a quickest way in my opinion: Zip deployment.
With Zip deployment, you can create a .zip package of your function’s files and then publish it directly to Azure by using Azure CLI or Powershell or the zipdeploy REST API (available to the endpoint https://<app_name>.scm.azurewebsites.net/api/zipdeploy).
In this post, I want to show you how you can use Zip deployment for deploying your Azure Functions quickly and on multiple subscriptions or regions.
For this demo, I’ve created a very simple HTTPTrigger Azure Function with Visual Studio by using the standard template:
When your function is ready to be deployed, right click the project, select Publish and then select Folder as the publish target:
In this way, Visual Studio creates a publish folder with all the content (files, binaries, DLLs and so on) that must be published on Azure in order to execute your function:
Select all the files and folder inside the publish folder and create a .zip package. This will be the ZIP archive that you will deploy on Azure directly.
To deploy our Azure Function by using Zip deployment, I’ve created a Powershell script that uses Azure CLI and that does all the work for you. The script can be downloaded from here: https://github.com/demiliani/afzipdeployment
This script does the following:
Step 1: It declares some variables, like the Azure region where to deploy the function, the Resource Group to use (or create), the Storage Account to create, the name of the Azure Function and the full path of the .zip archive to deploy:
$location = "westeurope" $resourceGroupName = "demozipdeployrg" $storageAccountName = "demozipdeploysa" $functionName = "DemoZipDeploy" $sourceZipPath = "C:\SD\source\repos\DemoZipDeploy\bin\Release\netcoreapp2.1\publish\DemoZipDeploy.zip"
Step 2: it creates a resource group:
az group create --name $resourceGroupName --location $location
Step 3: it creates a storage account:
az storage account create --name $storageAccountName --location $location --resource-group $resourceGroupName --sku Standard_LRS
Step 4: it creates a new function app:
az functionapp create --name $functionName --storage-account $storageAccountName --consumption-plan-location westeurope --resource-group $resourceGroupName --functions-version 2
Step 5: it publish the function on Azure by using the ZIP package:
az webapp deployment source config-zip -g $resourceGroupName -n $functionName --src $sourceZipPath
That’s it! Your Azure Function is deployed and it’s up and running in the time of a click… 🙂