Azure Functions Usage
In this post, I'm going to discuss Azure Functions.
Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. Azure Functions enables you to write serverless code to handle events at scale, with minimal overhead and cost. Azure functions work with both C#, F# and JavaScript functions. You may use many different triggers and binding types supported by Azure functions including monitoring queues. And it works with blob storage, sending emails, and able to develop in Visual Studio or from the command line with a text editor, if you prefer. As an awesome feature, this provides the ability to automate deployments, debug and monitor our functions.
As per the working of Azure Functions, you don't need to write, deploy, run in different places and deploying the application getting it as a big task. Because the Azure Functions gives you every feature to work with different combinations with the support of C# F# codes and JavaScript codes.
Will go through with the implementation of Functions.
In the Azure portal, you can search "Function Apps"
You will need to enter the application name which you can access later through the URL. The resource group also needed with an active subscription. Then you can select either you need to deploy this in Windows or Linux Os. Then with new storage, you can simply create the Azure Function.
When you create the app you can see like this.
After that, you can search with the created name to find your azure function to open the developing of your function.
Then you can open the application. Now you can see your application is now running mode.
With the name you created, you can navigate to your Azure Function using the URL.
Eg: https://testingfunctionappdemo.azurewebsites.net/
In the function section, you can create a new function with your own preferred language. Let's create "HTTP Trigger" which we can write C# or JavaScript or F# code to run a code as our need.
As in the authorization level, there are three options;
- Function: You need to provide function specific API key.
- Annonymous: No need of any authorizations.
- Admin: You need to provide an admin key.
So you can create Function with Annonymous Authorizations.
This will show "run.csx" file where the full code that we need to run. This is the C# script.
The sample c# script will shows. This script will return "Hello {Name}".
Name is the parameter that we need to provide when we calling the URL.
Like: https://testingfunctionappdemo.azurewebsites.net/api/HttpTriggerCSharp1?name=sasindu
Then this will return "Hello Sasindu" in <string> tags as in XML format.
This is the function you created.
Then you can create a trigger inside the function to trigger the function as a timer.
In the timer function, you can set the schedule which we need to trigger the script code inside the trigger function.
Let's edit the time trigger to call the 1st created function.
Use below code :
----------------
using System;
public static async Task Run(TimerInfo myTimer, TraceWriter log)
{
var client = new HttpClient();
var response = await client.GetAsync("https://testingfunctionappdemo.azurewebsites.net/?name=Sasindu");
var result = await response.Content.ReadAsStringAsync();
log.Info($"Executed HttpTriggerCSharp1: {DateTime.Now}, Result: {result}");
}
---------------
//Please use your url as: https://testingfunctionappdemo.azurewebsites.net/
Then you can see the output in the log.
Actually, this is a sample function application.
And in the "run.csx" file, you can edit that script as you need. You can use Azure SQL database queries as well.
To use SQL DB, When you click the Function Apps Name, you can see "OverView" and "Platform Features".
In here, you can find "Application Settings".
Then when you go down, you can see the "Connection String" area.
Here you can add the connection string as in we previously used in other c# applications. And just you can call that connection string in the run.csx file as below:
This is a simple update query.
As you can see we can do many more things with the serverless compute service.
References: /en-us/azure/azure-functions/
Comments
- Anonymous
February 18, 2018
great example, thanks!