[Tutorial] Azure Functions
Why use Azure Functions
Here are some of the reasons why you should use azure functions.
- Azure functions are lightweight and serverless.
- Azure functions are easier to write and deploy.
- Azure functions are fast to execute because there is no large application, startup time, initialization, and other events fired before the code is executed.
- Azure functions' execution is triggered when an event is fired.
- Azure functions are compute-on-demand, and that is scalable. When the demand for the execution increases, more resources are allocated automatically to the service, and when requests fall, all extra resources and application instances drop off automatically.
- Azure functions support programming languages, including ding C#, F#, Java, JavaScript, TypeScript, and Python. You choose your choice of language.
- Azure functions do not need any infrastructure and have 0 maintenance.
- The azure function can be built, tested, and deployed in the Azure portal using a browser.
- Azure functions are easy to upgrade and don't affect other parts of the website.
- Azure functions use industry standards and can communicate with other APIs, databases, and libraries.
Create an Azure Functions using Visual Studio 2019
Before we go too far, let's create Azure Functions using Visual Studio 2019.
Open your Visual Studio 2019 and create a new project.
On the new project template page, search for azure functions and see a template for Azure Functions. See below.
Select the Azure Functions template and click Next.
On the below screen, you need to give your functions application a name and select a location where the VS project is goiwille.
You may also see from the above screen that the function is created using C# and have Ahas and Cloud tags means, which will be deployed and run on Azure cloud.
Click Create button.
On the next screen, we need to select what kind of application it will be. Azure functions have this concept of triggers. A trigger is what causes a function to run. Each function has precisely one trigger.
Let's take a look at the below screen that shows various triggers available in Visual Studio for an Azure functions app.
As you can see, each trigger has its spic purpose.
For now, let us select Http Trigger that is executed whenever an HTTP request is made.
Click on Create button to create our app.
We land in Visual Studio with a default file, Function1.cs, and the default function code.
Here is the complete code of my function.
That was the default code of our Azure functions.
Now, let's build and execute it.
Visual Studio provides an Azure Storage emulator to run and test azure functions locally.
Build and Run the application using F5.
You will see the Azure Storage emulator is being started.
The next screen will be func.exe show, ng this message that your function is ready and can be used.
As you can see from the above screen, the Azure functions, Function 1 has GET, POST and the URL is http://localhost:7071/api/Function1. This is the URL you can use to execute the function.
Let's type this URL in our local browser and pass a query string with a value. Then, the output looks like the following: the function takes the query string input and displays it on the screen.
That is all. You just completed the Hello Azure Functions application.
Publish Azure Function to Azure
You can publish directly h your Azure functions app now in Visual Studio or Visual Studio Code.
In my Visual Studio project, right-click on the project name and select Publish.
It will launch the Publish screen, where you may select Azure, Docker, or other folders where you may wish to deploy your app.
In our case, I select Azure.
On the next screen, you will need to select Azure function app operating system, Windows, Linux, or App Container.
I select Windows.
On the next screen, you will have to provide the name of the function app, an Azure subscription, Resource group, Plan Type, Location, and Azure Storage to deploy and execute an Azure functions app.
Click on Create button will create a new App Service.
This process may take a while. Wait until it's done and you see the below screen.
On the below screen, you will see the Function instance. Click on the Finish button.
Once finished, you will go back to the Publish page, where you can see the Site URL, configuration, and a User name and Password automatically assigned to the functions app. You may want to COPY the Site URL for later use.
Click on Publish button.
The Publish process will start.
Once the publishing is finished, you will see Publish succeeded message.
To ensure that the function app is published, you can now copy the Site URL into a browser. My URL was this,
https://hellofunction20210205221452.azurewebsites.net/
You ..will see a message that your functions app is up and running.
Now, you're ready to consume this function app.
When to use Azure Functions?
Azure functions service is a lightweight, serverless compute service with its own use. You can't replace a large website with Azure functions.
Here are some of the use cases of Azure functions,
- Scheduled Tasks
- Reminders and Notifications
- Lightweight Web API
Azure functions are best suited for smaller apps with vents that can work independently of other websites. Some standard azure functions include sending emails, starting backup, order processing, task scheduling such as database cleanup, sending notifications, messages, and IoT data processing.
No comments:
Post a Comment