Skip to content

Latest commit

 

History

History
203 lines (137 loc) · 12.3 KB

File metadata and controls

203 lines (137 loc) · 12.3 KB
title Create your first containerized Azure Functions
description Get started by deploying your first function app from a Linux image in a container registry to Azure Functions.
ms.date 12/29/2023
ms.topic quickstart
ms.custom build-2023, devx-track-azurecli, devx-track-azurepowershell, devx-track-extended-java, devx-track-js, devx-track-python, linux-related-content, devx-track-ts
zone_pivot_groups programming-languages-set-functions

Create your first containerized Azure Functions

In this article, you create a function app running in a Linux container and deploy it to Azure Functions.

Deploying your function code to Azure Functions in a container requires Premium plan or Dedicated (App Service) plan hosting. Completing this article incurs costs of a few US dollars in your Azure account, which you can minimize by cleaning-up resources when you're done.

Tip

When you need to run your event-driven functions in Azure in the same environment as other microservices, APIs, websites, workflows, or any container hosted programs, consider instead hosting your containerized function apps in Azure Container Apps. Functions provides integrated support for developing, deploying, and managing containerized function apps on Container Apps. For more information, see Azure Container Apps hosting of Azure Functions.

[!INCLUDE functions-create-container-registry]

Create supporting Azure resources for your function

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

  • A resource group, which is a logical container for related resources.
  • A Storage account, which is used to maintain state and other information about your functions.
  • A function app, which provides the environment for executing your function code. A function app maps to your local function project and lets you group functions as a logical unit for easier management, deployment, and sharing of resources.

Important

This article currently shows how to connect to both the Azure Storage account and your container registry by using connection strings and other shared secret credentials. For the best security, you should instead use only a managed identity-based connection to both your storage account and to Azure Container Registry using Microsoft Entra authentication. For more information, see the Functions developer guide.

Use the following commands to create these items. Both Azure CLI and PowerShell are supported. To create your Azure resources using Azure PowerShell, you also need the Az PowerShell module, version 5.9.0 or later.

  1. If you haven't done already, sign in to Azure.

    az login
    

    The az login command signs you into your Azure account.

    Connect-AzAccount
    

    The Connect-AzAccount cmdlet signs you into your Azure account.


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

    az group create --name AzureFunctionsContainers-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 AzureFunctionsContainers-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.


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

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

    The az storage account create command creates the storage account.

    New-AzStorageAccount -ResourceGroupName AzureFunctionsContainers-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 is appropriate to you and unique in Azure Storage. Storage names must contain 3 to 24 characters numbers and lowercase letters only. Standard_LRS specifies a general-purpose account supported by Functions.

  4. Use the command to create a Premium plan for Azure Functions named myPremiumPlan in the Elastic Premium 1 pricing tier (--sku EP1), in your <REGION>, and in a Linux container (--is-linux).

    az functionapp plan create --resource-group AzureFunctionsContainers-rg --name myPremiumPlan --location <REGION> --number-of-workers 1 --sku EP1 --is-linux
    
    New-AzFunctionAppPlan -ResourceGroupName AzureFunctionsContainers-rg -Name MyPremiumPlan -Location <REGION> -Sku EP1 -WorkerType Linux

    We use the Premium plan here, which can scale as needed. For more information about hosting, see Azure Functions hosting plans comparison. For more information on how to calculate costs, see the Functions pricing page.

    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.

Create and configure a function app on Azure with the image

