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
- An Azure account with an active subscription.
- Azure CLI installed on your local machine. If not installed, follow these instructions.
- Node.js and npm installed (if your function is Node.js based).
- A basic Azure Function project created locally.
Steps
-
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.
-
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 andeastus
with your preferred Azure region. -
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. -
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
). -
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.
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:
- Visiting the Function App URL in your browser.
- Using
az functionapp logs tail --name <YOUR_UNIQUE_FUNCTION_APP_NAME> --resource-group MyResourceGroup
to view live logs. - Testing specific function endpoints.
Next Steps
Consider exploring these topics:
- Configuring Application Settings and Environment Variables.
- Setting up Continuous Deployment from a Git repository.
- Monitoring and diagnostics for your Function App.
- Securing your Azure Functions.