Back to Blog

Create Function Apps from CLI: plans, storage, and triggers

Complete tutorial about az functionapp create in Azure CLI. Learn Consumption vs Premium, storage account, app settings, identities.

Create Function Apps from CLI: plans, storage, and triggers

Create Function Apps from CLI: Plans, Storage, and Triggers

In the modern world of cloud computing, serverless architectures have gained immense popularity due to their efficiency and cost-effectiveness. Azure Functions is a serverless compute service that lets you run event-driven code without having to manage infrastructure. The command az functionapp create is pivotal for developers aiming to deploy their Azure Functions directly from the command line interface (CLI). This command allows you to create function apps across various plans, such as Consumption and Premium, while managing essential resources like storage accounts and application settings. Understanding how to utilize this command effectively can streamline your development processes, automate deployments, and enhance your application's scalability.

Prerequisites

Before diving into the Azure CLI commands, ensure you have the following:

  • Azure CLI: Install the Azure CLI on your machine. You can download it from here.
  • Azure Subscription: A valid Azure subscription is required to create resources.
  • Permissions: Ensure that you have the necessary permissions to create function apps and associated resources in your Azure subscription.
  • Authentication: Log in to your Azure account using:
    az login
    

Fundamental Concepts

Key Terminology

  • Function App: A container for your Azure Functions, allowing you to manage and deploy code efficiently.
  • Consumption Plan: A pay-per-execution plan that automatically scales based on demand, ideal for sporadic workloads.
  • Premium Plan: A plan that provides additional features such as longer execution times, pre-warmed instances, and VNET integration, suitable for high-demand applications.
  • Storage Account: Required for function apps, this serves as a backing store for function metadata and logs.

When to Use

  • Consumption Plan: Best for infrequent tasks where you want to minimize costs and automatically scale without managing infrastructure.
  • Premium Plan: Ideal for applications requiring high performance, longer execution times, and lower latency (eliminating cold starts).

Command Syntax

The basic syntax for creating a function app using Azure CLI is:

az functionapp create --name <app_name> --resource-group <resource_group> --storage-account <storage_account> --plan <plan_name> [--runtime <runtime>] [--runtime-version <version>] [other-options]

Parameters Table

Parameter Description
--name or -n Name of the new function app (unique).
--resource-group or -g Name of the resource group where the function app will be created.
--storage-account or -s Name of the storage account to use.
--plan or -p Name or ID of the app service plan.
--runtime The runtime stack for the function (e.g., dotnet, node, python).
--runtime-version The specific version of the runtime to use.
--functions-version Version of the Functions runtime (e.g., 4).
--assign-identity Assigns a managed identity for the function app.

Practical Examples

Example 1: Create a Function App in Consumption Plan

az functionapp create --name myFunctionApp --resource-group myResourceGroup --storage-account mystorageaccount --plan myConsumptionPlan --runtime node --runtime-version 14

This command creates a function app named myFunctionApp in the myResourceGroup resource group using a specified storage account and the Consumption plan.

Example 2: Create a Function App in Premium Plan

az functionapp create --name myPremiumFunctionApp --resource-group myResourceGroup --storage-account mystorageaccount --plan myPremiumPlan --runtime python --runtime-version 3.8

This command creates a function app in the Premium plan, suitable for high-demand applications.

Example 3: Assign a Managed Identity

az functionapp create --name myFunctionApp --resource-group myResourceGroup --storage-account mystorageaccount --plan myConsumptionPlan --assign-identity

This command creates a function app with a managed identity for secure access to Azure resources.

Example 4: Use Existing Storage Account

az functionapp create --name myFunctionApp --resource-group myResourceGroup --storage-account existingStorageAccount --plan myConsumptionPlan

This command creates a function app using an existing storage account.

Example 5: Create a Function App with Application Insights

az functionapp create --name myFunctionApp --resource-group myResourceGroup --storage-account mystorageaccount --plan myPremiumPlan --assign-identity --app-insights myAppInsights

This command creates a function app with Application Insights enabled for monitoring.

Example 6: Create a Function App with Environment Variables

