Serverless development with Azure Functions enables you to run small pieces of code in the cloud without the hassle of managing physical servers or containers. This way, you can focus only on code by eliminating all the infrastructure considerations. You can choose your own favorite programming language for developing applications using Azure Functions, thus gaining the freedom to work efficiently.

What Is Serverless?

Serverless architecture enables you to run code without the need to worry about managing infrastructure. Third party services handle your servers in serverless infrastructure. In addition, you can save time spent on operations and focus only on software development.

The payment model of serverless development enables you to pay only when the code runs. As a result, you can reduce development costs. In addition, serverless applications enable you to scale automatically based on your computing needs. For instance, you can scale automatically during rush hours and stop the extra resources when you don’t need them.

What Is Azure Functions Service?

Microsoft Azure Functions is a serverless computing platform designed to simplify and accelerate application development. This platform provides an environment to execute and host Azure applications. In addition, Azure Functions integrates with other Azure tools like IoT Hub, Cosmos DB and more.

You can use different trigger functions for flexible application development. Trigger functions are triggered with message queues, HTTP requests, and timers. Azure Functions provide high-level security and reliability for your apps. This platform can scale easily when your app is experiencing high demand hours.

Developing Serverless Apps With Azure Functions: Step-by-Step Tutorial

This tutorial walks you through the process of developing an Azure Functions serverless app with Python. The code is triggered with HTTP requests.

1. Requirements Before getting started, you need to configure some prerequisites:

2. Create a new project

  • Use the extension for Azure Functions to create, manage and deploy you project.
  • Click on the icon of Azure in the VS Code activity bar. You should see a list of all your Azure account available subscriptions.
  • Select “Create New Project” in the Functions explorer bar.
  • Select Python as the programming language.
  • In the “Select a template for your project’s first function” window, click on HTTP trigger in the list
  • In the Authorization level window, select “Function”.
  • Indicate the destination of the Python executable file.

Your Azure Function is now ready. Visual Studio creates a lot of files in the project structure. You should see the files on your left-hand side.

3. The Python Code One of the project structure files is init.py. This file contains 24 lines of Python code that makes up you Azure Function.

import logging
import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello {name}!")
    else:
        return func.HttpResponse(
             "Pass a name on the query string or in the request body",
             status_code=400
        )

The purpose of the code

  • The req function contains all the HTTP request. This includes includes request body and parameters.
  • The req function also validates that the request contains the name parameter and respond respectively.
  • The Azure Function service takes care of the operation of the HTTP server.
  • The function.json file contains all the Azure Functions configurations.

4. Test your serverless app locally

You can execute your Function on your local environment using the Azure Function Core Tools. Use the VS Code or the command line interface to start the Function app you can. Next steps walks you through the process of running your app with VS code.

  • Go to the Debug dropdown and choose “Start Without Debugging”.
  • Visual Code will execute the Function server in the Integrated Terminal.
  • Use the curl command and the URL of your HTTP Functions to send a request to your local server.
curl http://localhost:7072/api/AzureFunctionTrigger?Name=YourName
  • If you get a proper response like Hello YourName, this means that your serverless app is working.

5. Deploy To Azure

The extension for Azure Functions enables you to deploy to Azure. The deployment steps are listed below.

  • Click the icon of Azure on the left side of VS Code window.
  • Click the icon of Deploy to Function App in the explorer bar.
  • Choose Create new Function App in Azure.
  • Enter a new Function app name.
  • Choose the global location of your Function app host.
  • After choosing the location, VS code creates a resource group, a storage account, and a new Application Insights resource.

The serverless app is now working on Azure. Use the Azure Portal to view your code and resources.

6. Test your app on Azure

You can find a list of all associated resources in the Azure Portal. Here are the needed steps for testing your deployed app on Azure:

  • Click on your Function in the list.
  • You will see all the Function details, including the associated Function.
  • Select the Function name followed by the URL of the Get function.
  • This opens a window with the HTTP Function URL.
  • You can check if the Function is working by using this URL in curl, in the browser or Postman.

Conclusion

This tutorial covered the basics of serverless computing and Azure Functions, including:

  • How to create an app using Azure Function and VS Code.
  • Testing your app on your own machine.
  • How to deploy your serverless app to Azure cloud.

Hopefully, you now have the required knowledge to develop your next serverless project. Remember that technology is a continual process that requires constant learning. You can further leverage this tutorial to experiment with other trigger functions, and run a variety of experiments until you find a process that works for you.