A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://learn.microsoft.com/en-us/azure/azure-functions/create-first-function-cli-python below:

Create a Python function from the command line - Azure Functions

In this article, you use command-line tools to create a Python function that responds to HTTP requests. After testing the code locally, you deploy it to the serverless environment of Azure Functions.

This article uses the Python v2 programming model for Azure Functions, which provides a decorator-based approach for creating functions. To learn more about the Python v2 programming model, see the Developer Reference Guide

Completing this quickstart incurs a small cost of a few USD cents or less in your Azure account.

There's also a Visual Studio Code-based version of this article.

Configure your local environment

Before you begin, you must have the following requirements in place:

The recommended way to install Core Tools depends on the operating system of your local development computer.

The following steps use a Windows installer (MSI) to install Core Tools v4.x. For more information about other package-based installers, see the Core Tools readme.

Download and run the Core Tools installer, based on your version of Windows:

If you previously used Windows installer (MSI) to install Core Tools on Windows, you should uninstall the old version from Add Remove Programs before installing the latest version.

The following steps use Homebrew to install the Core Tools on macOS.

  1. Install Homebrew, if it's not already installed.

  2. Install the Core Tools package:

    brew tap azure/functions
    brew install azure-functions-core-tools@4
    # if upgrading on a machine that has 2.x or 3.x installed:
    brew link --overwrite azure-functions-core-tools@4
    

The following steps use APT to install Core Tools on your Ubuntu/Debian Linux distribution. For other Linux distributions, see the Core Tools readme.

  1. Install the Microsoft package repository GPG key, to validate package integrity:

    curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
    sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
    
  2. Set up the APT source list before doing an APT update.

    Ubuntu
    sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-$(lsb_release -cs 2>/dev/null)-prod $(lsb_release -cs 2>/dev/null) main" > /etc/apt/sources.list.d/dotnetdev.list'
    
    Debian
    sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/debian/$(lsb_release -rs 2>/dev/null | cut -d'.' -f 1)/prod $(lsb_release -cs 2>/dev/null) main" > /etc/apt/sources.list.d/dotnetdev.list'
    
  3. Check the /etc/apt/sources.list.d/dotnetdev.list file for one of the appropriate Linux version strings in the following table:

    Linux distribution Version Debian 12 bookworm Debian 11 bullseye Debian 10 buster Debian 9 stretch Ubuntu 24.04 noble Ubuntu 22.04 jammy Ubuntu 20.04 focal Ubuntu 19.04 disco Ubuntu 18.10 cosmic Ubuntu 18.04 bionic Ubuntu 17.04 zesty Ubuntu 16.04/Linux Mint 18 xenial
  4. Start the APT source update:

    sudo apt-get update
    
  5. Install the Core Tools package:

    sudo apt-get install azure-functions-core-tools-4
    

Use the func --version command to make sure your version of Core Tools is at least 4.0.5530.

Create and activate a virtual environment

In a suitable folder, run the following commands to create and activate a virtual environment named .venv. Make sure that you're using a version of Python supported by Azure Functions.

python -m venv .venv
source .venv/bin/activate

If Python didn't install the venv package on your Linux distribution, run the following command:

sudo apt-get install python3-venv
py -m venv .venv
.venv\scripts\activate
py -m venv .venv
.venv\scripts\activate

You run all subsequent commands in this activated virtual environment.

Create a local function

In Azure Functions, a function project is a container for one or more individual functions that each responds to a specific trigger. All functions in a project share the same local and hosting configurations.

In this section, you create a function project and add an HTTP triggered function.

  1. Run the func init command as follows to create a Python v2 functions project in the virtual environment.

    func init --python
    

    The environment now contains various files for the project, including configuration files named local.settings.json and host.json. Because local.settings.json can contain secrets downloaded from Azure, the file is excluded from source control by default in the .gitignore file.

  2. Add a function to your project by using the following command, where the --name argument is the unique name of your function (HttpExample) and the --template argument specifies the function's trigger (HTTP).

    func new --name HttpExample --template "HTTP trigger" --authlevel "anonymous"
    

    If prompted, choose the ANONYMOUS option. func new adds an HTTP trigger endpoint named HttpExample to the function_app.py file, which is accessible without authentication.

Run the function locally
  1. Run your function by starting the local Azure Functions runtime host from the LocalFunctionProj folder.

    func start
    

    Toward the end of the output, the following lines must appear:

    Note

    If HttpExample doesn't appear as shown above, you likely started the host from outside the root folder of the project. In that case, use Ctrl+C to stop the host, go to the project's root folder, and run the previous command again.

  2. Copy the URL of your HTTP function from this output to a browser and append the query string ?name=<YOUR_NAME>, making the full URL like http://localhost:7071/api/HttpExample?name=Functions. The browser should display a response message that echoes back your query string value. The terminal in which you started your project also shows log output as you make requests.

  3. When you're done, press Ctrl + C and type y to stop the functions host.

