Deploy Azure Functions using Azure CLI

This tutorial will guide you through the process of deploying an Azure Function App to Azure using the Azure Command-Line Interface (CLI). The Azure CLI is a powerful tool for managing Azure resources from your terminal.

Prerequisites

Steps

  1. Log in to Azure

    Open your terminal or command prompt and log in to your Azure account:

    az login

    This command will open a browser window for you to authenticate.

  2. Create a Resource Group

    A resource group is a logical container for your Azure resources. Create a new one:

    az group create --name MyResourceGroup --location eastus

    Replace MyResourceGroup with your desired name and eastus with your preferred Azure region.

  3. Create an Azure Storage Account

    Azure Functions requires a storage account for operations like managing triggers and logging function executions.

    az storage account create --name mystorageaccount <YOUR_UNIQUE_STORAGE_ACCOUNT_NAME> --resource-group MyResourceGroup --location eastus --sku Standard_LRS

    Remember to replace mystorageaccount<YOUR_UNIQUE_STORAGE_ACCOUNT_NAME> with a globally unique name for your storage account.

  4. Create a Function App

    Now, create the Function App itself. This is where your functions will run.

    az functionapp create --resource-group MyResourceGroup --consumption-plan-location eastus --runtime node --runtime-version 18 --functions-version 4 --name MyFunctionApp <YOUR_UNIQUE_FUNCTION_APP_NAME> --storage-account mystorageaccount<YOUR_UNIQUE_STORAGE_ACCOUNT_NAME>

    Replace placeholders with your resource group name, desired Function App name (globally unique), and storage account name. Adjust the --runtime and --runtime-version based on your function's language and version (e.g., python, dotnet).

  5. Deploy Your Function Code

    Navigate to your function project's root directory in your terminal and deploy your code:

    func azure functionapp publish MyFunctionApp <YOUR_UNIQUE_FUNCTION_APP_NAME>

    This command uses the Azure Functions Core Tools to zip and upload your project to the Function App.

Important: Ensure your function project is correctly configured for deployment. The func azure functionapp publish command typically handles packaging, but verify your host.json and any language-specific configuration files are in place.

Verification

Once the deployment is complete, you can verify your Function App is running by:

Next Steps

Consider exploring these topics:

Security Alert: Avoid hardcoding sensitive information like connection strings or API keys directly in your code. Use Application Settings within your Azure Function App for secure management.