az functionapp create --name myFunctionApp --resource-group myResourceGroup --storage-account mystorageaccount --plan myConsumptionPlan --set MY_ENV_VAR=somevalue

This command sets environment variables during the function app creation.

Example 7: Scale Out Function Apps

az functionapp update --name myFunctionApp --resource-group myResourceGroup --set siteConfig.minimumElasticInstanceCount=2

This command configures the function app to maintain a minimum of 2 pre-warmed instances.

Example 8: Create a Function App with Custom Domain

az functionapp create --name myFunctionApp --resource-group myResourceGroup --storage-account mystorageaccount --plan myPremiumPlan --assign-identity --custom-domain mydomain.com

This command creates a function app with a custom domain.

Real-World Use Cases

Scenario 1: Data Processing Pipeline

A company needs to process incoming data files intermittently. By using a Consumption Plan, they can ensure that they only pay for processing time when data is available, making it cost-effective.

Scenario 2: Real-Time Analytics

An e-commerce platform requires real-time analytics on user behavior. By using a Premium Plan, the platform can handle continuous user interactions with low latency and long-running function executions.

Scenario 3: Scheduled Tasks

An organization needs to run scheduled background tasks such as report generation. By creating a function app in the Consumption Plan, they can trigger these functions using a timer without incurring costs when not in use.

Best Practices

  1. Choose the Right Plan: Analyze your workload patterns to select between the Consumption and Premium plans.
  2. Use Managed Identities: For secure access to Azure resources, always utilize managed identities.
  3. Monitor Performance: Integrate Application Insights to monitor the performance and health of your function apps.
  4. Optimize Cold Starts: For performance-sensitive applications, consider using the Premium plan to avoid cold starts.
  5. Set Environment Variables Wisely: Use app settings to manage configurations securely and efficiently.

Common Errors

  1. Error: "The function app name is already in use."

    • Cause: The app name must be unique across Azure.
    • Solution: Change the name to a unique value.
  2. Error: "The storage account is not in the same region."

    • Cause: The storage account must be in the same region as the function app.
    • Solution: Create a storage account in the same region or move the app.
  3. Error: "Insufficient permissions to create resources."

    • Cause: Lack of necessary permissions in the Azure subscription.
    • Solution: Ensure you have the required permissions or ask an admin for access.
  4. Error: "Invalid plan name."

    • Cause: The specified plan does not exist or is not compatible.
    • Solution: Verify the plan name and ensure it exists in the resource group.

Related Commands

Command Description
az storage account create Creates a new Azure Storage account.
az functionapp plan create Creates a new App Service Plan for Azure Functions.
az functionapp update Updates the settings of an existing function app.
az functionapp delete Deletes a specified function app.

Automation Script

Here is a sample Bash script to automate the creation of a Function App:

#!/bin/bash

# Variables
RESOURCE_GROUP="myResourceGroup"
STORAGE_ACCOUNT="mystorageaccount"
FUNCTION_APP_NAME="myFunctionApp"
PLAN_NAME="myPremiumPlan"
RUNTIME="node"
RUNTIME_VERSION="14"

# Create Resource Group
az group create --name $RESOURCE_GROUP --location eastus

# Create Storage Account
az storage account create --name $STORAGE_ACCOUNT --location eastus --resource-group $RESOURCE_GROUP --sku Standard_LRS

# Create Function App Plan
az functionapp plan create --name $PLAN_NAME --resource-group $RESOURCE_GROUP --sku EP1 --is-linux

# Create Function App
az functionapp create --name $FUNCTION_APP_NAME --resource-group $RESOURCE_GROUP --storage-account $STORAGE_ACCOUNT --plan $PLAN_NAME --runtime $RUNTIME --runtime-version $RUNTIME_VERSION

echo "Function App '$FUNCTION_APP_NAME' created successfully!"

Conclusion

The az functionapp create command is a powerful tool for developers looking to streamline the deployment of Azure Functions. By understanding the differences between Consumption and Premium plans, configuring storage accounts, and properly managing application settings, you can leverage Azure's serverless capabilities to build scalable and efficient applications.

Next, explore further by integrating other Azure services with your function apps, or automate your deployments using CI/CD pipelines.

References