Introduction

In my previous article, we got an overview of CloudShell and how to use it for creating a web app in Azure. In this article, we will explore how to create an Azure Function using CloudShell (PowerShell).

Scenario

During deployment, we need to provide a more automated approach to create and manage Azure resources. In this article, we will see the step by step process of creating the Azure Function using PowerShell.

Prerequisites

  • Azure cloud subscription
  • Basic knowledge of PowerShell

CloudShell

Now, we will focus on the actual steps using CloudShell to create an Azure Function.

Step 1

Log into Azure Portal.

Step 2

Click on the CloudShell icon. This icon is present on the top horizontal bar on Azure Portal site.

Create Storage Account And Azure Function Using CloudShell

Step 3

Now, we will execute the below commands one by one.

To create a web app in Azure – first, we need to create a resource group and a service plan.

Create Storage Account And Azure Function Using CloudShell
Now, we must create a storage account before creating the Azure Function, because we need a storage connection of that storage account. Every storage account has its own storage connection.

Why do we need a storage account? Because an Azure Function holds function files; hence we need to have file storage to hold these files.

Prepare the parameters like resource group, location etc. for storage account and create it by using the command

‘New-AzureRmStorageAccount’-
  1. # Set Parameters for Storage account
  2. $newStorageParams = @{
  3. ResourceGroupName = $rg
  4. AccountName = $sa
  5. Location = $location
  6. SkuName = $storageSku
  7. }
  8. # create storage account with above parameters
  9. $storageAccount = New-AzureRmStorageAccount @newStorageParams
  10. $storageAccount
Every storage account has an account key and connection string. We need the value of these two parameters later while assigning the storage account details to Azure Function.
  1. # Get storage account key and create connection string
  2. $accountKey = Get-AzureRmStorageAccountKey -ResourceGroupName $rg -AccountName $sa | Where-Object {$_.KeyName -eq 'Key1'} | Select-Object -ExpandProperty Value
  3. $storageConnectionString = "DefaultEndpointsProtocol=https;AccountName=$sa;AccountKey=$accountKey"
Now, we will jump to the actual creation process of Azure Function.

First, we will prepare the parameters of the Azure Function, like function name, resource group, data center location etc. and then, will create that.
  1. #create function app
  2. $newFunctionAppParams = @{
  3. ResourceType = 'Microsoft.Web/Sites'
  4. ResourceName = $fun1
  5. Kind = 'functionapp'
  6. Location = $location
  7. ResourceGroupName = $rg
  8. Properties = @{}
  9. Force = $true
  10. }
  11. #create function
  12. $functionApp = New-AzureRmResource @newFunctionAppParams
  13. $functionApp
So far, we have not assigned the above-created storage account connection to the newly-created Azure Function. Hence, to assign the storage account, we need to use ‘Set-AzureRmWebApp’ command with parameters, as mentioned in the below command.
  1. # Set Function app settings
  2. $functionAppSettings = @{
  3. AzureWebJobDashboard = $storageConnectionString
  4. AzureWebJobsStorage = $storageConnectionString
  5. FUNCTIONS_EXTENSION_VERSION = '~1'
  6. WEBSITE_CONTENTAZUREFILECONNECTIONSTRING = $storageConnectionString
  7. WEBSITE_CONTENTSHARE = $sa
  8. }
  9. $functionAppSettings
  10. $setWebAppParams = @{
  11. Name = $fun1
  12. ResourceGroupName = $rg
  13. AppSettings = $functionAppSettings
  14. }
  15. $webApp = Set-AzureRmWebApp @setWebAppParams
  16. $webApp
That's it. We have successfully created an Azure Storage Account and then created the Azure function too.