Create an Azure Function with Python
    • Dark
      Light
    • PDF

    Create an Azure Function with Python

    • Dark
      Light
    • PDF

    Article Summary

    #ServerlessTips - Azure Functions
    Author: Dave McCollough Vice President - Information Technology

    In this article, we will demonstrate how to create an Azure Function using Python.

    Python is a general-purpose, high level programming language known for its readability and flexibility. Python can be used for scripting, backend web development, Data Science, AI and Machine Learning, IoT, etc.

    Python is used across several Azure Services including web apps, functions, storage, databases, AI, etc.

    Prerequisites

    1. Active Azure Subscription
    2. Visual Studio Code with Azure Functions Core Tools and Azure Functions extension installed
    3. Python version 3.6 or greater

    Create a local function project

    1. Open Visual Studio Code and click the Azure icon
      Picture1(7)

    2. Click the folder icon to create your new project
      Picture2(5)

    3. You will be prompted to select a location to save your project.

    4. Select Python from the Create New Project dropdown.

    5. Select the version of Python or Python environment you want to run your Azure Function project in.

    6. Select HTTP trigger as your project template.

    7. You will be prompted to create a function name. You can use the default (HTTPTrigger1) or change it based on your preference

    8. You will be prompted to select an Authorization level. For this example, you can choose Anonymous (choosing Anonymous means your function can be called without an API access key).

    9. You will be prompted to choose how you want to open your project, select Add to workspace (choosing Add to workspace means any configuration settings will apply only to this folder).

    10. Visual Studio will create your project

    11. Once your project is created, press the F5 Key to run your function

    12. Azure Functions Core tools will display output in the Terminal tab. This will allow you to access the URL of your function app.
      Picture3(3)

    13. You can either copy/paste the URL into your browser or cmd or ctrl + click to open the URL in a browser

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

    15. After modifying the URL, the function will respond and update the browser
      Picture4(3)

    Publish your function to Azure

    1. Click the Azure icon followed by the deploy to Azure icon
      Picture5(2)

    2. If you are not already signed into Azure, you will be prompted to sign in.

    3. Click Create new Function App in Azure.
      Picture6(1)

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

    5. Select your runtime stack. This should be the version of Python you are running locally.

    6. Select the region for your resources.

    7. The resources to support your function app will be created in Azure.

    8. Login to the Azure Portal.

    9. Navigate to the resource groups page and select the resource you created in step 4.

    10. Select the function app from the list of resources.
      Picture7

    11. Click Functions in the left-hand navigation bar

    12. Click on your function

    13. Click on Code + Test in the left-hand navigation bar

    14. You can now view your function code. If you want to test your function, click Get function URL.
      Picture8

    15. Copy and paste the URL into your browser.
      Append the question mark and name parameter to the URL.
      ?name=Dave

    16. After modifying the URL, the function will respond and update the browser.

    Summary

    In this article we reviewed how you can create and publish a Python function project from your local environment to Azure.

    Azure-functions.png


    Was this article helpful?