Build Your Next Viral Project Serverless using Azure Function and CoPilot


A wise person once said, “Don’t do the hard work, do the smart work.” Imagine you are an aspiring blog writer. To showcase your work, you have two options. You can either set up your own blogging infrastructure by self-hosting your website and maintaining everything end-to-end, or you can consider using existing blog hosting websites such as Hashnode. It is easy to work with, you pay a small fee based on your usage, and it is usually less hassle to begin with. This is similar to how serverless compute works. Instead of self-hosting your entire backend infrastructure, you can host them on serverless computes, popularly known as functions, for small repetitive tasks.
What do I Mean by Serverless?
The most popular way of deploying your backend application is by using the offerings of cloud providers such as Azure and AWS. They provide dedicated servers in Linux and Windows runtime to host a variety of services written in popular programming languages. They give users the flexibility to choose their required storage, memory, and processing power. However, they are costly and mostly charged on a fixed per-hour rate. Serverless, on the other hand, charges users based on their actual usage, i.e., in compute seconds used. This allows scaling as per their needs without worrying about costs incurred during non-peak times.
Servers with Benefits?
Cost Savings - For small-duration, repetitive tasks that are unevenly spread across the day, opting for a serverless infrastructure can help save costs in terms of the infrastructure required to host it compared to deploying on a dedicated server.
Scalability - With flexible consumption plans, users need not worry about scaling their functions app. The servers auto-scale depending on the traffic, again only costing you for the compute seconds used.
Less Hassle - Since you don’t need to set up the entire servers on your own, it helps in getting rid of managing operations end-to-end. The deployments are blazing fast for REST application-based servers.
High Availability - Some cloud providers like Azure provide functionality like "always on," which prevents your function from sleeping, thus preventing the latency arising from cold-start.
Different cloud-service providers have their own versions of serverless offerings. Some of the popular ones are AWS’s Lambda functions, Microsoft Azure’s Functions App (with flexible consumption plan), and Google Cloud Platform's Cloud Functions. For the purpose of this tutorial, we will try building a simple image generation service over Azure’s Functions App. You may go ahead with any other service provider offering too. I went ahead with Azure because my student credits were about to expire xD.
Chaliye Shuru karte hai!
So, today I will try building an Azure Functions App which will be HTTP triggered. So every time someone clicks on the link, the function will be triggered. The function is pretty simple. It takes the user’s bank balance as input from the query parameters. Based on the balance, it gives advice to the user on whether they should resign or not. Instead of returning bland advice, we will add a pinch of humor. Our Honest Billa (cat) will spit facts that are hard to digest in a PNG format. Pretty simple, right?
Prerequisites
Here is what my current system setup looks like. If you have something different, please find alternatives to the things mentioned before. They are just a GPT query away.
System OS - Windows 11
IDE - Visual Studio Code
Prerequisites - Python v3.11 and Node.JS v23
Installing the Required Packages
First of all, we need to install some libraries which will come in handy during local development.
npm i -g azure-functions-core-tools@4 --unsafe-perm true
This is the core library required to scaffold a local Azure Functions App and also run it locally. To make things easy for us, we can install the Azure Functions VS Code extension. With this extension, creating new functions and deploying them to Azure are just a set of a few clicks.
Scaffolding the Initial Project
Once you have the extension installed, log in to your Azure account to access all the resources you have already created.
Then, go to the workspace tab at the bottom of the extension. From there, you can scaffold a new local function project. For this project, I will keep the trigger method as HTTP and auth permission as anonymous, allowing the function's link to be accessible by a larger audience.
Based on my preference, I chose Python as the project's language and HTTP trigger as the template. Additionally, I selected the Authorization level as anonymous, which will allow users to trigger my function without any added authorization.
Developing the Core Logic of Function with CoPilot
With the initial scaffolding done, it’s time to bring out our secret weapon - GitHub CoPilot. With the power of GPT-4, it will be able to build the initial draft of our application within a few prompts.
We will be editing the functions_app.py
file. It will contain the core logic of our function. Since we are going to generate some meme images and return them as a response, we need to store some static content. For that, we will create a separate folder static
for storing the images.
After a few iterations, we have an initial draft of the function ready to be tested. We will run it locally via the Azure function extension.
Executing the Function Locally
To run this function locally, go to the run and debug section in VS Code. From there, choose the Attach to Python functions
option in the dropdown and click on the small green triangle button. And voila, your function will be ready to test locally!
Deploying to Azure Function App in Few Clicks
Once you have verified the basic flow, it is time to deploy this over Azure. For that, ensure that an Azure Functions App exists in your connected Azure account. You can create it either via the Azure web dashboard or by using the VS Code functions extension. Here is a link to the official article from the Microsoft dev team explaining the process in detail.
With the app created successfully, deploying it to Azure is just a few clicks away. Click on the Deploy to Azure option and follow the on-screen instructions. It will give a warning regarding existing data deployed on the Azure function app being overwritten, but you can ignore that.
And you're done. Now go to the Azure dashboard and open your function app from the Azure resource center. From there, you will find the URL where your function app is hosted, allowing you to trigger it by making the appropriate request.
LLM not working as expected for you? No worries, here is a link to my GitHub code repository. Just clone it and deploy it to your own Azure function app. Feel free to improve it as per your need. Also, if you want to try my deployed function app, you can check it using this link. It will be up and running (unless my student credits have expired).
What Could Be Improved?
Functions apps are suitable for lightweight applications that can be executed quickly, such as CSV processing or triggering mail alerts. However, they are not meant for heavy computing tasks like real-time video generation due to their limited resources. Generally, any serverless resource should not be used for heavy computing as it defeats their purpose. In our small project, we uploaded a couple of static images and fonts with our application. Since the size is small, it won’t be a big issue, but ideally, these resources should be stored in a blob storage, independent of our function app, and accessed via a typical CDN setup. However, this project is for educational purposes, so we don’t need to think that hard.
Why Did I Do This?
You might be wondering about the point of this project. Who would make an HTTP request every time for this silly thing? It depends on how you integrate it into your workflow. For example, you can add this to your personal finance app. So every time a user considers an impulsive purchase, you can fetch their balance in real-time and show them this result. One can integrate this into their chatbot with a similar use case. The opportunities are endless, limited only by our vision and imagination. The main advantage that Azure Functions App offers over traditional REST API projects is the ease of setting up the project and deploying it with better cost control.
Today, we covered what serverless resources are and how they can power your lightweight backend applications. We also built a small serverless function, deployed it using Azure, and tested it end-to-end with the help of LLMs and useful VS Code extensions. I hope you learned something new today. I would love to know what you will build as your next serverless app. If you face any difficulty, feel free to connect with me.
Subscribe to my newsletter
Read articles from Lakshay directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Lakshay
Lakshay
My friends used to poke me for writing long messages while texting. I thought why not benefit from it? I have led communities, small startups, hackathon teams. Made web apps, NFTs, youtube vlogs.. yes, I am an engineer (almost)