Overview
Azure functions allow us to run the code in a server-less manner. That means we do not have to create the VM to host the running code nor do we have to create a web application and publish code to the web application to run continuously.
In this article, we will create a simple azure function that responds to incoming requests.
To follow this article, we should take care of the below prerequisites
- Microsoft Azure subscription
If you do not have one, you may go for the Azure Free Tier program: https://azure.microsoft.com/en-us/free/
This will give you an access to Azure for 12 months for free!
- Visual Studio 2017 (version 15.3)
Include the Azure development workload.
Post the installation or upgrade of Visual Studio 2017 to version 15.3, manually update the Visual Studio 2017 tools for Azure Functions.
- Open Visual Studio 2017
- Select Tools menu
- Under Extensions and Updates > Updates > Visual Studio Marketplace > Azure Functions and Web Jobs Tools
- Click Update
The VSIX installer will run to install the updates.
Preferably, close the Visual Studio during the update.
Azure Functions Project Creation in Visual Studio 2017
After we update “Azure Functions and Web Jobs Tools”, we should see Azure Functions project template in Visual Studio. This project can be published to function app in Azure from Visual Studio.
A function app hosts execution of our custom developed azure functions.
- Open Visual Studio
- Click New Project
- Select Visual C# > Cloud > Azure Functions
Note
Function app name must be a valid namespace. Do not use special characters, hyphens, underscores, or non-alphanumeric characters.
- Right-click the project name and select Add > New Item.
- Select Azure Function and click Add.
- Select HttpTrigger.
- Select Anonymous for Access Rights.
- Click OK.
The function created can be accessed by HTTP request from any client.
A class file will be added to your project that has the basic implementation of your Azure function.
Let’s take a close look at the generated file.
- The FunctionName attribute represents the name of the function.
- The HttpTrigger attribute represents the message that triggers the function.
Test the Azure Function locally
Now, we have the basic Azure function ready. Let us test this on local first.
- Click Run or F5 from Visual Studio to execute the Azure function.
- It will prompt you to download and install the Azure Functions CLI tools.
- Click "Yes" to continue.
- This will download and install Azure Functions CLI tools.
- Once installed, now our Azure function is ready to listen.
- Copy the URL of function from the Azure Functions runtime output, highlighted in Red rectangle below (i.e. http://localhost:7071/api/Function1).
- Paste the URL for the HTTP request to any browser.
- Append the query string ?name=<yourname>
- Execute the request.
If you can see the same result, our Azure function is working fine on local and we are ready to publish it to Azure.
Publish Function to Azure
- Right-click project
- Select Publish.
- Choose "Create New" if you do not have any pre-existing Azure Function App.
- Choose “Select Existing” option if you have one already.
- Click Publish.
- The Azure function will be deployed to Azure Function App.
Test function in Azure
You can browse to URL that calls your HTTP triggered function as: