Serverless — from the beginning, using Azure functions ( Azure portal ), part I

Follow me on Twitter, happy to take your suggestions on topics or improvements /Chris

Serverless is Cloud-computing execution model in which the cloud provider runs the server, and dynamically manages the allocation of machine resources. So essentially you can focus on writing code as your Cloud Provider does the rest

This is the first part of this series:

  • Serverless — from the beginning, using Azure functions ( Azure portal ), part I, you are here
  • Serverless — from the beginning, using Azure functions ( Azure portal ), part II, we will cover how to use our Portal IDE more efficiently
  • Serverless — from the beginning, using Azure functions ( VS Code ), part III, in progress
  • Serverless — from the beginning, using Azure functions ( Azure CLI ), part IV, in progress

In this article we will cover the following:

  • Serverless , What is Serverless and why it may be a good choice
  • Function apps , triggers, and bindings
  • Functions in function apps, Here we will cover things like languages it supports, authoring choices, testing it out, monitoring logging and setting of authorization level and much more

We already assume that putting your apps in the Cloud is a given. With that we mean you’ve seen the benefits that mean in terms of not having to maintain hardware, only pay for what you actually use and so on.

TLDR; if you just want to learn how to build an Azure function, and don’t want a long speech on theory, then jump to the headline called “Creating a function app”, you can always read some Serverless and Azure theory later 😃

Resources

There is so much to learn on this topic and there are some great docs as well as LEARN modules to help you in your learning process:

Azure function LEARN modules

The many choices in the Cloud

Now, being in the Cloud means you have options, A LOT of options in fact. You can be on the lowest level deciding exactly what memory, or hard drive type, your apps can run on. Then you can be on a more managed level where you are happy to create a Virtual Machine, a so-called VM, where you can install the OS and software you need. There are still more steps on this ladder namely running your applications in App Services where you don’t have a VM anymore, just a place for your code to reside and yes you can decide what OS to run this on but that’s pretty much it, it’s a SaaS, software as a Service platform. BUT, there is a step above that — Serverless.

Introduction to Serverless

So what does Serverless mean? Serverless is Cloud-computing execution model in which the cloud provider runs the server, and dynamically manages the allocation of machine resources. So essentially you can focus on writing code as your Cloud Provider does the rest.

FaaS — function as a service

A FaaS, function as a service is a category of Cloud Computing Services and is about offering a Platform where the infrastructure is maintained and your main worry is the code you write. An application built following the above model is said to use a serverless architecture

All the great Cloud providers have a FaaS offering:

  • AWS , has AWS Lambda
  • Google , has Google Cloud Functions
  • IBM/Apache , has OpenWhisk
  • Oracle , has Cloud Fn
  • Microsoft , has Microsoft Azure Functions

Ok, what benefits does it offer then?

  • Everything is functions , Your business logic runs as functions
  • NO Manual provisioning , you don’t have to manually provision or scale infrastructure.
  • Managed infrastructure , The cloud provider manages infrastructure.
  • Automatic scaling , Your app is automatically scaled out or down depending on load.

Serverless on Azure

Azure has two kinds of approaches for Serverless architecture

  • Azure Logic Apps, intro, Azure Logic Apps enables you to create powerful workflows
  • Azure Functions, Azure Functions is a serverless application platform. Developers can host business logic that can be executed without provisioning infrastructure

What else is there to know about Serverless?

Is it all unicorns and rainbows?

Well, Serverless is definitely great but there are some things we need to know about them like:

  • They are stateless , function instances are created and destroyed on demand. If state is required, it can be stored in an associated storage service
  • They are event-driven , they run only in response to an event (called a “trigger”), such as receiving an HTTP request, or a message being added to a queue. So essentially you declare where data comes from and where it goes. You do this declaratively with something called bindings which means you don’t need to code to talk to queues, blobs, hubs, only business logic is needed

They do have Drawbacks , drawbacks are in the form of limitations on execution time and execution frequency.

  • Timeout , The timeout is 5 minutes, This timeout is configurable to a maximum of 10 minutes. If your function requires more than 10 minutes to execute, you can host it on a VM. Additionally, if your service is initiated through an HTTP request and you expect that value as an HTTP response, the timeout is further restricted to 2.5 minutes, BUT there’s also an option called Durable Functions that allows you to orchestrate the executions of multiple functions without any timeout
  • Execution frequency , If you expect your function to be executed continuously by multiple clients, it would be prudent to estimate the usage and calculate the cost of using functions accordingly. It might be cheaper to host your service on a VM

