Aug 2021Serverless Series: Microservices and Azure Functions⚡
<blockquote>Azure Functions provide us with greater flexibility in our application architecture. Let’s see how we can build a serverless microservices architecture with Azure Functions and Azure API Management.</blockquote>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*6ZOoKck_X5Itd2YE.jpg"><figcaption>image credit: <a href="https://i.pinimg.com/originals/4d/73/e6/4d73e60bca8b09f679be99d6e7a878f7.jpg">dribble.com</a></figcaption></figure><p>These days, adopting a Microservices architecture seems to be commonly implemented through the use of containers. Microservices can be deployed in a number of ways and ultimately the best choice is the one that suits your needs.</p>
<p>In a <a href="https://kylermintah.medium.com/when-do-i-choose-cloud-and-when-datacenter-4e8add8132fa">previous article</a> in this series, I gave a quickstart tutorial for Azure Functions. Today let’s see how one can build out microservices with Azure Functions and some help from Azure API Management (APIM).</p>
<h3>1| Getting Started</h3>
<p><strong>Azure Functions</strong> allow us to leverage trigger-based serverless capabilities to fulfill our development goals without the traditional overhead associated with, hosting, management, scaling, etc.</p>
<p><strong>Azure API Management </strong>is a cloud service that allows developers to manage, secure, and maintain their APIs in-depth including relevant configurations (API gateways, rate-limiting, versioning, and more…) regardless of where the servers are hosted.</p>
<p>To attempt this walkthrough you will need to have:</p>
<ul><li>An active Azure account and subscription. You can <a href="https://azure.microsoft.com/free/?ref=microsoft.com&utm_source=microsoft.com&utm_medium=docs&utm_campaign=visualstudio">create an account for free</a> and <a href="https://azure.microsoft.com/en-us/free/">obtain a free Azure trial subscription</a>.</li></ul>
<h3>2| Architecture Design</h3>
<p>In this walkthrough, we will be going serverless and leveraging APIM to build a simple Microservices architecture building off of the quotes Azure Function we built in <a href="https://kylermintah.medium.com/when-do-i-choose-cloud-and-when-datacenter-4e8add8132fa">this previous article</a>. If you would like to follow along reference that article first!</p>
<h4><strong>Our Microservices</strong></h4>
<p>The focus of this walkthrough is to get a hands-on introduction to APIM and Azure Functions. We will build a very simple microservices architecture.</p>
<p>We will have two services our client applications can access.</p>
<p><strong>Quotes Service </strong>— supplies clients with randomly served quotes.</p>
<p><strong>Author Service — </strong>allows clients to look up information about an Author.</p>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/934/1*AJUtH3X6MX6mh5hm-ulz4g.png"><figcaption>The client application can access both the author and quotes services</figcaption></figure><h3>3| Create Author Service Function</h3>
<p>From the Azure portal, create a new function app and fill out the following basic settings:</p>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/832/1*tdha5VLMcXe3Ri7WcwTHSw.png"></figure><p>You can keep all other settings the same. Select <strong>Review + create </strong>and then select <strong>Create.</strong></p>
<p>Next, open the newly created Function App which I have named <strong>author-quite-service </strong>(you will come up with your own unique name), select <strong>Functions </strong>from the side menu, and then <strong>+Create</strong>.</p>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/717/1*Nlj8RNwBbq75Mxag6XEYqw.png"></figure><p>You will be prompted with the <strong>Create function menu</strong>. To expedite things, select <strong>Develop in portal </strong>for the Development environment option and <strong>HTTP trigger </strong>for the template, and fill out the rest of the prompts like so:</p>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/677/1*I7dwVX1nJs1ymH2nfW6Ygg.png"></figure><p>Once in the portal of the <strong>author-lookup-function, </strong>select <strong>Code + Test</strong> from the side menu so we can implement the service functionality.</p>
<p>Copy the following code into the function body:</p>
<a href="https://medium.com/media/92dcba16605564f051124bc901d189ca/href">https://medium.com/media/92dcba16605564f051124bc901d189ca/href</a><p>Don’t forget to hit <strong>save</strong>! feel free to select <strong>Test/Run </strong>to try out the implementation.</p>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/732/1*Lsl-twIJRe9UHHjniWdKqg.png"></figure><p>Next, modify and visit the following URL to ensure that it is working</p>
<blockquote><em>https://{Your Author Function App Name}.azurewebsites.net/api/author-lookup-function?authorName=Maya%20Angelou</em></blockquote>
<p>Note the addition of the query parameter <strong>authorName </strong>which we can set to one of our authors.</p>
<h3>4| Create Quotes Service Function</h3>
<p>If you haven’t already, check out this <a href="https://kylermintah.medium.com/when-do-i-choose-cloud-and-when-datacenter-4e8add8132fa">previous article</a> where we run through creating the Quotes Azure Function which we will be using as one of our microservices.</p>
<p><a href="https://kylermintah.medium.com/when-do-i-choose-cloud-and-when-datacenter-4e8add8132fa">Serverless Series: Quickstart with Azure Functions ⚡</a></p>
<h3>5| Creating Azure API Management Resource</h3>
<p>In your Azure Portal, select API Management Services and click <strong>+Create</strong>.</p>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/531/1*enw16a-twbkefR6sPX6uZQ.png"><figcaption>If you do not see it, select “Create a resource” and search for it on the next page</figcaption></figure><p>Then proceed to create an API Management Resource with an appropriate set of Basic configuration settings. Here is mine for reference:</p>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/876/1*K70s8OR58Q1rvDsX2Z5ZcQ.png"></figure><p>Note, I am using the <strong>Consumption pricing tier</strong> as it is perfect for a microservices-based architecture. It runs on shared infrastructures, is billed on a per-execution-basis, and can scale up or down with demand.</p>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hbklsFQ6VwSvotMPxqW66g.png"><figcaption>Take note of the Consumption Tier constraints as of August 2021</figcaption></figure><p>We can leave all other settings as they are. Select <strong>Review + create </strong>and then select <strong>Create.</strong></p>
<h3>6| Add Function Apps to API Management Resource</h3>
<p>Now that we have created both our <strong>Author </strong>and <strong>Quotes </strong>services, we will connect them with our APIM in Azure so that we can leverage them through a shared gateway as microservices.</p>
<p>In the Azure portal, open up your quote app APIM. From the side menu, select <strong>APIs</strong>, and then from the blade select <strong>Function App </strong>to add our services.</p>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/860/1*rNGvyyTq8KEPl1OrJVI_Cg.png"></figure><p>From here we can search for our Function Apps and follow the prompts to add them. Make sure to repeat this process for both Function Apps.</p>
<p>Once completed you should be able to access both functions via your APIM gateway.</p>
<h3>7| The Result 🎉</h3>
<p>We now have two endpoints accessible through a single APIM gateway that provide client access to our microservices.</p>
<h4><strong>Quote Service Endpoint Running</strong></h4>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/1023/1*fza5yVf8l9R90iySZIVJ9g.gif"><figcaption>Quote Service endpoint</figcaption></figure><h4>Author Service Endpoint Running</h4>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8MeQ0Iyihc0g49clPCvmzw.gif"><figcaption>Author Service endpoint</figcaption></figure><h4><strong>You can easily leverage your new services in a client application</strong></h4>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/622/1*rrqzM6pQR9LMJck4tAa-JA.gif"><figcaption>Simple Android Application that uses our microservices</figcaption></figure><p>In my last article, I showcased an Android app I put together that used our quote Azure Function. I have now expanded the app model to include our newly created author service!</p>
<p>Our quotes application now has two microservices, one to randomly generate quotes, and another to look up information about the quote author.</p>
<p>This is a simple example that serves as a basic introduction to leveraging Azure Functions in a microservices model and as additional usage and complexity are introduced to the application the model should prove more useful especially when aided by Azure API Management.</p>
<p>I hope this was a helpful introduction to using APIM with Azure Functions. Feel free to reach out if you have any questions or suggestions regarding this article.</p>
<p>Until next time, Happy building! 😀</p>
<h4>Further Reading & Exercises</h4>
<ul>
<li><a href="https://azure.microsoft.com/en-us/solutions/microservice-applications/">Microservices on Azure - What Is Microservices | Microsoft Azure</a></li>
<li><a href="https://azure.microsoft.com/en-us/services/functions/">Azure Functions Serverless Compute | Microsoft Azure</a></li>
<li><a href="https://azure.microsoft.com/en-us/services/api-management/">API Management - Manage APIs | Microsoft Azure</a></li>
</ul>
<img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f2b4435015e9" width="1" height="1" alt="">
Read