DEV Community 👩‍💻👨‍💻

Sameer Jejurkar
Sameer Jejurkar

Posted on • Originally published at blog.cloudbuff.in on

Creating our first Azure Function

In the first article of this series, we had a quick overview of concepts for Azure Functions. In this article we will create a simple function and run it locally.

Prerequisites

Before we can create our first Azure Function, we need to set up the development environment. We will be using the increasingly popular Visual Studio Code as our IDE. Checkout the steps to download and install Visual Studio Code here.

Next, we will install Azure Functions extension for VS Code. Click the Extensions icon on the vertical left menu. Type Azure Functions in the search field, click on Azure Functions from the search results. A new tab will open to the right. Click on Install button to install this extension.

VSCode_azure_func_extension.png

Note that two other extensions - Azure Account and Azure Resources - will also be installed as they are dependencies for Azure Functions.

VSCode_azure_func_ext_dependencies.png

To run and test our functions locally, we need to install Azure Core Functions Tool. See this to download and install this tool.

Our first Azure Function

Next, go to VS Code and open the Command Palette. Use keyboard shortcut Ctrl + Shift + P (use Cmd + Shift + P on Mac) or View > Command Palette... menu option.

VSCode_command_palette.png

After the Command Palette opens, type Azure Functions into text box and select Azure Functions: Create New Project... option.

02_VSCode_create_project.png

Create a new folder called blog-azure-functions. We will use this new folder as our project folder.

VSCode_Workspace_folder.pngNow we will go through a wizard to create the function. The first step is to choose the language for the Azure Function. For this article, we will go with C#.

03_VSCode_function_language.png

The next step will be to select the version of Azure Functions. We will go with version 4 as that is recommended and also happens to be the latest version.

04_VSCode_functions_version.png

Next, we'll choose .NET 6.0 LTS as the framework.

05_VSCode_dotNet_version.png

We want this function to be invoked by making an HTTP request. Hence, we will choose HTTP as the function trigger.

06_VSCode_function_trigger.png

Next, we have to provide a name for our function. We will name it SimpleHttpFunction. 07_VSCode_function_name.png

We will use CloudBuff.Function as the namespace. 08_VSCode_function_namespace.png

The last step is to decide the access rights for the function, i.e., specify the type of authentication required to successfully invoke the function. Since we want this function to be accessible without any type of authentication, we will choose Anonymous.

09_VSCode_function_accessRights.png

VS Code will generate sample code for this function - similar to the screenshot below. Some keywords may be underlined with red, squiggly lines. This indicates that there are some unresolved dependencies or references.

10_VSCode_CSharp_code.png

There will be a small pop-up on the bottom right asking to execute the restore command. Click Restore and that should resolve all references, making the red squiggly lines disappear.

11_VSCode_restore_CSharp.png

Note that the code is created in file SimpleHttpFunction.cs - using the function name that was provided.

12_VSCode_function_code.png

Test the function

Now, it is time to test the function. To run the function, click the option Run Without Debugging under the Run menu.

13_VSCode_run.png

A new Terminal view will be opened and once the function starts, it will show the URL for the function: http://localhost:7071/api/SimpleHttpFunction.

14_VSCode_run_terminal.png

Copy the function URL and paste it in a browser. If a message like the one below is displayed, it means our function was executed successfully.

15_function_invoke_1.png

Now let's modify the URL. Add a name query parameter with Jack as the value. We will get a different response now. 16_function_invoke_2.png

And that's it!! We were able to successfully create a function and test it locally.

To terminate the function, select the stop option from the run/debug bar. 18_stop_function.png

Conclusion

In this article, we created a simple function with HTTP trigger and tested it locally. In the next article, we will deploy this function to Azure cloud.

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.