Serverless Series: Quickstart with Azure Functions ⚡
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:
- An active Azure account and subscription. You can create an account for free and obtain a free Azure trial subscription.
- Node.js (reference list of supported versions)
- Visual Studio Code with Azure Functions extension.
2| Create Local Functions Project
In VSCode, create a local Functions project (we will publish it to Azure later).
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!
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.
Open the URL and it will take you to the default GET request page
If you pass in a ‘name’ as a query parameter the function should return a personalized message
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:
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.
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.
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.
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!
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.
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.
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! 🙂