Create Azure Function using Visual Studio Code and PowerShell

Create Azure Function using Visual Studio Code and PowerShell

In this Azure tutorial, we will discuss Create Azure Function using Visual Studio Code and PowerShell. Along with this, we will also discuss the below topics.

  • How To Create Azure Functions In Visual Studio Code
  • Build Azure function in visual studio code
  • Debug Azure Function In Visual Studio Code
  • Run Azure Function Locally using Visual Studio Code
  • Deploy Azure function from visual studio code
  • Create Azure Function with PowerShell
  • Open Existing Azure Function In Visual Studio Code

Create Azure Function using Visual Studio Code and PowerShell

Well, here we will discuss Create Azure Function using Visual Studio Code. Before that, We should know the Prerequisites needed or the things needed to Set Up Your Environment to Create Azure Function using Visual Studio Code.

Set Up Your Environment to Create Azure Functions

Prerequisites

Below are the things needed before you start to Build Your First Azure Function in Visual Studio Code.

  • The first thing we need is an Azure Account or Azure Subscription. If you don’t have an Azure Account till now, create an Azure Free Account now.
  • Visual Studio Code needs to install. If you don’t have Visual Studio Code installed in your machine, then install the Visual Studio Code now.
  • The Azure Function Extensions for the Visual Studio Code.

How To Create Azure Functions In Visual Studio Code

Now assuming that we have everything ready needed for the development activities, Let’s start the actual development. Follow the below steps to Create Azure Functions using Visual Studio Code.

Step-1: Open the Visual Studio Code in our local System, click on the Extensions button from the left side. Now search for the Azure Functions and then click on the Install button as below to install the Azure Functions extension in the Visual Studio Code.

Create Azure Function using Visual Studio Code

Step-2: Now you can able to see, Azure Functions extensions has been installed successfully.

How To Create Azure Functions In Visual Studio Code

Step-3: Now click on the Azure button from the left side and then click on the Create New Project icon from the top as shown below.

How To Create Azure Functions using Visual Studio Code

Step-4: Now, on the Create a new project window, click on the Browse button to select a Project location.

Create Azure Functions In Visual Studio Code

Step-5: Now select a language to build the Azure Function, I am choosing here the language option here as C#. The available language options are Javascript, TypeScript, C#, Python, Java, and PowerShell.

Create Azure Functions using Visual Studio Code

Build an HTTP Trigger Azure Function

Step-6: Now you need to select a trigger for your Azure Function. I will choose here the option as HTTP Trigger.

Develop Azure Functions using Visual Studio Code

Step-7: Provide the Function name and click on the Enter button. Now it will ask for the NameSpace name and then click on the Enter button on the next window.

visual studio code azure functions c#

Step-8: Now select the AccessRights option as Anonymous on the Create new HTTP Trigger window.

visual studio code azure functions using c#

Step-9: Now you can able to see below, the Azure function got created successfully.

create azure function in visual studio code

Run Azure Function Locally using Visual Studio Code

Step-10: Now click on the Run button from the left side or press F5 to run the project.

azure functions c#

Oh, I got the error “Configured debug type ‘coreclr’ is not supported” like below.

azure functions how to create

So to fix the error “Configured debug type ‘coreclr’ is not supported”, Click on the Extensions button from the left side and search for “C# Extensions” and click on the install button to install the C# extensions in Visual Studio Code.

Configured debug type 'coreclr' is not supported

Now You can able to see the C# extension has been successfully installed. You can set the Color Theme by clicking the Set Color Theme if you want.

Configured debug type coreclr is not supported error

Ohk, Now Run the Project again, this time I got the warning “You must have the Azure Functions Core Tools installed to debug your local functions” again. Just click on the Install button.

You must have the Azure Functions Core Tools installed to debug your local functions

Once you will install this, this time the project will run successfully without any issue. You can see below, the project ran successfully.

how to create azure function app in visual studio code

How to locally test Azure functions

Now to check if the function is working, You can copy the function URL, and paste in the Browser. http://localhost:7071/api/MyNewDemoAzureFunction.

Once you will put the URL in the Browser, Now you can able to see the default message “This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.”

run azure functions locally

Ohk now let’s provide a value for the name parameter in the query string. Now you can see we got the expected out Put as below.

Hello, raj. This HTTP triggered function executed successfully.

azure functions how to create using visual studio code

Now let’s test this function on the Postman app. Open the Postman on your local machine, copy and paste this URL and choose the GET option and then click on the Send button. You can able to see we got the expected output with the response code 200 OK.

azure functions how to create in visual studio code

This is the way to Build Azure function in visual studio code. Now time to Deploy Azure function from visual studio code.

How to debug azure functions In Visual Studio Code

Well, we can able to debug the Azure function in Visual Studio Code using simple steps

Put the debugger in the Azure Function as shown below

