Getting started with Azure Functions using Python

Azure Function logo

Azure Functions is an event driven serverless compute solution that lets you to execute your code without provisioning or managing the infrastructure to support it. “Functions” are blocks of code that can be executed when you need to respond to specific events. As the number of requests increase, Azure Functions automatically allocates as many resources and instances of your function(s) as necessary to respond to the increased demand. Inversely, as the number of requests decrease the resources and application instances are automatically reduced. This means you only pay for the compute resources you need when you need them

Here are few scenarios where you may want to consider using Azure Functions:
Timer or schedule based triggers
Real-time streaming or event processing
Processing files during a data upload
Processing IOT data streams

Let’s get started creating an Azure Function with Python!

Prerequisites

Create the Azure Function Project

  1. Open Visual Studio code.
  2. Click the Azure icon in the left hand navigation bar.


  3. Click the Create New Project icon.


  4. Create a new folder.


  5. Select Python from the the dropdown.


  6. Select the appropriate Python environment from the dropdown.


  7. Select HTTP trigger from the dropdown.


  8. Name the trigger or leave the default name.


  9. Select Function from the dropdown.


  10. Select Open in current window from the dropdown.


  11. Visual Studio code will scaffold your Azure Functions project (this may take a minute).
  12. In the Visual Studio code explorer, browse to and open the _init_.py file.


  13. The following code makes up your Azure Function.

    Let’s take a minute and look at what the code does:

    Line 8: The Main function has a req parameter which provides the details of the HTTP request, including the request body.

    Line 9: The name variable is defined and assigned a value based on the “name” query parameter from the HTTP request.

    Line 10 – 16: These lines check if a value has not been assigned to the name variable and respond accordingly.

    Line 18-24: These lines check if a value has been assigned to the name variable and update the browser accordingly.
  14. To run your function locally, press F5 or click the Visual Studio code Run menu and select Start Debugging.
  15. Open a browser and browse to the URL provided in the Terminal window. (Alternatively you can cmd or ctrl + click the URL to open in a browser).



  16. To verify that your function is working correctly, you can append a question mark, the name parameter and your name to the URL as shown below:
    http://localhost:7071/api/HttpTrigger1?name=Dave

    After updating the URL, press the return key and your function will update the browser based on your input.

Deploy your function to Azure

  1. Click the Azure icon in the left hand navigation bar in Visual Studio code.
  2. Click the Deploy to Function App icon.


  3. Click + Create new Function App in Azure


  4. Enter a globally unique name for your function app.


  5. Select your Runtime Stack (I’m using 3.8, but your Python version may be different).


  6. Select a location for your function app resources.


  7. The Azure Functions extension will create and deploy the necessary resources in Azure to support your function app. Once the deployment is complete, you will receive a notification in Visual Studio code.


  8. Login to the Azure Portal.
  9. Browse to your list of resource groups and find your newly deployed Azure Function resources.
  10. Click on the resource group and then the function app within the resource group.


  11. You will be redirected to the Overview page for your function app. Here you can access all the resources relating to your function app.


  12. Click Functions in the left hand navigation pane to view the functions you have deployed.


  13. Click the function you deployed in the previous section.


  14. Click Code + Test in the left hand navigation pane.


  15. This screen allows you view your function app code as well as test the function. To test the function:
    Click Get Function URL


    Copy the URL


    Paste it into a browser


  16. To verify your function is working correctly in Azure, you can append a ampersand, the name parameter and your name to the URL as shown below:
    https://pythonazurefuncsample.azurewebsites.net/api/HttpTrigger1?code=b20kb9sWVa5IuqGw23Yf7yFbFEOMibEh65uiZRdYiANuezD6xXeDdw==&name=Dave



  17. It’s a good idea to clean up any resources related to your function app to avoid any unnecessary charges. You can do this by deleting the resource group created for this application either in the portal or by running the following command in Azure CLI.

    az group delete -n <your-resource-group-name>

    If you’re interested in learning more about using the Azure Functions, check out the Microsoft Documentation as well as Microsoft Learn for additional details and tutorials.

Summary

In this blog post we created an Azure Function using Python on our local machine, tested it and then deployed it to Azure.

Thank you for reading!