Create supporting Azure resources for your function

Before you can deploy your function code to Azure, you need to create three resources:

Use the following commands to create these items. Both Azure CLI and PowerShell are supported.

  1. If needed, sign in to Azure.

  2. Create a resource group named AzureFunctionsQuickstart-rg in your chosen region.

    az group create --name AzureFunctionsQuickstart-rg --location <REGION>
    

    The az group create command creates a resource group. In the above command, replace <REGION> with a region near you, using an available region code returned from the az account list-locations command.

    New-AzResourceGroup -Name AzureFunctionsQuickstart-rg -Location '<REGION>'
    

    The New-AzResourceGroup command creates a resource group. You generally create your resource group and resources in a region near you, using an available region returned from the Get-AzLocation cmdlet.

    Note

    You can't host Linux and Windows apps in the same resource group. If you have an existing resource group named AzureFunctionsQuickstart-rg with a Windows function app or web app, you must use a different resource group.

  3. Create a general-purpose storage account in your resource group and region.

    az storage account create --name <STORAGE_NAME> --location <REGION> --resource-group AzureFunctionsQuickstart-rg --sku Standard_LRS
    

    The az storage account create command creates the storage account.

    New-AzStorageAccount -ResourceGroupName AzureFunctionsQuickstart-rg -Name <STORAGE_NAME> -SkuName Standard_LRS -Location <REGION>
    

    The New-AzStorageAccount cmdlet creates the storage account.

    In the previous example, replace <STORAGE_NAME> with a name that's appropriate to you and unique in Azure Storage. Names must contain 3 to 24 characters numbers and lowercase letters only. Standard_LRS specifies a general-purpose account supported by Functions.

    The storage account incurs only a few cents (USD) for this quickstart.

  4. Create the function app in Azure.

    az functionapp create --resource-group AzureFunctionsQuickstart-rg --consumption-plan-location westeurope --runtime python --runtime-version <PYTHON_VERSION> --functions-version 4 --name <APP_NAME> --os-type linux --storage-account <STORAGE_NAME>
    

    The az functionapp create command creates the function app in Azure. You must supply --os-type linux because Python functions only run on Linux.

    New-AzFunctionApp -Name <APP_NAME> -ResourceGroupName AzureFunctionsQuickstart-rg -StorageAccountName <STORAGE_NAME> -FunctionsVersion 4 -RuntimeVersion <PYTHON_VERSION> -Runtime python -Location '<REGION>'
    

    The New-AzFunctionApp cmdlet creates the function app in Azure.

    In the previous example, replace <APP_NAME> with a globally unique name appropriate to you. The <APP_NAME> is also the default subdomain for the function app. Make sure that the value you set for <PYTHON_VERSION> is a version supported by Functions and is the same version you used during local development.

    This command creates a function app running in your specified language runtime under the Azure Functions Consumption Plan, which is free for the amount of usage you incur here. The command also creates an associated Azure Application Insights instance in the same resource group, with which you can monitor your function app and view logs. For more information, see Monitor Azure Functions. The instance incurs no costs until you activate it.

Deploy the function project to Azure

After you've successfully created your function app in Azure, you're now ready to deploy your local functions project by using the func azure functionapp publish command.

In your root project folder, run this func azure functionapp publish command:

func azure functionapp publish <APP_NAME>

In this example, replace <APP_NAME> with the name of your app. A successful deployment shows results similar to the following output (truncated for simplicity):

...

Getting site publishing info...
Creating archive for current directory...
Performing remote build for functions project.

...

Deployment successful.
Remote build succeeded!
Syncing triggers...
Functions in msdocs-azurefunctions-qs:
    HttpExample - [httpTrigger]
        Invoke url: https://msdocs-azurefunctions-qs.azurewebsites.net/api/httpexample
Invoke the function on Azure

Because your function uses an HTTP trigger, you invoke it by making an HTTP request to its URL in the browser or with a tool like curl.

Copy the complete Invoke URL shown in the output of the publish command into a browser address bar, appending the query parameter ?name=Functions. The browser should display similar output as when you ran the function locally.

Run curl with the Invoke URL shown in the output of the publish command, appending the parameter ?name=Functions. The output of the command should be the text, "Hello Functions."

Clean up resources

If you continue to the next step and add an Azure Storage queue output binding, keep all your resources in place as you'll build on what you've already done.

Otherwise, use the following command to delete the resource group and all its contained resources to avoid incurring further costs.

az group delete --name AzureFunctionsQuickstart-rg
Remove-AzResourceGroup -Name AzureFunctionsQuickstart-rg
Next steps

Connect to Azure Cosmos DB

Connect to an Azure Storage queue

Having issues with this article?


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4