Exploring Azure - Azure Functions
Tags: azure, serverless, azurefunctions
Published: 2021-07-21 08:30:21

In this Article we will be continuing our journey in exploring Azure. On this episode we will explore and dig deep into the theory Azure Functions.

This is Part 1 of 2 where we cover the theory of Azure functions.
You can go to part 2 by following the link below
https://dev.to/moe23/exploring-azure-azure-functions-coding-21gp

You can watch the full video on Youtube
{% youtube a4e1eh6MoSU %}

So what we will cover today:  

- Serverless Cloud Computing
- Traditional Vs Serverless
- What are Azure Functions
- Why do we need it?
- Digging Deep
- Ingredients
- How to build Azure Functions

As always please comment your questions, clarifications and suggestions in the comments down below. Please like, share and subscribe if you like the video. It will really help the channel

### Serverless Cloud Computing

Serverless computing hosts and runs code in the cloud. It is “serverless” in the sense that you do not need to install or maintain servers to run code. Which means No Infrastructure Headache. The infrastructure is already setup and its already configured for us to utilise

Serverless computing eliminates this infrastructure barrier for developers. It eliminates the complexity of managing a server. 

### Traditional Deployment vs Serverless Computing
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ql2bwwjc512rfyzlx61a.png)

When doing a traditional deployment we need to do the following steps

- Get the correct server and configure it
- Add all of the required configuation, install the dependencies and runtime
- Deploy our code
- Configure our code on the server
- Add  and configure security to our server

![https://s3-us-west-2.amazonaws.com/secure.notion-static.com/80172ce3-d641-46f6-a8cd-6c4d5ec67125/traditional-deployment.png](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/80172ce3-d641-46f6-a8cd-6c4d5ec67125/traditional-deployment.png)

But the work doesnt finish after deployment, the list of work that needs to be done after the deployment is as follow 

- Server Management
- Security Monitoring
- Keeping the Server Updated
- Resolve any configuration issues
- Requires some experience in handling the servers
- There are many different types of servers
- Difficult application scaling


The difference comes when we want to utilise Azure functions for deployment:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rfbgajk26r1g6gzmst8u.png)

- Create the Azure Function on Azure and configure it
- Upload our code


The work that needs to be done after Azure Functions 

- No server management
- No server security worries
- Auto Scaling

### What are Azure Functions


The ability to write code whitout setting up infrastructure, no servers, no virtual machine no containers. 

Azure functions are quick to build, easy to maintain and powerful to operate 

They easy serverless functions that can scale automatically to meet demands, without the worry of infrastructure, provision and servers.

### Why do we need it

Azure functions provides the benefits of both PaaS, and IaaS in a single platform. Azure functions is a serverless solution that allows you to write less code, maintain less infrastructure, and save on costs. Instead of worrying about deploying and maintaining servers, the cloud infrastructure provides all the up-to-date resources needed to keep your applications running.

They are small peices of code, which only care about the functionality they are providing. They react to external triggers, we will discuss triggers in more details in later section.

Some of the benefits of utilising Azure functions

- Variety of programing languages: C# - Java - F# - node.js - PHP - Python
- **Azure Service integrations**: It can integrate with different Azure services so the potentials are limitless
- **Supports CI/CD**: Although they are serverless functions, we can still integrate  devOps pipelines.
- **Pay as you go model**: Think about servers you manage in an on-premises data center. You have servers hosting applications, but those applications aren’t always in 
use. But, they are always consuming resources. Using serverless, you only pay for the compute resources needed to execute the code. This consumption model saves costs by only using compute resources when required.
- **Automatic Scaling:** it adds or removes compute resources to meet changes in demand. Compute resources include items such as CPU, memory, and networking.
- **Reliability**: With Azure handling the infrastructure and the server management, we can rest assure the reliability of our application. As in case the app crashes it automatically restarted and running again
- **Flexibility**: We have the flexibility of specifying the max runtime of our app up to 10 min.

