Book Image

Learning Azure Functions

Book Image

Learning Azure Functions

Overview of this book

Functions help you easily run small pieces of code in cloud with Azure functions without worrying about a whole application or the infrastructure to run it. With Azure functions, you can use triggers to execute your code and bindings to simplify the input and output of your code. This book will start with the basics of Azure Functions. You will learn the steps to set up the environment and the tools that we will be using in the further chapters. Once you have a better understanding of this, we will be creating our first hello world function app. Later you will be introduced to triggers, how they are used to activate a function, and how binding can be used to output results of a function.You will also explore the steps to create an assembly with complex functionality that can be used by functions. Next, this book will teach you to scale your functions and use them to process data, integrate systems, and build simple APIs and microservices. Finally, this book will cover some diagnostic techniques with Azure App services and best practices of working with Azure Functions. By the end of this book, you will be well-versed with the techniques of scaling your Azure functions and making the most of serverless architecture.
Table of Contents (17 chapters)
Title Page
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Customer Feedback
Dedication
Preface

Why Azure Functions?


Yes, the immediate question can be what exactly is this Azure Function? In simple English, it is running a function in a cloud environment. It enables us to create a serverless application in a Microsoft Azure environment. Consider a scenario where we know the problem, we know the code that can fix the problem, and we don't want to worry about resources that execute this code. This is the easiest way to focus on logic and business and enhancing the scope of productivity. The biggest benefit is we only need to pay for what we use. Let's understand what exactly in terms of features are provided by the Azure Functions and Azure App Services:

Azure App Services (Web Apps, Mobile Apps), and Functions support different languages and frameworks, DevOps capabilities, scaling, load balancer and etc.

Azure Functions is a service provided by Microsoft for serverless computing. The Azure function is a combination of code plus events plus data. Azure Functions is open source and available on GitHub:

Let's see where Azure Functions is placed in Cloud Service Models in the following figure:

Azure Functions are similar to Azure Webjobs with some differences such as scaling policies, trigger events, and language support. We don't need to worry about infrastructure for execution of the piece of code or function. It is like a FaaS. We can execute Azure Functions in response to events as well.

The languages that are supported are C#, F#, Node.js, Python or PHP, batch, bash, or PowerShell.

There are two types of pricing plans available in the Azure Functions:

  1. Consumption plan: When we execute functions, Microsoft Azure provides all the resources. We don't need to worry about resource management, and we only pay for the time that our functions are executed. The consumption plan pricing includes a monthly free grant of 1 million requests and 400,000 GBs of resource consumption per month. Free grants apply to paid consumption subscriptions only.
  2. App Service plan: This executes functions just the way we execute Azure App Services. We can utilize the same App Service plan created for any application and execute Azure Functions on it without any extra cost.

Having App Service plan as the host for Azure Functions provides lots of benefits that are available with Azure App Services. We can utilize remote debugging, deployment slots, continuous deployment, vertical scaling and horizontal scaling, auto-scaling, and so on. If we use the Azure App Service, then it is a multitenant scenario. If we want to utilize a dedicated environment, then we can utilize the App Service Environment that is a dedicated service from Microsoft Azure, where we can host a function in a virtual network and configure network security groups (NSGs) for an enhanced level of security.

Triggers and bindings are the core of Azure Functions. It allows us to write a function to respond to events that occur in the Azure or other services. As the name suggests, trigger indicates how the function should be invoked and bindings are related to data. Azure Functions can have only one trigger associated with it. Bindings are the connection to the data from within the code available in the function. Unlike triggers, functions can have one or more input and output bindings.

The following table shows the triggers and bindings that are supported with Azure Functions:

Type

Service

Trigger*

Input

Output

Schedule

Azure Functions

Yes

HTTP (REST or Webhook)

Azure Functions

Yes

Yes**

Blob Storage

Azure Storage

Yes

Yes

Yes

Events

Azure Event Hubs

Yes

Yes

Queues

Azure Storage

Yes

Yes

Queues and topics

Azure Service Bus

Yes

Yes

Storage tables

Azure Storage

Yes

Yes

SQL tables

Azure Mobile Apps

Yes

Yes

No-SQL DB

Azure DocumentDB

Yes

Yes

Push Notifications

Azure Notification Hubs

Yes

Twilio SMS Text

Twilio

Yes

SendGrid email

SendGrid

Yes

Note

(* - All triggers have associated input data)

Note

(** - The HTTP output binding requires an HTTP trigger)

Let's try to get to grips with Azure Functions quickly:

  1. Go to https://functions.azure.com/try.
  2. Select Webhook + API as a scenario and select JavaScript as the language.
  3. Click on Create this function:
  4. Select an auth provider.
  1. Select any suitable authentication method and sign in with it:
  2. Wait for few seconds until a free trial fo Azure Functions is ready:
  1. The sample script is ready for Hello World!:
  1. Click on Test on the right side of the browser window. In Request body, provide the name:
  1. Click on Run and scroll down.
  1. See the Output and Status:

So, we have just executed the simple Hello World function. We will execute Azure Functions in a proper Azure subscription in later chapters.

The following are some of the benefits of Azure Functions:

Before we jump to Azure Functions, let's understand some basics of Microsoft Azure cloud. We will discuss the key components and services of Microsoft Azure in the next section.