Azure Functions: A Comprehensive Guide For Beginners

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

AZURE FUNCTIONS

A comprehensive guide for beginners


Overview
Data and services are relatively straightforward to manage
together, but add in a sprinkling of interactive programming, and
you have a more complex solution. Cloud services have matured to
the point where they need programming to link systems together.
To do this, one of the largest cloud providers, Microsoft Azure,
introduced Functions—a toolset to run programming in the cloud.
Whole systems can now be written and managed through scalable
cloud services.

In this ebook, we’ll dig into the following topics:

Serverless computing

Understanding Azure Functions

How do you write Azure Functions?

Managing Azure Functions

Understanding the future of programming in the cloud

By the time you have read this eBook, you will have a strong under-
standing of how to develop serverless solutions.

2 | www.simplilearn.com
What is an Azure
Function?
Writing Azure Functions is new, which presents excellent “early
adopter” opportunities. Many companies are still deciding
whether or not to adopt cloud services and have not made the
pivot to programming in the cloud. At its core, Azure Functions
programming manages events that drive functions executed on
the cloud. In this section, you will learn what serverless computing
is, what an event-driven architecture is, how to program in the
cloud, and the history of Azure Functions.

Serverless computing
Serverless computing is a revolution in software and hardware
design—one that provides us the power to be truly autonomous,
powered by machine intelligence. IT has used the same familiar
tools of management and deployment, with one differentiator—
now they can run applications serverless without the need for a
traditional IT admin. Serverless computing is already being used
by the world's most extensive cloud services, like Azure, and is
rapidly growing in popularity with companies like Uber, Airbnb,
and Netflix.

Serverless computing is not a one-time thing. Already, thousands


of software developers are using this technique to build their
apps and services. Now, the technology is being applied in the
development of online stores.

But how does serverless computing work? In simple terms, it's a


way for a software developer to avoid managing an entire server
for every aspect of their application. The cloud provider will host
and control functions.

In essence, the code does not live on a server or computer in the


same way as traditional code for APIs, websites, or apps. The code

3 | www.simplilearn.com
is compiled and runs on the cloud as needed. Running code on the
cloud provides the same benefits of scale as other cloud services
such as a database. If there is a need for a more massive database,
the cloud will automatically scale to support the demand. In the
same way, programming in the cloud will scale to meet demand.

Event-driven architecture
The functions written with Azure Functions and other cloud
programming languages are event-driven. This means that you've
got basic controls such as clicks, but you also get things like
sockets, files, and custom functions. You can use these as event
channels.

It's also effortless to pass data into any of these functions using
the SignalR library. So, for example:

