Deploying An Azure Function using VS Code

In the previous tutorial we bind an Azure Function to an Iot Hub so that we can process the events coming into the hub. However, the Azure Function was running on my local development machine so that’s not really ideal. In practice we would want to deploy it to some production environment, so that is exactly what we are going to do in this tutorial.

What we will need:

  • Azure PowerShell
  • Azure Account
  • Visual Studio Code (+ Azure Functions extension)

Create Azure Function App resource:

Let’s go into our Powershell console and create an Azure Function App resource. We will use the New-AzFunctionApp commandlet to create the Function App. For that we will need the following parameters:

  • Function App Location
  • Function App Name
  • Resource Group Name
  • Storage Account Name

For the Function App location and resource group name we will use the same as we used for the IoT Hub. However we have not create a storage account yet, so let’s go ahead and do that first. Once the storage account has been created, we then create the Azure Function App resource.

PS D:WorkspaceIoTHub> $sn = "storageaniotodyssey"
PS D:WorkspaceIoTHub> $rg = "rg-sea-aniotodyssey"
PS D:WorkspaceIoTHub> $loc = "southeastasia"
PS D:WorkspaceIoTHub> $sku = "Standard_LRS"
PS D:WorkspaceIoTHub> New-AzStorageAccount -ResourceGroupName $rg -AccountName $sn -Location $loc -SkuName $sku
StorageAccountName  ResourceGroupName   PrimaryLocation SkuName      Kind      AccessTier CreationTime         ProvisioningState EnableHttpsTrafficOnly LargeFileShares
------------------  -----------------   --------------- -------      ----      ---------- ------------         ----------------- ---------------------- ---------------
storageaniotodyssey rg-sea-aniotodyssey southeastasia   Standard_LRS StorageV2 Hot        5/05/2021 9:47:58 am Succeeded         True
PS D:WorkspaceIoTHub> Import-Module Az.Functions
PS D:WorkspaceIoTHub> $fan = "fa-sea-aniotodyssey"
PS D:WorkspaceIoTHub> New-AzFunctionApp -Name $fan -ResourceGroupName $rg -Location $loc -StorageAccount $sn -Runtime "Dotnet" -FunctionsVersion 3
VERBOSE: 'DotNet' runtime version is specified by FunctionsVersion. The value of the -RuntimeVersion will be set to '3'.
VERBOSE: OSType for Dotnet is 'Windows'.
WARNING: Unable to create the Application Insights for the function app. Creation of Application Insights will help you monitor and diagnose your function apps in the Azure Portal.
Use the 'New-AzApplicationInsights' cmdlet or the Azure Portal to create a new Application Insights project. After that, use the 'Update-AzFunctionApp' cmdlet to update Application Insights for your function app.
Name                Status  OSType Runtime Location      AppServicePlan ResourceGroupName   SubscriptionId
----                ------  ------ ------- --------      -------------- -----------------   --------------
fa-sea-aniotodyssey Running                southeastasia                rg-sea-aniotodyssey xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
PS D:WorkspaceIoTHub>
By this point, we should have created these resources

Deploy Function App to Azure:

Now that we have created the Function App resource on Azure, we can deploy our Function App onto it. The process will be a manual but simple one, using Visual Studio Code.

I am already signed in to my Azure account where the Function App resource was created. The process is as easy as using the Visual Studio Code command palette, select the “Azure Functions: Deploy to Function App…” and select the resource to deploy to.

Deploying Function App using VS Code

That’s it, we now have a live Function App which is handling events from our Azure IoT devices.

Categories: .NET, Azure, Internet of things

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: