Complex Azure Function Example (parameters, connection to database, hiding secrets)

Bartłomiej Leja
3 min readJun 29, 2020

Azure Functions are basically serverless apps hosted on Azure cloud. You may ask what the heck is a serverless app so here is the definition 😉

„Serverless applications are event-driven cloud-based systems where application development rely solely on a combination of third-party services, client-side logic and cloud-hosted remote procedure calls (Functions as a Service).”

To follow this tutorial you need to have Visual Studio 2019 with installed Azure development workload (you choose this during the installation of VS 2019). Additionally you must have account on Azure (trial version should work).

We will create a simple Azure Function that will query database (Azure DB) with help of dapper micro-orm. If you don’t know how to create Azure DB you can find nice tutorial here. Azure function endpoint will return a list of faculties on my university with history and year of foundation. Azure function will also accept name of faculty as a parameter and when there won’t be parameter it will return all records.

First, we need to create Azure Function. In vs 2019 you will have many options but the simplest is HTTP trigger

Step by step instructions to create simplest Azure Function you can find here.

Azure function is basically a static class with Run method, where all the magic happens.

Gist above is a fully working “endpoint” that serves data about faculties.

Injecting ExecutionContext gives the possibility to use ConfigurationBuilder (lines 8 to 13). Thanks to ConfigurationBuilder, Azure Function can read connection string from JSON file or from Application settings on azure. Next line 14 is assigning name of faculty parameter to string variable. Line 24 is a classic dapper Query that will return all rows from the database. Lines under dapper query, are formatting output of the query result. Result will be in form of a table.

Nice thing about developing Azure Functions in Visual Studio 2019 is that you can debug it locally. Just hit run in visual studio and you can check your app on localhost.

Publishing Azure Function is easy thanks to visual studio 2019. You just click publish on the project and go through few configuration wizards. This is well described in Microsoft manuals for example here.

The last thing to mention is how to hide your pressure secrets, like connection strings in azure settings. Go to old view in azure portal, click configuration button. In connection string section add your connection string with the same name as in JSON file from Azure Function Solution.

You can find whole code under this address.

List of interesting links:

  1. https://blog.jongallant.com/2018/01/azure-function-config/
  2. https://docs.microsoft.com/en-us/azure/azure-functions/functions-scenario-database-table-cleanup
  3. https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-your-first-function-visual-studio
  4. https://www.sarmaad.com/2016/07/azure-functions-tutorial-sql-database/#lightbox/2
  5. https://docs.microsoft.com/en-us/azure/azure-sql/database/single-database-create-quickstart?tabs=azure-portal

--

--

Bartłomiej Leja

I am a programming enthusiast. Mainly focus on C# and Javascripts programming languages. In my free time, I like to ride a bike and listen to music.