n this demo, I am going to show you how to create an Azure Function using PowerShell and Visual Studio Code.
Prerequisites:
To install PowerShell extension for VS Code, open VS code, go to extensions tab and search for PowerShell. Click Install.
Once it gets installed successfully, you will find it alongside the other tabs.
To download and install .NET Core SDK 2.2, you need to go to this link https://dotnet.microsoft.com/download and find this button to download and install.
To install the Azure Functions Core tools, open command prompt and run the command npm install -g azure-functions-core-tools
Now we need to install the Azure Function extension in VS Code. Open VS Code, go to Extensions and search for ‘azure functions’. Select Install.
Restart Visual Studio Code and select the Azure icon on the Activity bar. You should see an Azure Functions area in the Side Bar.
Select the Azure logo to display the Azure: Functions area, and then select the Create New Project icon.
Choose a location for your Functions project workspace and choose Select.
Select the Powershell (preview) as the language for your function app project.
Choose HTTP Trigger as the template for your first function.
Use HttpTrigger as the function name.
And choose an authorization level of Function.
When prompted, choose Add to workspace.
Azure Functions Core Tools integrates with Visual Studio Code to let you run and debug an Azure Functions project locally. To debug your function, insert a call to the Wait-Debugger cmdlet in the function code before you want to attach the debugger.
Press F5 to start the debugging process.
Most of you might get this error. Click Cancel. We need to fix this in order to go ahead.
Go to File –> Preferences –> Settings
Search for PowerShell and select PowerShell Configuration.
Scroll down to find these two settings. Power Shell Default Version and Power Shell Exe Path. In the first one, write ‘PowerShell Core 6’ and in the latter one, write the path as ‘C:Program FilesPowerShell6pwsh.exe’. The settings will be automatically saved.
Close the settings and press F5. This time the debugging would be done successfully. Copy the URL endpoint of your HTTP-triggered function.
Go to the PowerShell terminal which would be running by default. If it is not running, you can start a new one by clicking the + sign.
Append the query string ?name= to this URL, and then use Invoke-RestMethod to execute the request, as follows.
Click Continue.
Since we have placed the Wait Debugger, it will pause there. You can hover the cursor over $name and see that the value has been stored. Click Continue.
In the PowerShell terminal, it will return with the message ‘Hello PowerShell’.
And in the Task terminal, it will return this.
—————————————————————————————————-
Remember to remove any calls to Wait-Debugger before you publish your functions to Azure.
Go to settings once again. Search for Azure Functions. Tick the Azure Functions: Advanced Creation option. This enables the advance creation of Azure Function apps where you can select the values while creating the resource. If you keep this unchecked, it will take the values by default and just ask you for the function app name.
In the Azure: Functions area, select the Deploy to Function App icon. If not signed-in, you are prompted to Sign in to Azure. You can also Create a free Azure account. After successful sign in from the browser, go back to Visual Studio Code.
Next, select an appropriate subscription.
Next, select +Create New Function App in Azure.
Give a globally unique name to your function app.
Select Windows OS.
Select App Service Plan.
Select create new app service plan. In most cases, it will select the name of your function app as the app service plan’s name. Press enter.
Select Free Pricing Tier and location closest to you.
Select PowerShell as runtime. You can see that right now, it is in Preview.
Create a new resource group and to go with the default name in the next step, press enter.
And the same thing with storage account.
You will see the progress of your deployment at the bottom right corner of your VS Code instance.
Once the deployment gets completed, you will see this notification.
Back in the Azure: Functions area, expand the new function app under your subscription. Expand Functions, right-click HttpTrigger, and then choose Copy function URL.
To verify that your published function runs in Azure, execute the following PowerShell command, replacing the Uri parameter with the URL of the HTTPTrigger function you just copied. As before, append the query string &name= to the URL, as in the following example.
Invoke-WebRequest -Method Get -Uri “YourCopiedURL&name=”