Serverless vs App Services

It’s easy to think that your first go-to, for putting apps in Azure, is AppService that fits most likely with your mental model as a developers, you want to move the app that you have from On-Premise to the Cloud and to do so you need to provision databases, creating your Services in App Service and that’s it right? Well, most applications are seldom that simple, they tend to need to talk to a number of subsystems to maybe log in, or grab a piece of data somewhere or perform a computation.

All these side things are maybe the concern of more than one app in your ecosystem so it makes sense to move them out into separate services. Then you might realize you only need to call on these services very seldom like when a new user is created or there is an incoming request. Your response at that point is maybe to place that incoming message on a Queue, or insert a row in a Database or maybe create a Slack notification.

What we are saying here is that maybe we don’t need to pay for a full AppService and the uptime and responsiveness it gives us, but instead we need a framework that can trigger a function based on a predefined event and that can then carry out a computation that results in a side effect like calling another service/database/queue/whatever.

Now we have come to the sweet spot where Serverless really shines, seldom called services that need to do something in response to some kind of event happening.

In a word

Serverless computing helps solve the allocation problem by scaling up or down automatically, and you’re only billed when your function is processing work. Speaking of billed, if your FaaS is reading/writing towards a storage that you provisioned, then you are paying for function execution as well as that storage, so keep that in mind authoring your FaaS.

What is a function app?

Functions are hosted in an execution context called a function app. Which means what? Think of the Function app as the project you host your functions in.

Prerequisites

Ok, there are some things that need to exist before we can get our function up there in the cloud. Those are:

  • Service plan , There are two choices of plans Consumption service plan, CSP and Azure App Service plan, ASP CSP has automatic scaling and bills you when your functions are running and configurable timeout period for the execution of a function. By default, it is 5 minute, ASP allows you to avoid timeout periods by having your function run continuously on a VM that you define
  • Storage account , function app must be linked to a storage account. It uses this for internal operations such as logging function executions and managing execution triggers. Also, function code and configuration file are stored here

Creating a function app

Now there are different ways of creating a Function app, namely:

  • Portal , Using the Azure Portal
  • CLI , Using the Azure CLI
  • VS Code , Using VS Code to scaffold an Azure Function app and Azure Functions using some amazing plugins made for the purpose.

In this article, we will focus on the first option but in doing so we will put some focus on some great concepts you need to know about, so stay with me cause we are about to do some coding next:

Select the correct template, Function App

Let’s head to the portal and log in at

portal.azure.com

Once logged in select the following :

So that's, selecting Create a resource, followed by Compute and finally selecting the Function App template.

Make the choices in the template

Once we’ve selected the Function app template we need to select a few more things. Your UI will at this point look something like this:

Ok so the following choices need to be made:

  • Enter an app name , App name must be globally unique as it will serve as part of base URL
  • Select a subscription , well pick out one of the ones you have
  • Select a resource group , you can choose an existing resource group or create a new one, up to you
  • Select an OS , choices here are Windows or Linux, we opt for Windows cause we need to select something 😃
  • Select Hosting plan , this should be Consumption plan, we mentioned Consumption plans earlier in this article and why it’s the better choice
  • Select geography , well select the region closest to you
  • Runtime stack , this is the language you are going to be coding in so we select Javascript
  • Create a new storage account , let’s take an existing or create a new one
  • Enable application insights , for stats and other types of application tracking

Now hit the button CREATE

This takes a while, like a few minutes. Have some coffee or other hot beverage at this point:

Verify your app

Check that it has a public URL and it is possible to navigate to its default page

Ok great we have a default page, now what? Well, it’s time to add a function to our Function app.

Creating a Function, for your Function app

There are two UI behaviors here depending on whether you have no functions added to your Function app, so you are starting out fresh or the second option being that you have an existing Function app with at least one function in it.

When you create a new function there are some decisions you need to make before you can start coding like:

  1. Trigger type , this is about deciding what should trigger the invocation of your function like an HTTP call or maybe a change to a database row or something else
  2. Authoring , there are three ways to author your function, in VS Code, Any editor + Core Tools or In-portal

Starting fresh — no functions added (yet)

When adding your first function, you are presented with the Quickstart screen.