transaction.post(_ => this._handleTransactionEvent(transaction),


err => this._handleErrorEvent(err)

This will send a signal to the program that a transaction has been
processed. It will also create an error event to indicate if the
transaction failed.

If you want more control over which functions are called, you can
add additional features.

Why was Azure Functions


introduced
The reason Azure Functions was developed by Microsoft
is to enable developers to build applications in a serverless
environment. The purpose of Azure Functions is to simplify
developing smaller, on-demand applications that are responsive
to events. For example, Microsoft launches an Azure Function
instance within milliseconds of the start of a game.

Azure Functions was initially designed for use cases such as


image or object uploads to cloud storage, updates to databases,
responding to website clicks, or reacting to sensor readings from

4 | www.simplilearn.com
an IoT connected device such as a Raspberry Pi. Azure Functions
can also be used to manage back-end services triggered by
custom HTTP requests, and "spin down" services when not in use,
to effectively manage the resource.

Each Function is securely running a native Linux executable. There


are many ways to write functions. Programming languages like
Node.js, Python, Java, Go, Ruby,[3], and C# (through .NET Core)
are all officially supported. In late 2018, Azure introduced custom
runtime support, giving developers the ability to run a function in
the language of their choice if the language supports the runtime.

How to call an Azure Function


Moving beyond the basics, you should know where and how you
can call Azure Functions functions. There are three ways:

Through services that generate queues or data streams

Synchronously

Asynchronously

You can create event source mappings in Azure Functions


and give functions permissions to access other services in the
execution role for services that generate queues or data streams.
For example, AWS Lambda reads data from other services, creates
events, and calls your functions.

Other services call your functions directly. You can grant


permissions to other services in the function's resource-based
policy and configure other services to generate events and call
your functions. The call can be synchronous or asynchronous,
depending on the service. For synchronous calls, other services
wait for a response from your function and may retry if they
encounter an error.

Azure Functions queue the event before passing it to your function


for asynchronous calls. Other services will get a successful
response, and they have no idea what will happen next once the
event is placed in a queue. Lambda is responsible for retrying
and may send failed events to the dead letter queue that you can
configure in the event of an error.

5 | www.simplilearn.com
In other words, Azure provides you, the developer, many ways in
which to interact with functions.

Working with the Azure Portal


The Azure Portal is the central location you will go to manage your
Lambda functions. To access and manage your Lambda functions,
you will need to take the following steps:

1. Go to https://portal.azure.com/#home

2. From “Services” select Functions

3. You will now be able to view all of your functions.

In the next section, you will create your first function. If already
have any functions, you will see them listed on the screen. If you
want to create a new function, you can do so by selecting the
“Create function” button. More on that in the next section.

6 | www.simplilearn.com
Writing your first Azure
function
In this section, you are going to write your first function. You will
need to have an active Azure account for the following steps to
work. Signing up is easy and you do not need to use your credit
card.

1. To get started you need to go to Azure Portal (https://portal.


azure.com/) as shown below.

2. Select the “Create a Resource” button.

3. Then, you will go to the Azure Marketplace where you can


search for any supported resource. For this example you are
going to select “Function App”.

7 | www.simplilearn.com
4. We are going to make it easy to get a basic function up and
running. There are a couple of fields you need to enter. The
first will be a “Resource Group”. Go ahead and name yours
“MyFunctionApp.”

8 | www.simplilearn.com
5. You will need to give your app a unique name. In this example,
the name is “SimpliLearnFunction”.

6. Publish as “Code”.

7. Choose the runtime stack “.NET Core”

9 | www.simplilearn.com
8. Press the “Review + Create” button.

9. You will be asked to review your settings. Select the “Create”


button if you agree with the settings.

10 | www.simplilearn.com
10. It will take up to five minutes to create your default app.

11. You will receive a notification to go to your Resources. Your


new app should look like the following:

12. From the center of the screen, select the “New Function”
button to add a function to the app.

13. You will be offered several ways to create a function. For now,
select the “In-Portal” option.

14. We are going to set up a simple HTTP Rest API. To do that,


choose “Webhook+API”. There are many other options you can
explore later.

15. The following placeholder code will be generated:

11 | www.simplilearn.com
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static async Task<IActionResult> Run(HttpRequest req,


ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a re-
quest.");

string name = req.Query["name"];

string requestBody = await new StreamReader(req.Body).Read-


ToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;

return name != null


? (ActionResult)new OkObjectResult($"Hello, {name}")
: new BadRequestObjectResult("Please pass a name on the
query string or in the request body");
}

16. You can test this code. Select “get function URL” to copy the
URL.

17. Paste the URL into a Web browser window. By default you will
get the following error:

18. The get a positive response add the following variable that the
URL is looking to have parsed into the code “&name=Matt” and
then hit return to the following:

19. Congratulations, you have created your first function!

There is a lot more you can do with functions, but this gives you a
basic understanding.

12 | www.simplilearn.com
Managing Fees
Nothing is free, but Microsoft does do its best to keep fees
transparent. Azure only charges you for what you use. This is
the model applied across most Azure services. Azure Functions
generates revenue for Microsoft each time a function is run. It only
takes milliseconds for an event to run (typically 200 milliseconds)
with a cost for each event. Each event is known as a Request.

Also, Microsoft will charge you for how long it takes to run a
function. The final charge is Request x Duration.

Free is only free with certain


conditions
The Azure free usage tier comes with 1M free requests per month
and 400,000 GB-seconds of compute time per month. This seems
like a lot, but "buyer be warned." Your Lambda functions can
increase very quickly. Let's take the following example:

If you have 100 concurrent users, 100 functions are processed


each microsecond, and it can process 400,000 events/second. So
theoretically, you should expect 100’s of commands per second
for this handler. However, there is usually not only one handler/
event per user. You also need to consider not just the number
of events, but also the number of requests/users for each result.
For example, what if you have 100 concurrent users, but only 50
requestors are sending 200 requests per second to the handler?

Also, your free tier can have time limitations. Use the Portal to
keep a close eye on how you use your Azure Functions.

The future of Azure Functions is


both bright and cloudy.
When Azure Functions made its public debut, people said it was

13 | www.simplilearn.com
overhyped and that there was no reason to be excited about it. For
one, functions were written in JavaScript, so they required server-
side scripting. While this made the API harder to use, the platform
was also viewed as a hassle for developers. Developers would end
up rewriting hundreds of lines of code with snippets of JavaScript,
then writing function invocations to move the function execution
forward. However, this was done without any serious rework to the
Lambda code itself, so the system was held up for a while.

Over the last couple of years, the introduction of a broad range of


compilers that give developers the opportunity to write functions
in almost any language, coupled with the "pay for computing
time when the function is run" model, more and more people are
building solutions to run in a serverless environment. To this end,
we are now seeing Lambda being used for the following:

Web and mobile applications

Internet of Things

Artificial Intelligence and machine learning

Codeless development

Each of these areas is exploding with opportunity.

14 | www.simplilearn.com
The Future of Azure
Functions
The future of Functions (and I/O-heavy scripts) can be split into
three key areas:

Scaling across Azure regions

Increased capabilities

Making it easy to create solutions with JavaScript

Scaling out across multiple Azure regions: Azure is currently


reviewing how to limit where and how much you can scale your
functions across Azure regions. This would help to scale to areas
that are larger than what Microsoft offers currently (America, Asia,
Europe, etc.). As it turns out, this will be a key enabler for certain
new features including:

Automating cloud-connected solutions

Connecting DevSecOps tools

Synchronizing portability between hybrid cloud environments

If Microsoft can make this work, we will likely see more and more
"size" in our usage of Azure.

Having limits to the size of your functions will also make scaling to
new regions a lot easier than scaling in existing regions, which will
be a big win for users and developers.

These updates lead to an exciting future of JavaScript event-driven


platforms to automate the build and delivery of quality services.
It's compelling that JavaScript developers can easily focus on
building great APIs rather than worrying about the infrastructure
to support them.

A common misconception is that more web workers will require


more resources. However, this isn't the case at all. With several
websites being hosted on Azure, there are very few resources
being used to run them all. Rather, you are using only the
resources necessary to run the backend, creating a highly available

15 | www.simplilearn.com
and reliable architecture. This provides more resources to the
front-end developers in terms of modules and components, which
makes framework deployment efforts much easier.

In short, what you can expect is that Azure Functions will continue
to expand in scope and capability as developers explore the full
potential of serverless applications.

Learn more about working


with Azure
If you want to get started on an exciting career in cloud
computing, check out our Microsoft Azure Fundamentals Training
program. If you’re already familiar with the basics of the cloud and
Azure, you can boost your career through our Microsoft Azure
Architect Technologies AZ-300 Training course which will prepare
you for the certification exam. These are just a couple of options
in our extensive offering in Cloud Computing training. What are
you waiting for?

16 | www.simplilearn.com
Founded in 2009, Simplilearn is one of the world’s leading providers of online training for Digital
Marketing, Cloud Computing, Project Management, Data Science, IT Service Management,
Software Development and many other emerging technologies. Based in Bangalore, India, San
Francisco, California, and Raleigh, North Carolina, Simplilearn partners with companies and
individuals to address their unique needs, providing training and coaching to help working
professionals meet their career goals. Simplilearn has enabled over 1 million professionals and
companies across 150+ countries train,certify and upskill their employees.

Simplilearn’s 400+ training courses are designed and updated by world-class industry experts.
Their blended learning approach combines e-learning classes, instructor-led live virtual
classrooms, applied learning projects, and 24/7 teaching assistance. More than 40 global
training organizations have recognized Simplilearn as an official provider of certification
training. The company has been named the 8th most influential education brand in the world by
LinkedIn.

For more information, visit www.simplilearn.com.

© 2009-2020 - Simplilearn Solutions. All Rights Reserved. | The certification names are the
trademarks of their respective owners.

17 | www.simplilearn.com

You might also like