Functions in a serverless architecture consist of logic that serves a single, well-defined purpose. They are executed using an ephemeral compute service and can be scaled automatically based on demand. Azure Functions is Microsoft’s solution for serverless functions.
— post by Joseph Ingeno, provided by Packt —
There are different ways that a function for Azure can be created, such as using Visual Studio, Visual Studio Code, or the Azure command line interface (Azure CLI). However, so as to make this demo not require the installation of any tools, we will create a function through the Azure portal.
The following steps will be necessary to create a function in the Azure portal:
- Logging in to the Azure portal
- Creating a function app
- Creating a function in the new function app
- Testing the function
- Customizing the function
- Cleaning up resources
Logging in to the Azure portal
If you do not already have one, the first step is to create a free Azure account by navigating to https://azure.microsoft.com. Each account receives some free credit to allow you to try out Azure services.
Once you have an account, you can log in to the Azure portal by going to http://portal.azure.com.
Creating a function app
A function app in Azure is a container that hosts the execution of individual functions. Before we create a function, we must create a function app that will host it.
- In the top left corner, select the Create a resource option, followed by Compute.
- Select Function App.
- Create a function app by providing values for the various settings:
- App Name: A unique name for the function app
- Subscription: The subscription under which the function app will be created
- Resource Group: A resource group is a container that holds resources related to the Azure solution. Create a new resource group or use an existing one for the function
- Hosting Plan: The hosting plan for the function app; The consumption plan is charged on a pay-per-execution basis and dynamically allows resources based on the app’s load. The App Service Plan lets you define a capacity allocation with predictable costs and scale.
- Location: The region where your function app will execute; Select one near you or near other services that your function will need to access.
- Storage: An Azure storage account is used to store and access your Azure Storage data objects. Create a new storage account or select an existing one to be used by the function
- Click the Create button to deploy the new function app. Once the application has been deployed, you will receive a notification.
Creating a function
Once your new function app has been created, we can create a function to be used in the container. We’ll be creating an HTTP triggered function that can be executed via an HTTP call.
- If Function Apps is already under your Favorites, simply select it.
- If it isn’t already in your favorites, select All services. Find Function Apps and click the star next to it to make it one of your favorites. After doing so, select Function Apps.
- You should see the function app that you created previously. Click the Create New icon next to Functions under your function app in order to create a new function.
- To create a new function, we can either select a premade one or create one on our own. In this case, we will use one of the premade ones.
Select Webhook + API and select one of the languages. In this example, I have elected to use C#. Click the Create this function button when you are finished, which will create the function.
If C# was selected as the language, the code for the premade function is as follows:
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
log.Info("C# HTTP trigger function processed a request.");
// parse query parameter
string name = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
if (name == null)
// Get request body
dynamic data = await req.Content.ReadAsAsync<object>();
name = data?.name;
return name == null
? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
: req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
Testing the function
Now that the function has been created, we can test it to confirm that it works properly.
- Your function has now been created and should be visible in the Functions section underneath your function app.
- With your function selected, click the Get function URL link to get the URL for your function.
- The previous step will cause the Get function URL dialog box to be displayed. Select default (Function key) for the Key drop-down, and then click Copy to copy the URL.
The URL contains a key that is required, by default, to access the function over HTTP.
- Paste the URL into a browser. As we saw when looking at the code, there is a query string parameter for a name Add &name=<yourname> to the end of the URL, replacing <yourname> with your actual name. Press the Enter key to execute the request.
If the name that you passed was “Joe,” depending on the browser that you used you would either see the string “Hello Joe” or you would see it in XML format.
Trace information is written to logs during function execution. With the focus being placed on your function in the Azure portal, the contents of the logs can be viewed at the bottom of the page.
Customizing the function
You may want to customize the function that you just created in terms of things like the HTTP methods that are supported by the function and the routing template. Under your specific function, select the Integrate option.
There are various options for the HTTP trigger that can be customized.
Allowed HTTP methods
You can configure your function to support all HTTP methods (in which case the Selected HTTP methods section with its checkboxes is hidden) or selected HTTP methods. In the Selected HTTP methods section, you can choose which HTTP methods you want the function to support (GET, POST, DELETE, HEAD, PATCH, PUT, OPTIONS, and TRACE).
You can choose between the following authorization levels:
With Function selected as the authorization level, an API key is required to access the function. This key was being passed as part of the URL:
If we set the authorization level to Anonymous, our function will be accessible without an API key. This makes the URL look as follows:
You can change the route that is used in order to invoke the function. For example, if you were to enter “/HelloTrigger” in the Route template textbox, instead of using the function name in the URL as is the case by default, the enter route template value will be used instead. The resulting URL is as follows:
The “api” part of the URL is part of the base path prefix and is handled by a global setting.
Cleaning up resources
Resource groups in Azure are containers that hold resources such as function apps, functions, and storage accounts. Deleting a resource group deletes everything that it contains. Once you are done with the demo, you may want to delete the resource group that was used so you are not billed for any resources.
- In the Azure portal, go to the relevant resource group page. One of the ways that this can be done is by selecting Resource groups and then selecting a resource group.
- An alternative approach is to select our function app as we had done previously and select its resource group from there.
- With the resource group in focus, select the Delete resource group
- Follow the instructions and click the Delete
It can take a few minutes, but eventually, the resource group and its contents will be deleted. A notification will appear to let you know when it is done.
Joseph Ingeno is a software architect who has designed and developed many different software applications. During his career, he has worked on projects for a number of different business domains, using a variety of technologies, programming languages, and frameworks. His book, Software Architect’s Handbook, was published by Packt and is now available. The Software Architect’s Handbook is a comprehensive guide to help developers, architects, and senior programmers advance their career in the software architecture domain.