In one of the projects my client required to have a code/process/business logic to be available on a shared location to be executed automatically/on demand by different applications. Any shared processes/applications like this require to be hosted on a server or machine. In my scenario my client had Azure subscription but was reluctant to acquire/purchase more infrastructure as client’s internal process of acquiring/approving such resources was complex and lengthy. To cut the story short that had to be SERVER LESS!
Azure App function Triggers:
Azure app functions have different types of Triggers available to execute Azure app function code. Triggers are what cause a function to run. A trigger defines how a function is invoked and a function must have exactly one trigger.
I will be using three different triggers in my solution HTTP Trigger, Timer Trigger and Azure service bus queue trigger. I will explain why I have chosen these three later. Lets first see what each trigger is used for.
HTTP Trigger: Execute a function when an HTTP request is received.
Timer Trigger: Execute a function at a set interval.
Azure Service Bus trigger: Run a function when a Service Bus queue or topic message is created.
Now let me share the complete requirement
1. Web/Mobile applications should be able to execute the shared code synchronously
2. Web/Mobile application should be able to execute the shared code Asynchronously
3. A scheduled process should be able to execute this code.
I cannot use just one type of trigger-function to address all three scenarios (Synchronous, Asynchronous and scheduled process) mentioned above. So I will use three types of triggers but my shared logic will be placed only in one of the Azure app function as this is the main requirement to have a code logic in only one place and shared with different applications.
1. So for first requirement which requires Synchronous processing, I will use HTTP Trigger. Applications will send http request to the Azure app function which will trigger the shared function code. Also I will place my shared business logic/code in HTTP trigger function. This way the code can be easily called and executed by other triggers. You can enforce Azure Active Directory (AAD) authentication on this HTTP trigger as well by following few steps.
Below diagram shows how applications are sending request to the azure app shared function.
2. For second requirement which requires asynchronous execution. I will use Azure service bus queue and Service Bus Trigger. So applications will write messages on Azure service bus queue which will trigger its associated Azure function. As mentioned above my shared code logic is in the HTTP trigger function. The service bus trigger will call the HTTP trigger by sending http request to execute the shared logic.
Below diagram shows how applications are sending request
3. Third requirements require a scheduled process to execute the shared function. I will use Time trigger to implement this requirement. Timed trigger will execute this shared logic at a defined time. In my scenario Time trigger should update an on-premise database with the response of trigger. In below diagram you can see a complete design of this solution.
A complete diagram will look like this
Lets see how easy it is to develop and deploy an App Function from Visual Studio:
I will give example of HTTP trigger using C# language and visual studio. The other two triggers have similar way to develop but some other configuration steps.
You can also create functions and triggers directly from Azure portal.
If you don’t have an Azure subscription, create a free account before you begin https://azure.microsoft.com/en-us/free/.
In visual studio 2019 create a project and then search for Azure Functions. Select Azure functions and click Next.
Give a name to your function and create it.
On next screen select HTTP Trigger
You will see sample code generated by VS
Test this code by running the application and adding the query string ?name=anyname on the browser as shown in image below.
Publish in Azure from VS
In Solution Explorer, right-click the project and select Publish.
In Pick a publish target, use the publish options Azure Functions Consumption Plan
Select Create Profile. If you haven’t already signed-in to your Azure account from Visual Studio, select Sign-in. You can also create a free Azure account.
In App Service: Create new and then fill the information below.
1. Select Create to create a function app and its related resources in Azure with these settings and deploy your function project code.
2. Select Publish and wait for the deployment to complete.
After the deployment completes the root URL of the function app in Azure is shown in the Publish tab.
3. In the Publish tab, choose Manage in Cloud Explorer. This opens the new function app Azure resource in Cloud Explorer.
Cloud Explorer lets you use Visual Studio to view the contents of the site, start and stop the function app, and browse directly to function app resources on Azure and in the Azure portal.
Few Important things:
- On premise database Server access in Azure:
To access any on premise database you have to use Hybrid Connections in Azure. https://docs.microsoft.com/en-us/azure/app-service/app-service-hybrid-connections
- Automatic scaling
Azure Functions uses a component called the scale controller to monitor the rate of events and determine whether to scale out or scale in.
If you expect your function to be executed by multiple clients, it would be prudent to estimate the usage and calculate the cost of using functions accordingly. It might be cheaper to host your service on a VM. If you know your maximum possible number of executions, you can estimate your cost by using below link. https://azure.microsoft.com/en-us/pricing/details/functions/
- If your functions take more than 10 minutes
Then you can go for Durable functions options. Durable Functions allows you to orchestrate the executions of multiple functions using different patterns without any timeout. You can get more information on link below.