Create Storage Account And Azure Function Using CloudShell

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

  • CloudShell is a feature in the Azure cloud which gives us the facility to write in commands with browser-based experience.
  • CloudShell helps the Azure technical team to manage and control Azure Resources.
  • We have the following choices – Bash, PowerShell.
  • Secure access to authenticate account access for PowerShell and CLI.
  • CloudShell is integrated to open source objects as well.
  • CloudShell supports multiple languages – .NET Core, Go, Java, Nods.JS, PowerShell, and Python etc.

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
  • Create variables and assign values.
    1. # RG and location  
    2. $rg = "Name"  
    3. $location = "Central US"  
    4. #Storage Account  
    5. $sa = "Name"  
    6. $storageSku = 'Standard_LRS'  
    7. # function names  
    8. $fun1 = "functioname"  
  • Create Resource Group
    1. #create Resource Group  
    2. New-AzureRMResourceGroup -Name $rg -Location $location  
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.  
  9. # create storage account with above parameters  
  10. $storageAccount = New-AzureRmStorageAccount @newStorageParams  
  11. $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.  
  12. #create function  
  13. $functionApp = New-AzureRmResource @newFunctionAppParams  
  14. $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.   
  10. $functionAppSettings  
  11.   
  12. $setWebAppParams = @{  
  13.     Name = $fun1  
  14.     ResourceGroupName = $rg  
  15.     AppSettings = $functionAppSettings  
  16. }  
  17.   
  18. $webApp = Set-AzureRmWebApp @setWebAppParams  
  19. $webApp  
That's it. We have successfully created an Azure Storage Account and then created the Azure function too.


Similar Articles