Debug Azure Function In Visual Studio Code

Now Navigate to the Browser where you are trying to Browse the Function URL with the query string parameter value. The URL is like http://localhost:7071/api/Mynewfunction2?name=raj. What you need to do is just refresh the Browser or select the URL and hit Enter Button. Now if you will go back and check the Function where you put the Breakpoint earlier. It will hit the debugger and if you will mouse over on the name parameter, you can able to see the Value as “raj” there, refer to the above screenshot for more clarity.

debug azure functions locally visual studio code

This is how we can able to Debug Azure Function In Visual Studio Code using the above mentioned steps.

Deploy Azure function from visual studio code

Now we have developed our Azure Function in Visual Studio Code. So the next step is we need to deploy the Azure function from the visual studio code. Follow the below steps for the deployment.

Click on the Azure button from the left side and then click on the Deploy to Function App… icon as highlighted below.

Deploy Azure function from visual studio code

It might ask you to enter your Azure Credentials. Provide the credentials and then once signed in, click on the + Create new Function App in Azure

Deploy Azure function using visual studio code

Now provide an unique name for the Function App and then click on Enter.

How to deploy azure function from visual studio code

Now provide a location for the resource

How to deploy azure function using visual studio code

Now you can see below, the Deployment is completed successfully with out any issue.

How to Deploy Azure function visual studio code

Now to make sure that the Azure function is deployed successfully in the Azure Portal, Log in to the Azure Portal and navigate to the App services and you can able to see the Azure Function app that we have deployed from the Visual Studio Code.

How to publish Azure function from visual studio code

Now click on that, and then on the App service window. click on the Functions link from the left side navigation.

How to publish Azure function using visual studio code

You can able to see the function which we have created from the Visual Studio Code.

publish Azure function using visual studio code

Now the next step is we will check in the Postman app if the function is working fine. To do that click on the function and then click on the Get Function Url from the top.

publish Azure function from visual studio code

On the Get Function Url window, click on the Copy button to copy the Function URL.

publish Azure function in visual studio code

The URL will look like below

https://mynewfunctionapp235.azurewebsites.net/api/MyNewDemoAzureFunction?

Now to test this, Open the Postman app, Paste the above URL and choose the GET operation, then provide a value for the name parameter in the query string and then click on the Send button. You can able to see, we got the expected output with the proper response code without any issue.

Steps to deploy Azure function from visual studio code

Now you can also test the Azure Function in another way inside the Azure Portal itself. Click on the Code + Test link from the left side, You can able to see the function. JSON code. Click on the Test/Run button from the top. Now you can choose the HTTP method and provide the value for Parameter in the Body on the Input window and then click on the Run button.

Steps to deploy Azure function using visual studio code

Now you can able to see, On the Output window, we got the expected output with the proper HTTP response code as below

Steps to publish Azure function from visual studio code

This is the way to Deploy Azure function app from visual studio code using the above mentioned steps.

Create Azure Function with PowerShell

We discussed above how to create Azure Functions using C#. We can also able to create Azure Function with PowerShell. As part of this functionality, we will have to perform the below activities.

  • Need to create a resource group using the PowerShell cmdlet
  • Then we will create a storage account that is the container for all the elements. using the PowerShell cmdlet
  • We need to get the storage account connection string from the Azure Portal using the PowerShell cmdlet.
  • The next is to create a function app using the PowerShell script.
  • Now we need to Set the function app settings.
  • Finally, we need to deploy the function into the Azure Portal.

Create Azure function using PowerShell

Login To Azure

The first step is to login to the Azure using PowerShell with Login-AzureRmAccount PowerShell command.

PS C:\WINDOWS\system32> Login-AzureRmAccount

Now it will prompt you to enter your Azure Credentials. So enter the username and password and then click on the Sign In button.

Create Azure function using PowerShell

Create Azure Resource Group using PowerShell

The next step is to create the Resource Group. Resource group act as a container for all the element. We need to use the below PowerShell cmdlet to create the Azure Resource Group using the PowerShell cmdlet.

$myResourceGroup= 'demofunctiongrp'
$mylocation = 'eastus'
$rsgGrp = Get-AzureRmResourceGroup -Name $myResourceGroup -ErrorAction SilentlyContinue
if (-not $rsgGrp) {
    $rsgGrp = New-AzureRmResourceGroup -Name $myResourceGroup -Location $mylocation
}
Create Azure function in PowerShell

Create Azure Storage Account using PowerShell

The next thing we need is an Azure Storage account. So, use the below PowerShell script to create the Azure Storage Account.

$myStorageAccount = "FunctionDemoStrg123"
$Sku = 'Standard_LRS'
$Paramsstrg = @{
    ResourceGroupName = $myResourceGroup
    AccountName       = $myStorageAccount
    Location          = $mylocation
    SkuName           = $Sku
}
$mystrgAccount = New-AzureRmStorageAccount @Paramsstrg
create azure function app using powershell

