Edit

Share via


Use Azure Functions in Azure Container Apps

This article shows you how to create an Azure Functions app in Azure Container Apps, complete with preconfigured autoscaling rules.

Prerequisites

Resource Description
Azure account An Azure account with an active subscription.

If you don't have one, you can create one for free.
Azure Storage account A blob storage account to store state for your Azure Functions.
Azure Application Insights An instance of Azure Application Insights to collect data about your container app.

Create a Functions app

The following steps show you how to use a sample container image to create your container app. If you want to use this procedure with a custom container image, see Create your first Azure Function on Azure Container Apps and Functions in containers.

  1. Go to the Azure portal and search for Container Apps in the search bar.

  2. Select Container Apps.

  3. Select Create.

  4. Select Container App

  5. In the Basics section, enter the following values.

    Under Project details:

    Property Value
    Subscription Select your Azure subscription.
    Resource group Select Create new resource group, name it my-aca-functions-group, and select OK.
    Container app name Enter my-aca-functions-app.
  6. Next to Optimize for Azure Functions check the checkbox.

  7. Under Container Apps environment enter:

    Property Value
    Region Select a region closest to you.
    Container Apps environment Select Create new environment.
  8. In the environment setup window, enter:

    Property Value
    Environment name Enter my-aca-functions-environment
    Zone redundancy Select Disabled.
  9. Select Create to save your values.

  10. Select Next: Container to switch to the Container section.

  11. Next to Use quickstart image, leave this box unchecked.

  12. Under the Container details section, enter the following values.

    Property Value
    Name This box is prefilled with your selection in the last section.
    Image source Select Docker Hub or other registries
    Subscription Select your subscription.
    Image type Select Public.
    Registry login server Enter mcr.microsoft.com
    Image and tag Enter azure-functions/dotnet8-quickstart-demo:1.0
  13. Under Environment variables enter values for the following variables:

    • AzureWebJobsStorage
    • APPINSIGHTS_INSTRUMENTATIONKEY or APPLICATIONINSIGHTS_CONNECTION_STRING

    Enter either Managed identity or connection string values for these variables. Managed Identity is recommended.

    The AzureWebJobsStorage variable is a required Azure Storage account connection string for Azure Functions. This storage account stores function execution logs, manage triggers and bindings, and maintains state for durable functions.

    Application Insights is a monitoring and diagnostic service that provides insights into the performance and usage of your Azure Functions. This monitoring helps you track request rates, response times, failure rates, and other metrics.

    Tip

    By default, a containerized function app monitors port 80 for incoming requests. If your app uses a different port, use the WEBSITES_PORT application setting to change the default port.

  14. Select Next > Ingress to switch to the Ingress section and enter the following values.

    Property Value
    Ingress Select the Enabled checkbox to enable ingress.
    Ingress traffic Select Accepting traffic from anywhere.
    Ingress type Select HTTP.
    Target port Enter 80.
  15. Select Review + Create.

  16. Select Create.

  17. Once the deployment is complete, select Go to resource.

  18. From the Overview page, select the link next to Application URL to open the application in a new browser tab.

  19. Append /api/HttpExample to the end of the URL.

    A message stating "HTTP trigger function processed a request" is returned in the browser.

Prerequisites

Create a Functions App

To sign in to Azure from the CLI, run the following command and follow the prompts to complete the authentication process.

  1. Sign in to Azure.

    az login
    
  2. To ensure you're running the latest version of the CLI, run the upgrade command.

    az upgrade
    
  3. Install or update the Azure Container Apps extension for the CLI.

    If you receive errors about missing parameters when you run az containerapp commands in Azure CLI or cmdlets from the Az.App module in PowerShell, be sure you have the latest version of the Azure Container Apps extension installed.

    az extension add --name containerapp --upgrade
    

    Now that the current extension or module is installed, register the Microsoft.App and Microsoft.OperationalInsights namespaces.

    az provider register --namespace Microsoft.App
    
    az provider register --namespace Microsoft.OperationalInsights
    
  4. Create environment variables.

    RESOURCE_GROUP_NAME="my-aca-functions-group"
    CONTAINER_APP_NAME="my-aca-functions-app"
    ENVIRONMENT_NAME="my-aca-functions-environment"
    LOCATION="westus"
    
  5. Create a resource group.

    az group create \
      --name $RESOURCE_GROUP_NAME \
      --location $LOCATION \
      --output none
    
  6. Create the Container Apps environment.

    az containerapp env create \
        --name $ENVIRONMENT_NAME \
        --resource-group $RESOURCE_GROUP_NAME \
        --location $LOCATION \
        --output none
    
  7. Create an Azure Functions container app.

    az containerapp create \
      --resource-group $RESOURCE_GROUP_NAME \
      --name $CONTAINER_APP_NAME \
      --environment $ENVIRONMENT_NAME \
      --image mcr.microsoft.com/azure-functions/dotnet8-quickstart-demo:1.0 \
      --ingress external \
      --target-port 80 \
      --kind functionapp \
      --query properties.outputs.fqdn
    

    This command returns the URL of your Functions app. Copy this URL and paste it into a web browser.

  8. Append /api/HttpExample to the end of the URL.

    A message stating "HTTP trigger function processed a request" is returned in the browser.