Talk With Angel Garcia!

Talk With Angel Garcia!

a blog about software and beers

Creating a simple Serverless Microservice using Azure Functions and CosmosDB.

If you follow my work you will be familiar with the Microservice we are going to build today, it will serve as the backend for the Xamarin App “CabañasRD Community Edition“.

Let’s start defining some terms, from Microsoft documentation we can quote:

“Serverless computing enables developers to build applications faster by eliminating the need for them to manage infrastructure.

With serverless applications, the cloud service provider automatically provisions, scales, and manages the infrastructure required to run the code.

Serverless functions accelerate development by using an event-driven model, with triggers that automatically execute code to respond to events and bindings to seamlessly integrate additional services. A pay-per-execution model with sub-second billing charges only for the time and resources it takes to execute the code.

This is basically what Azure Functions does, and we are going to use it with Azure Cosmos DB which is a Microsoft’s globally distributed, multi-model database service.

You can see this presentation I made for an Azure course for devs where I covered all you need to know about Azure Functions.

Talk is cheap. Show me the code!

Prerequisites:

  • An Azure account with an active subscription.
  • The Azure CLI version 2.4 or later.
  • The Azure Functions Core Tools version 2.7.1846 or a later 2.x version.

Let’s go!

Open a terminal, it’s time to login to your account though the Azure CLI, execute:

This will prompt the browser for you to login, then you can close the browser and come back to the terminal.

As a good practice for organizing resources we should create a Resource Group (Rg from now) to have all the stuffs relative to our domain, let’s called CabanasRD, if you have multiple subscriptions you must select the one you want to use before creating the Rg, run the following commands:

We will need the CosmosDB before creating our Azure Function, run the following commands for:

  1. Create a CosmosDB account with the free tier benefit activated (this could take up to 15 minutes, be patient).
  2. Create the database of type SQL.
  3. Create the SQL container.
  4. Show the read-only level keys for use it later.

Now, open a browser and go to the Azure Portal, we will need to upload a JSON file manually for populate the database container since there is no command in the CLI for do this.

  1. Go to https://portal.azure.com/
  2. Open the CosmosDB going to Resource Groups ⇒ CabanasRD ⇒ cabanas-cosmos-account
  3. Select the option “Data Explorer” in the left blade.
  4. Under SQL API, select cabanasdb ⇒ cabanascollection and select the “Upload File” option then select this JSON file and upload it.

Cool! the dependencies for running our Azure Function are ready!

Run the following commands to proceed with the creation of the functions with an HTTP Trigger and a binding to the previously created database:

The function code is so simple that we don’t even have to explain it, paste this code in the CabanasRDFunction.cs file:

Add the following connection string to the local.settings.json:

Run it again using the func start command and go to http://localhost:7071/api/cabanas to see the data coming from CosmosDB!

Seems like it’s time to deploy this function to Azure!

The function code needs to be store in an Azure Storage Account, so, we will create it from the command line and then the function, once it’s done we are going to deploy the code, use the following commands to accomplish all the requirements:

If you call the function you will get an internal server error, see the logs… oh! we forgot to add the Connection String to the Function in Azure, go to the portal and open the Function’s configuration, under “Connection Strings” add the same we added before locally.

🕺🏽Yessir, we are done!

One more thing… this will cost you $0.00!

See you in the next post! 🚀

See the code on Github

DROP A COMMENT

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.