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
  • Run Azure Function Locally using Visual Studio Code
  • Deploy Azure function from Visual Studio code
  • Create Azure Function with PowerShell

Subscribe to Our YouTube Channel for more videos

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 start with the development activity.

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 be installed. If you don’t have Visual Studio Code installed on your machine, then install 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 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, and click on the Extensions button from the left side. Now search for the Azure Functions and then click on the Install button below to install the Azure Functions extension in the Visual Studio Code.

vscode azure functions

Step-2: Now you can able to see, that Azure Functions extensions have 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 function 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 the option of 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: Select the AccessRights option as Anonymous on the Create new HTTP Trigger window.

azure function vscode

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.

test azure function locally

So to fix the error “Configured debug type ‘coreclr’ is not supported” ” click on the Extensions button from the left side 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

Okay, 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.

azure functions visual studio code

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

how to create azure function app in visual studio code

How to locally test Azure functions

For testing Azure functions locally, You can copy the function URL, and paste into the Browser. http://localhost:7071/api/MyNewDemoAzureFunction.

Once you put the URL in the Browser, 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

Okay now let’s provide a value for the name parameter in the query string. Now you can see we got the expected outcome 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 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.

testing azure functions locally

This is the way to Build Azure function in Visual Studio code. Now time to Deploy the Azure function from Visual Studio code.

You can also check out: How to Debug Azure Function locally in Visual Studio Code

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

publish function app from visual studio

Now provide a 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

visual studio code azure functions

Now you can see below, that the Deployment is completed successfully without any issues.

azure functions vscode

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.

deploy azure function from visual studio

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

publish Azure function using visual studio code

The next step is to check in the Postman app to see 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 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 issues.

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 the 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 the 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 using 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.

Login To Azure

The first step is to log in to Azure using PowerShell with the 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 groups act as a container for all the elements. 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 Azure Function App using 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.

Create Azure Function using Visual Studio Code – Video Tutorial

Now after the deployment, you can also Edit existing Azure Function in Visual Studio Code.

You may also like the following articles

Conclusion

Well, In this article, we discussed, How To Create Azure Functions In Visual Studio Code, Build Azure functions in Visual Studio Code, and then discuss Run Azure functions locally using Visual Studio Code, and then finally, we discussed the steps to Deploy Azure functions from Visual Studio code, and Create Azure Function with PowerShell. Hope you have enjoyed this article !!!