### Let us Dig deep

What is the difference between Azure functions, functions app, and functions

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6p5ctd14aztk5xjx482q.png)

When you start working with Azure Functions, you’re going to see many ‘functions’ thrown around. For a beginner, this nomenclature may get confusing fast. There are three ‘function’ names you need to be aware of.

- Azure Functions: This is the name of the service itself, like Azure Automation, Azure Virtual Machines, or Azure Storage Accounts.
- Function App: This is an instance of Azure Functions. It’s a single application.
- Function: This a function defined purely in code. You can have multiple functions inside of a single Function App.

### Triggers

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k6pxroplqf9q56efj503.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ymyin3iwj7lqs6c9exwu.png)![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9e5pn5r224h4c0zykkwq.png)

Azure Functions is “event-driven.” This attribute refers to how the code being executed by Azure Functions starts. A function will start via an event.  The trigger is the event responsible for executing an Azure function, and there are dozens of triggers to choose from. Examples of trigger events include:

- **HTTP trigger**: Receiving an HTTP request, like interacting with an **[API](https://www.ibm.com/cloud/learn/api)** to retrieve the application’s information.
- **Timer trigger**: Running on a schedule, such as sending reminders once a day about upcoming appointments.
- **Azure Blob Storage trigger**: Running on blob storage object creation, such as resizing an image of uploaded images.

### Bindings

Azure Functions is a service that consists of various functions. When running, a function can connect to other Azure services. To do that, a function requires a **[binding](https://docs.microsoft.com/en-us/azure/azure-functions/functions-triggers-bindings?tabs=csharp)**.
A binding is code that “links” one Azure service to another and has a direction (in or out). That binding direction refers to how the “linked” Azure service sends or receives information from the function.

Examples of bindings include:

- **Storage Account**: Access blobs, tables, and queues in a storage account, such as saving records to a table.
- **Azure Cosmos DB**: Interact with records in Cosmos DB, such as updating or creating new documents.
- **Kafka**: send data for operational monitoring data. This involves aggregating statistics from distributed applications to produce centralized feeds of operational data.
- **Service Bus**: send messages to Azure Service Bus messaging service on cloud used to connect any applications, devices, and services running in the cloud to any other applications or services

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cclioti3v12nbqoui73x.png)

### Monitoring and Logging

To monitor your Function Apps, Azure Functions uses **Application Insights**.
Application Insights gathers data generated by the function app, including application traces and events you write within the app.

### Runtime Hosts

The code that we write in an Azure function needs to be known to Azure, so Azure will be able to run the compiled code. But how do we tell Azure which language are we using and which version of compilation we want. 

Azure functions provides runtime host, every function must have a defined *runtime host.*

Runtime hosts determine what language and the version we are using to code our function. Azure Functions currently support three versions of the runtime host: 1.x, 2.x, and 3.x.

Based on Microsoft recommendation we need to use the latest version of the runtime code avaialble. Since it will have the more features and allow us to expand our usage of Azure Functions.

By default when we create a new function it will automatically be in version 3.x as for now;

### Hosting Plans

Even though you don’t have to manage the server running the code in a function, you must still tell Azure a little bit about how you intend to use it. Azure Functions’ infrastructure is defined via hosting plans. 
Hosting plans dictates what operating system the code runs on, its scaling ability, and availability.

The three main hosting plans include:

- **Consumption**: Provides automatic scaling up and down to meet changes in demand. You
only pay for compute resources when the function is running. (Default selection)
- **Premium**: Useful for running Function Apps continuously, access to more CPU and
memory options, and virtual network connectivity than provided in the
Consumption plan. Choose Premium plans for longer runtimes and faster
start times.
- **Dedicated**: Run functions in **App Service plans** for predictive scaling where you can scale up or down manually without
waiting for auto-scaling to take effect. Choose Dedicated plans when you need the function to run continuously.

Please Comment your questions