A function app on Azure manages the execution of your functions in your Azure Functions hosting plan. In this section, you use the Azure resources from the previous section to create a function app from an image in a container registry and configure it with a connection string to Azure Storage.

  1. Create a function app using the following command, depending on your container registry:

    az functionapp create --name <APP_NAME> --storage-account <STORAGE_NAME> --resource-group AzureFunctionsContainers-rg --plan myPremiumPlan --image <LOGIN_SERVER>/azurefunctionsimage:v1.0.0 --registry-username <USERNAME> --registry-password <SECURE_PASSWORD> 
    
    az functionapp create --name <APP_NAME> --storage-account <STORAGE_NAME> --resource-group AzureFunctionsContainers-rg --plan myPremiumPlan --image <DOCKER_ID>/azurefunctionsimage:v1.0.0
    
    New-AzFunctionApp -Name <APP_NAME> -ResourceGroupName AzureFunctionsContainers-rg -PlanName myPremiumPlan -StorageAccount <STORAGE_NAME> -DockerImageName <LOGIN_SERVER>/azurefunctionsimage:v1.0.0
    
    New-AzFunctionApp -Name <APP_NAME> -ResourceGroupName AzureFunctionsContainers-rg -PlanName myPremiumPlan -StorageAccount <STORAGE_NAME> -DockerImageName <DOCKER_ID>/azurefunctionsimage:v1.0.0
    

    In this example, replace <STORAGE_NAME> with the name you used in the previous section for the storage account. Also, replace <APP_NAME> with a globally unique name appropriate to you and <DOCKER_ID> or <LOGIN_SERVER> with your Docker Hub account ID or Container Registry server, respectively. When you're deploying from a custom container registry, the image name indicates the URL of the registry.

    When you first create the function app, it pulls the initial image from your Docker Hub. You can also Enable continuous deployment to Azure from your container registry.

    [!TIP]
    You can use the DisableColor setting in the host.json file to prevent ANSI control characters from being written to the container logs.

  2. Use the following command to get the connection string for the storage account you created:

    az storage account show-connection-string --resource-group AzureFunctionsContainers-rg --name <STORAGE_NAME> --query connectionString --output tsv
    

    The connection string for the storage account is returned by using the az storage account show-connection-string command.

    $storage_name = "<STORAGE_NAME>"
    $key = (Get-AzStorageAccountKey -ResourceGroupName AzureFunctionsContainers-rg -Name $storage_name)[0].Value
    $string = "DefaultEndpointsProtocol=https;EndpointSuffix=core.windows.net;AccountName=" + $storage_name + ";AccountKey=" + $key
    Write-Output($string) 
    

    The key returned by the Get-AzStorageAccountKey cmdlet is used to construct the connection string for the storage account.


    [!IMPORTANT] This article currently shows how to connect to the default storage account by using a connection string. For the best security, you should instead create a managed identity-based connection to Azure Storage using Microsoft Entra authentication. For more information, see the Functions developer guide.

    Replace <STORAGE_NAME> with the name of the storage account you created earlier.

  3. Use the following command to add the setting to the function app:

    az functionapp config appsettings set --name <APP_NAME> --resource-group AzureFunctionsContainers-rg --settings AzureWebJobsStorage=<CONNECTION_STRING>
    

    The az functionapp config appsettings set command creates the setting.

    Update-AzFunctionAppSetting -Name <APP_NAME> -ResourceGroupName AzureFunctionsContainers-rg -AppSetting @{"AzureWebJobsStorage"="<CONNECTION_STRING>"}
    

    The Update-AzFunctionAppSetting cmdlet creates the setting.


    In this command, replace <APP_NAME> with the name of your function app and <CONNECTION_STRING> with the connection string from the previous step. The connection should be a long encoded string that begins with DefaultEndpointProtocol=.

  4. The function can now use this connection string to access the storage account.

[!INCLUDE functions-container-verify-azure]

Clean up resources

If you want to continue working with Azure Function using the resources you created in this article, you can leave all those resources in place. Because you created a Premium Plan for Azure Functions, you'll incur one or two USD per day in ongoing costs.

To avoid ongoing costs, delete the AzureFunctionsContainers-rg resource group to clean up all the resources in that group:

az group delete --name AzureFunctionsContainers-rg

Next steps

[!div class="nextstepaction"] Working with custom containers and Azure Functions