A Beginner’s Introduction to Azure Functions

Richard Rios
Neudesic Innovation
5 min readMay 17, 2024

What are Azure Functions?

Azure Functions are a serverless, event-driven compute platform and has out-of-the-box support for some of the most common programming languages, such as: C#, C# Script, JavaScript, Python, Java, PowerShell, and TypeScript. While these are the most common, Azure Functions is not limited to just these languages as you can create custom handlers!

Using custom handlers, you can create Azure Functions using your favorite languages such as Rust, Go, C++, or whatever language you fancy. Azure Functions are supported in both Windows and Linux environments.

Understanding Event-Driven Architecture

Event-driven refers to the paradigm where the flow of the application is determined by events such as user actions, sensor outputs, or messages from other programs. For example, you are most likely using a web browser to view this article, and may have even clicked a couple links to get here. When you land on a website and find a link you’re interested in viewing, the “click” of that link is an event. This “click” event notifies some event handler (i.e. JavaScript) that a “click” event has occurred. The event triggers a flow of execution behind the scenes that eventually leads you to the page you wanted to visit.

Azure Functions work in a somewhat similar fashion, but instead of a click event kicking off the flow, Azure Functions use Triggers. There are many triggers that can be used in Azure Functions and a table of some of the more common trigger types are in the next section. Additionally, Azure Functions use Input Bindings and Output Bindings. Input Bindings can be used to retrieve additional information from various data sources. For example, you can use an Input Binding to pull data from a Cosmos DB container when the function triggers. Output Bindings are used to pass data to an external source such as a Service Bus. Using multiple output bindings requires a little more finesse as of .NET 8 and is out of scope of this article but is something to be aware of.

Triggers, Input and Output Bindings, oh my!

As previously mentioned, Azure Functions support multiple types of Triggers, Input Bindings, and Output Bindings. Some of the more common types are listed below.

Common Azure Function Bindings

Visit Microsoft for a complete listing of Azure Function Bindings.

Now that we have an idea of what functions are and some ideas for triggers, let’s take a look at an example.

HTTP Trigger

The HTTP Trigger executes a function when a request is received. Below is a simple “Hello, World!” example using .NET 8.

What does all of the above mean, though?

[Function(nameof(MyHttpFunction))] 

Informs the framework that this is an Azure Function that has the name MyHttpFunction.

[HttpTrigger(AuthorizationLevel.Anonymous, “get”)] 

Defines the HTTP trigger with anonymous authorization and only accepts GET requests.

HttpRequestData 

Contains information pertaining to the request, such as the body or query parameters.

FunctionContext 

Object that contains useful information about the current function execution context.

A quick note about AuthorizationLevel

Azure Functions offer different levels of authorization for HTTP Trigger functions and should be used appropriately for the Function usecase.

  • AuthorizationLevel.Anonymous
    - Allows any client to call the function and is a good authorization level for public APIs and webhooks.
  • AuthorizationLevel.Function
    - Requires a function-specific API key to be included in each request.
    - Useful for restricting access to critical operations such as database operations.
  • AuthorizationLevel.Admin
    - Requires the master key of the function and is the most restrictive authorization level.
    - Should be used sparingly.
    - Ideal for restricting access to administrative level operations.

Important Note
Function-specific API keys and Master keys should never be publicly available or stored directly in code. Both key types can be managed directly using the Azure Portal or the Azure CLI.

Let’s run the example and see what happens.

  1. Create a new Azure Function project in your favorite editor. In this example, Visual Studio is used. You can follow the guide below for VSCode.
Creating a new project in Visual Project
Selecting a project type in Visual Studio

Give your Azure Functions project a name.

Visual Studio project configuration

Select the desired framework, we’ll be using .NET 8 and setting the “Authorization Level” to Anonymous. If you do not have .NET 8, it can be downloaded from Microsoft.

.NET Framework selection for new project

Now, let’s rename Function1.cs to MyFirstHttpTrigger.cs.

MyFirstAzFuncs solution listing

Let’s update the [Function(“Function1)] annotation to something more meaningful. Your function may look similar to the below snippet.

Let’s run this in Debug mode (F5) (or Debug > Start Debugging) and see what happens, the MyFirstHttpTrigger.cs code should look like the below snippet.

At this time, a new console window should have opened and given you some information, such as the location of the project, a URL, and some other debug information.

Azure Function running output

We can use the provided URL http://localhost:7073/api/MyFirstHttpTrigger to trigger the function using a GET or POST request. Curl will be used for this example. You can download curl from their homepage linked below.

curl -X http://localhost:7073/api/MyFirstHttpTrigger

This provides us with the result of the function returned from the OkObjectResult(“Hello, world!”) line.

Curl command response

The console window where the function is running should also have some additional information that is written from the _logger object.

Azure Function generated log output

Congratulations, you have successfully created and run your first Azure Function using an HTTP Trigger.

Have more questions about Azure functions or other Azure services? Tag me in the comments! My company, Neudesic, is Microsoft’s global Azure partner of the year, so if I can’t answer them I’ll find someone who can!

--

--