fbpx
Hero Illustration
0 Comments
Mitrais, Software Development

How to Create a Serverless Job Using Azure Functions Timer Trigger

Function as a service (FaaS) is a cloud computing service that offers customers a platform for developing, running, and managing application functionalities without the challenges of building and maintaining the infrastructure generally associated with developing and launching an app. 

The first widely available commercial FaaS AWS Lambda was launched by Amazon in 2014, followed by Google Function and Azure Functions in 2016.

Azure Functions is Microsoft’s serverless cloud FaaS that allows code to be deployed and executed without server infrastructure or configuration. Azure Functions supports many languages, including Python, C#, Java, JavaScript, and TypeScript.

Scalability is a significant advantage of Azure Functions. When using Azure instances, you will use more resources when the execution demand increases and less resources when the execution demand decreases. As a result, it will automatically drop off extra resources.

Advantages of using Azure Functions

1. Azure Functions is a serverless solution.

2. Azure Functions are simple to write and deploy.

3. Azure Functions usage is billed per event.

4. Azure Functions execution is triggered when an event is triggered.

5. Azure Functions require almost no maintenance.

6. Azure Functions can be built, tested, and deployed with a browser in an Azure portal.

7. Azure Functions don’t affect other parts of your website and are simple to upgrade.

8. Azure Functions can communicate with different APIs and use industry standards, databases, and libraries.

Three main methods to trigger Azure Functions

1. Timer trigger: A timer trigger tells Azure that the Function should be executed at specific times.

2. HTTP trigger: An HTTP trigger tells Azure that the Function will be invoked through an HTTP request.

3. Blob storage trigger: A blob storage trigger describes a function whose logic is executed when a file is uploaded or updated in an Azure Blob Storage instance.

Azure Functions Timer Trigger

We will explain how to create, build and deploy a simple Azure Functions timer trigger using Visual Studio 2022 with C# programming language for our demo.

1. Create Azure Functions Timer Trigger Project

On the Create A New Project template page, search for Azure Functions and choose the template for Azure Functions. Then, select Azure Functions and click Next.

After that, configure your project name, path location of the project, and solution name. 

Here is the default class created in the Function1.cs 

Renaming the Function class and Function names to suit your needs is easy. For example, you can define the time interval by utilizing a cron tab expression, which can be found online through various references. The default value for the time interval is 5 minutes so that this code will be executed every 5 minutes. 

Add any other details that you want; this method will do it every 5 minutes. In this example, let’s display “Hello World!” in the timer trigger class.

2. Deploy Azure Function Timer Trigger

There are many methods for deploying Azure Function as part of a CI/CD process. For this section, the deployment process will use the published setting file generated from an Azure instance. Then, import it into the project and publish the project to an Azure example.

In Azure, after creating the Azure Instance, click “Get Publish Profile” to download the publish profile file. 

Go back to Visual Studio, right-click the project, and click “Publish.”

Click “Add a Publish Profile” to create a new publish profile.

Choose “Import Profile” and click “Next.”

Publish a profile that has been downloaded from the Azure instance. Then, click “Finish.” 

A Publish profile will be created in the project. Click the “Publish” button to deploy the Azure Function project to the Azure instance automatically.

You can see the published Azure Function project under the Functions menu bar.

To check the function log, click the function name (in this case, “DisplayHelloWorld.”) Click “Monitor” in the menu bar to show the date and time the Function was executed. Then, click one of the date and time data to show the log.

Conclusion

Azure Function offers two significant benefits: its flexible pricing model that allows you to pay only for the resources you’ve consumed through a pay-as-you-go system and its scalability. However, there are potential disadvantages, such as latency and memory limits. For example, FaaS adds some latency, so for a Fintech high-performance application, it may be a better idea to use non-FaaS functionalities. In addition, you should check your memory and execution time limits when designing critical architectural features.

Also, remember that there is no locally stored data. Therefore, your application has to be stateless. It is good in most uses but can still be considered a limit.

Azure Functions with a timed trigger are the cloud equivalent of Scheduled Tasks and Windows Services. As Azure Functions is in the cloud and easy to use, Azure Functions is a much more practical and maintainable option for any cloud-based application.

References

Microsoft Documentation: https://learn.microsoft.com/en-us/azure/azure-functions/functions-get-started?pivots=programming-language-csharp

Azure Function C Sharp Corner documentation: https://www.c-sharpcorner.com/article/what-is-azure-functions/

https://assist-software.net/blog/pros-and-cons-serverless-computing-faas-comparison-aws-lambda-vs-azure-functions-vs-google

Author:

Ari Hadisuryanto, Software Engineer Analyst Programmer

Contact us to learn more!

Please complete the brief information below and we will follow up shortly.

    ** All fields are required
    Leave a comment