Serverless Series: Quickstart with Azure Functions ⚡

Kyler Mintah
5 min readJul 31, 2021

--

This is a quick tutorial to get you started with Azure Functions! This is a Cloud Function solution that empowers developers with effective serverless capabilities.

Objective

We will build an Azure Function to return a random quote from a list of provided quotes via an HTTP request. This toy example should be good to get started using Azure functions and add another tool to your arsenal when evaluating options for your backend services.

Language Choice

There are a number of languages to choose from when creating Azure Functions including Go, Rust, C#, Python, Java, JavaScript, TypeScript, and PowerShell at the time of writing.

For this tutorial, I will be using JavaScript/Node.js.

Tools

For this tutorial, I will be using Visual Studio Code but note that there are a number of IDE/tooling choices available to work with Azure Functions!

1| Getting Started

To get started you will need to have:

2| Create Local Functions Project

In VSCode, create a local Functions project (we will publish it to Azure later).

If you are already signed in to your Azure account, you should be able to see your subscriptions. If not, sign in.

When prompted, select JavaScript as language choice

Next, select HTTP trigger for project template

Next, input a relevant function name. I chose QuoteSampleFunction.

Next, for the Authorization level, select Anonymous so that anyone can call the function endpoint.

Lastly, select Add to workspace

You should now have an Azure Functions project with generated files to get started with!

Generated file structure for Azure Function

3| Run default Azure Function Locally

Let’s try out our function so far to make sure it’s working.

Hit theF5 key to run the QuoteSampleFunction. If successful you should see the localhost URL.

Azure Function running on localhost address

Open the URL and it will take you to the default GET request page

Default template HTTP trigger Azure Function response

If you pass in a ‘name’ as a query parameter the function should return a personalized message

Default template HTTP trigger Azure Function response with query parameter

4| Add Our Custom Logic

As highlighted, our objective here is to return a random quote from a list hosted on our Azure Function.

Open QuoteSampleFunction/index.js and replace the content with the following code:

QuoteSampleFunction/index.js

Lines 5 through 27 contain our list. Lines 29 contains our logic to randomly generate a quote, and lastly, line 31 through 35 is how we are sending our response object.

In VSCode, hit theF5 key to run the QuoteSampleFunction with our new logic. Open the localhost URL printed in the console and you should be greeted by one of four quotes.

Hit refresh on your browser to see a randomly sampled quote from our list. (note yours should display JSON instead of a raw message).

5| Hosting Our Function in Azure

We would like for our Azure Function to be publically accessible, so let’s follow some simple steps to host it on Azure.

Firstly, make sure you are signed into the Azure extension. If you do not have an account now would be a good time to create one!

Then find the Deploy to Function App… button and select it.

Select Deploy to Function App

You will be prompted to select a subscription, so choose the one you would like to use. Then select Create New Function App when prompted.

Then, enter a globally unique name for our hosted function. I called mine quoteSampleFunction.

Choose a relevant runtime stack. I chose Node.js 14 LTS.

Lastly, select a relevant region where you would like to host your Azure function. I chose East US.

Deployment to Azure is underway!

Your Azure Function will then begin deployment. This may take a moment or two.

Once the deployment is complete, you will be able to make requests to your Azure function from anywhere via the hosted URL!

Now able to make global requests to Azure Function

5| So What’s the Big Deal?

Well… we just quickly created a service that we can leverage in our backend without the traditional overhead associated with managing servers, hosting, and scaling. In a couple of minutes, we now have a service we can use anywhere.

Android App powered by our Azure Function

I scratched together a simple Android app that uses our Azure Function in its backend to showcase this. The app can easily leverage our Azure Function in its service configuration.

Getting into Gradient Design these days

This example barely scratches the surface. There are tons of opportunities to leverage Azure Functions to your benefit more.

Check out this page for some real-world Azure Functions examples.

Happy Building! 🙂

--

--