At this point, the following is shown in the middle of the page

Let’s create that function by hitting New function

Select authoring type

Now you are faced with the choice of how to author your Function:

  • VS Code , this is a great choice, plenty of plugins supporting this option
  • Any editor + Core tools, a more agnostic choice, but definitely a good choice as well
  • In-portal , you will write code in the Portal

For now, we will go with the In-portal option

Select trigger type

We are now faced with:

  • Webhook + API , function will run as soon as a certain URL is hit
  • Timer , function will run according to a schedule
  • More templates , there a ton more templates worth exploring

For now, we will go with the Webhook + API option.

It’s a long list of templates to choose from. Don’t you feel excited that there is so much more? 😃

Voila, your function is scaffolded

This is now our coding environment:

Pre-existing functions

In this scenario, we already have at least one function in our Function app. You want to look for a text saying Function + on in the left menu, it should look like this:

Select trigger type

Clicking the + sign will present you with the following screen in the main field:

This screen allows you to choose a trigger type (HTTP, Timer, or Data) and programming language (C#, JavaScript, F# or Java). Then, based on your selections, Azure will generate the function code and configuration for you with some sample code provided to display out the input data received in the log

We choose HTTP Trigger, so the first option.

We are then faced with naming our function and doing an initial selection on authorization level (we can change that part later on)

Taking it for a spin

We can test it in the portal or hit the URL. To get a URL we click get function URL and test it in a browser:

After you’ve chosen to copy the URL you head to the browser input the URL and make sure to add &name=chris. Cause if you look at your Javascript code you see that it expects the parameter name in either the body or as a URL parameter. It should look like this:

Build our service

Ok then shall we build something more fun than a hello world? Ok, it's not going to be a ton more fun but it's at least something you can cringe over like a colleague of mine did look Adam you've made it into the blog post haha.

Ok, we are going to build a… wait for it, drumrolls, a dad jokes service. You know those really bad puns told by a parent that compels you to deny any relations to them, yes those jokes. Google is my friend, so let’s start Googling for some jokes and store them in a list:

var dadJokes = [ 
 "Did you hear about the restaurant on the moon? Great food, no atmosphere.", 
 "What do you call a fake noodle? An Impasta.", 
 "How many apples grow on a tree? All of them.", 
 "Want to hear a joke about paper? Nevermind it's tearable.", 
 "I just watched a program about beavers. It was the best dam program I've ever seen.", 
 "Why did the coffee file a police report? It got mugged.", 
 "How does a penguin build it's house? Igloos it together." 
];
1
2
3
4
5
6
7
8
9

There, if I can’t unsee it, neither can you 😉

Ok, next step is to find some pictures of dogs looking like they are laughing because of the Internet right 😉

var punDogs = [ 
 "image1.jpg", 
 "image2.jpg" 
]
1
2
3
4

What, I didn’t give you the actual image URL, you’re a big developer, I’m sure you can find pictures of dogs on the Internet, or why not cats 😉

Now to select a random dad joke and random image:

var newNumber = Math.floor(Math.random() * dadJokes.length); 
var dogImageUrl = Math.floor(Math.random() * punDogs.length);
1
2

Lastly, let’s answer with an HTML response:

context.res = { 
 status: 200, 
 headers: { 
 "Content-Type": "text/html" 
 }, 
 body: '<h3>'+dadJokes[newNumber]+'</h3>' + '<br><img src="'+ punDogs[dogImageUrl] +'"/>' };
1
2
3
4
5
6

Let’s save our code and make sure to set the authorization level to Anonymous.

Above we are describing how to click the menu option Integrate in our left menu followed by selecting Anyonymous in our Authorization level dropdown.

Because why would we want to restrict the usage of this profound function to the outside world? 😉

Taking our app for a spin it now looks like this:

That’s it for this time. We’ve taken you through building your very first app, it wasn’t that hard, was it? 😃

Summary

There is so much more to say on the Serverless topics. So many different ways we can trigger this function other than HTTP and integrations that are already there and waiting for you to use them. But we have to save something for future parts of this series. Hopefully, you’ve gotten a good idea of what Serverless is and when to use it and how to create your first one of many Azure functions.

Most of all think about the old ways of doing things like handling infrastructure yourself, and thinking about for example load balancing is a thing you barely need to consider yourself with.

See you in the next part hopefully 😃