Retrieve the storage account connection string from the Azure Portal

Now we need to retrieve the storage account connection string from the Azure Portal that we need to access the Storage account which we need to store the Azure Function Code.

$mystrgacntKey = Get-AzureRmStorageAccountKey -ResourceGroupName $myResourceGroup -AccountName $myStorageAccount |
    Where-Object {$_.KeyName -eq 'Key1'} | Select-Object -ExpandProperty Value
$mystorageacntconstng = "DefaultEndpointsProtocol=https;AccountName=$myStorageAccount;AccountKey=$mystrgacntKey"
create azure function app with powershell

Create the Azure Function App using the PowerShell

Now the next step is to create the Azure Function App using the below PowerShell cmdlet.

$MyNewfunctionApp = 'MyNewAzureDemoFunction'
$Params = @{
    ResourceType      = 'Microsoft.Web/Sites'
    ResourceName      = $MyNewfunctionApp 
    Kind              = 'functionapp'
    Location          = $mylocation
    ResourceGroupName = $myResourceGroup
    Properties        = @{}
    Force             = $true
}
$MuAzureFunctionApp = New-AzureRmResource @Params 
Create the Azure Function App using the PowerShell

Apply the settings for the Function App

Now we need to Set the function app settings.

$newAppSettings = @{
    AzureWebJobDashboard                     = $mystorageacntconstng
    AzureWebJobsStorage                      = $mystorageacntconstng
    FUNCTIONS_EXTENSION_VERSION              = '~1'
    WEBSITE_CONTENTAZUREFILECONNECTIONSTRING = $mystorageacntconstng
    WEBSITE_CONTENTSHARE                     = $myStorageAccount 
}
$paramsdem = @{
    Name = $MyNewfunctionApp 
    ResourceGroupName = $myResourceGroup
    AppSettings = $newAppSettings
}
$mywebApplicatn = Set-AzureRmWebApp @paramsdem
How to create Azure function using PowerShell

Deployment of the Azure Function

Now we will deploy the Azure Function in Azure Portal using the below PowerShell cmdlet.

$mynewfunctionName = 'MyNewFunctionpro'
$mynewfunctionContent = Get-Content ./HelloWorld/run.ps1 -raw
$mynewfunctionSettings = Get-Content ./myNewFunctionpro/function.json |ConvertFrom-Json
$mynewfunctionRSID = '{0}/functions/{1}' -f $MuAzureFunctionApp.resourceid, $mynewfunctionName
$mynewfunctionProp =@{
config = @{bindings = $mynewfunctionSettings.bindings
}
files = @{
'run.ps1' = "$mynewfunctionContent"
}
}
$Params = @{
ResourceID = $mynewfunctionRSID
Properties = $mynewfunctionProp
Apiversion = '2015-08-01'
Force = $true
}
$function = New-AzureRmResource @Params
Create Azure function PowerShell

This is how we can create and deploy the Azure Function using the PowerShell cmdlet.

Open Existing Azure Function In Visual Studio Code

Once you have deployed the Azure Function in the Azure Portal, You can able to open that Azure Function again using Visual Studio Code or Visual Studio IDE, If you want to modify by chance the Azure Function again. You need to follow the below steps to do that.

Step-1: Login to Azure Portal (https://portal.azure.com/)

Step-2: Navigate to Function App that you have Deployed. Click on the one that you want open with Visual Studio Code or VisualStudio.

Open Existing Azure Function In Visual Studio Code

Step-3: Once you click on the Function App, Click on the Switch to classic experience and then click on the Continue to classic experience button.

How to Open Existing Azure Function In Visual Studio Code

Step-4: Now click on the Download app content button to download the your Function app content.

Open Existing Azure Function using Visual Studio Code

Step-5: Now on the Download app content project window, select the Content and Visual Studio project option, and then click on the Download button.

How to Open Existing Azure Function using Visual Studio Code

Step-6: It will prompt you to save the app content zip file in your local path. You can Unzip that file and try opening the folder in your Visual Studio Code or Visual Studio IDE.

Open Existing Azure Function in Visual Studio

This is how you can Open Existing Azure Function In Visual Studio Code by following the above mentioned steps.

You may also like following below articles

Conclusion

Well, In this article, we discussed, Create Azure Function using Visual Studio Code, How To Create Azure Functions In Visual Studio Code, Build Azure function in visual studio code, and then we discussed the steps to Debug Azure Function In Visual Studio Code, Run Azure Function Locally using Visual Studio Code and then finally, we discussed the steps to Deploy Azure function from visual studio code, Deploy Azure function app from visual studio code and Create Azure Function with PowerShell, Open Existing Azure Function In Visual Studio Code. Hope you have enjoyed this article !!!

Leave a Comment