Creating our first Azure Function
Tags: azure, serverless, azurefunctions
Published: 2022-09-25 07:09:21

---
title: Creating our first Azure Function
published: true
date: 2022-09-24 14:09:31 UTC
tags: #azure #serverless #azurefunctions
canonical_url: https://blog.cloudbuff.in/azure-functions-part-2
---

In the [first article](https://dev.to/sjejurkar/up-and-running-with-azure-functions-part-1-1j22) 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](https://code.visualstudio.com/docs/setup/setup-overview).

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](https://cdn.hashnode.com/res/hashnode/image/upload/v1664023792598/a0hIiQ398.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1664024499435/D1m4N3aF5.png)

To run and test our functions locally, we need to install Azure Core Functions Tool. See [this](https://github.com/Azure/azure-functions-core-tools#installing) 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](https://cdn.hashnode.com/res/hashnode/image/upload/v1663864977603/_q_hhIWXX.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1663865842133/PPuLwg_nb.png)

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

![VSCode_Workspace_folder.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664028032993/JlTCBi7or.png)Now 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](https://cdn.hashnode.com/res/hashnode/image/upload/v1663867011976/iETy7tFrl.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1663867101804/Y2iOQrFlk.png)

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

![05_VSCode_dotNet_version.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1663868724987/vAbYL99OQ.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1663868362194/yBAKt9YVB.png)

Next, we have to provide a name for our function. We will name it `SimpleHttpFunction`. ![07_VSCode_function_name.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1663868384520/nU8S-qvi9.png)

We will use `CloudBuff.Function` as the namespace. ![08_VSCode_function_namespace.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1663868399706/3QX4N2y4V.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1663868415974/aCsecuXr9.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1663949381060/6k5QV6NzC.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1663949274231/iTj-140H_.png)

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

![12_VSCode_function_code.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1663949953076/SbqPU4aG4.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1663949681158/71PDLZBS_.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1663950522151/NsNIkjsZg.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1663949713509/HYaBwnnRB.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1663949723698/JcMqm57Hi.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1663951161042/hI2zfknGk.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.