Deploy Azure Function App with .NET 8 to Azure Container Apps via Bicep

Deploy Azure Function App with .NET 8 to Azure Container Apps via Bicep

Raymond Tang Raymond Tang 0 1243 1.90 index 9/16/2023

Code description

Azure Function App can now be deployed into Azure Container Apps managed environment.  This code snippet shows you how to deploy an Azure Function App created with .NET 8 (dotnet-isolated as runtime) into a managed environment of Azure Container Apps. The container source image is from GitHub (ghcr.io). You can also use any other container registry.

Resources created

This Bicep template creates the following resources:

  • Blob Storage account

  • Azure Log Analytics workspace

  • Azure Application Insights

  • Azure Container Apps managed environment

  • Azure Function App

Required credential

You need to provide a container registry user name and password if you are using private registry.  This template uses GitHub Packages.

Code snippet

    @description('Specifies the location for resources.')
    param location string = 'australiaeast'
    
    @description('Name of the Container App Environment')
    param functionAppEnvName string = 'my-container-app-env'
    
    param functionAppName string = 'my-functions'
    
    @secure()
    @description('User name of the container registry')
    param registryUserName string 
    
    @secure()
    @description('User password of the container registry')
    param registryPassword string
    
    var applicationInsightsName =  '${functionAppName}-appinsights'
    
    // Log analytics space
    resource logAnalytics 'Microsoft.OperationalInsights/workspaces@2022-10-01' = {
      name: '${functionAppName}-logs'
      location: location
      properties: {
        sku: {
          name: 'PerGB2018'
        }
      }
    }
    
    // application insights
    resource applicationInsight 'Microsoft.Insights/components@2020-02-02' = {
      name: applicationInsightsName
      location: location
      properties: {
        Application_Type: 'web'
        WorkspaceResourceId: logAnalytics.id
      }
      kind: 'web'
    }
    
    // Storage account (for storing function app)
    resource storageAccount 'Microsoft.Storage/storageAccounts@2023-01-01' = {
      name: 'myfunctionappstorage'
      location: location
      kind: 'StorageV2'
      sku: {
        name: 'Standard_LRS'
      }
    }
    
    resource appEnvironment 'Microsoft.App/managedEnvironments@2023-05-02-preview' = {
      name: functionAppEnvName
      location: location
      properties: {
        appLogsConfiguration: {
          destination: 'log-analytics'
          logAnalyticsConfiguration: {
            customerId: logAnalytics.properties.customerId
            sharedKey: logAnalytics.listKeys().primarySharedKey
          }
        }
      }
    }
    
    // Function app
    resource functionApp 'Microsoft.Web/sites@2022-09-01' = {
      name: functionAppName
      location: location
      kind: 'functionapp,linux,container,azurecontainerapps'
      properties: {
        managedEnvironmentId: appEnvironment.id
        siteConfig: {
          linuxFxVersion: 'Docker|ghcr.io/my-repo/my-functions-image:latest'
          appSettings: [
            {
              name: 'FUNCTIONS_WORKER_RUNTIME'
              value: 'dotnet-isolated'
            }
            {
              name: 'FUNCTIONS_EXTENSION_VERSION'
              value: '~4'
            }
            {
                name: 'DOCKER_REGISTRY_SERVER_URL'
                value: 'ghcr.io'
            }
            {
                name: 'DOCKER_REGISTRY_SERVER_USERNAME'
                value: registryUserName
            }
            {
                name: 'DOCKER_REGISTRY_SERVER_PASSWORD'
                value: registryPassword
            }
            {
                name: 'WEBSITES_ENABLE_APP_SERVICE_STORAGE'
                value: 'false'
            }
            {
              name: 'APPLICATIONINSIGHTS_CONNECTION_STRING'
              value: applicationInsight.properties.ConnectionString
            }
            {
              name: 'AzureWebJobsStorage'
              value: storageAccount.properties.primaryEndpoints.blob
            }
            {
              name: 'EnvVar1'
              value: 'value of environment variable'
            }
          ]
          connectionStrings: [
            {
              name: 'DefaultConnection'
              connectionString: 'connection string to a SQL Azure database'
              type: 'SQLAzure'
            }
          ]
        }
      }
    }
    
    output functionAppHost string = functionApp.properties.defaultHostName
    
azure azure-bicep azure-functions container dotnetcore

Join the Discussion

View or add your thoughts below

Comments