Deploying the Azure function app
Once you create the infrastructure to host your function and you implement your function, you need to deploy it into the cloud.
The main tool you can use to do this is Azure Functions Core Tools, which you used in the previous chapters to create and locally debug your functions.
One of the commands that the tool exposes is the azure command, which allows you to manage your Azure stuff, and in particular the functionapp option (which allows you to manage Azure Functions).
You can use the following command to get help:
C:\MasteringServerless\MyFirstFunctionApp> func azure functionapp
This will show you the supported options for the functionapp command:
As you can see in the previous screenshot, you have four commands that you can use to interact with your function in Azure. The command we are looking for is the publish command.
The publish command syntax is simple. Using this command, you can push your Azure Function directly into the function app.
First of all, you have to connect to your subscription using the following command:
C:\MasteringServerless\MyFirstFunctionApp> Connect-AzAccount
The preceding command asks you for your Azure credentials and will set your default subscription as the target for the next command.
Pay attention when you try to execute the command because you need to import the Az.Accounts module before you run it. If you don't import the module, you could receive the following error:
Connect-AzAccount : The 'Connect-AzAccount' command was found in the module 'Az.Accounts', but the module could not be
loaded. For more information, run 'Import-Module Az.Accounts'.
At line:1 char:1
+ Connect-AzAccount
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Connect-AzAccount:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CouldNotAutoloadMatchingModule
To solve this issue, you can simply run the following cmdlet:
C:\MasteringServerless\MyFirstFunctionApp> Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
C:\MasteringServerless\MyFirstFunctionApp> Import-Module Az.Accounts
If you have more than one subscription and you want to change the subscription target, you can use the Get-AzSubscription to retrieve the list of your subscriptions (you need their IDs) and select the desired one with the Select-AzSubscription cmdlet.
Once you connect to your desired Azure subscription, you can finally deploy your Azure Function using the following command:
C:\MasteringServerless\MyFirstFunctionApp> func azure functionapp publish <functionappname>
Here, <functionappname> is the name of the function app that you want to host your function:
The publish command builds your function if you didn't build it before. Looking at the preceding screenshot, you can see that after the build step, the command creates an archive for the directory you are publishing.
This happened because the publishing process, by default, uses a ZIP file to package and deploy your code.
Packaging compiled files for distribution in a single ZIP file provides a number of advantages over distributing individual files:
- Less data is transferred
- All of the files contained in the ZIP file are updated simultaneously
Behind the scenes, the ZIP deployment uses the same engine used by the single-file deployment. This engine is called Kudu, and we will talk about it later in this chapter. When you push a ZIP file into a function app, the deployment engine (Kudu) decompresses it and substitutes all the previous files (if they exist) in the target directory.
If you want to use the ZIP deployment approach without using Azure Functions Core Tools, you can create the ZIP file by yourself and use the Kudu API to deploy it as you would for a standard website.
If you want to understand how the ZIP file is structured, you can download the current ZIP file using the Azure portal.
In the function app page, you can find the Download app content option, as shown in the following screenshot. You can use it to download the current ZIP file:
When you click on Download app content, you can choose to download the ZIP file or the Visual Studio project. The structure of the ZIP file is shown in the following diagram:
Next, we will be looking at deployment slots.