最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Azure Function app FlexConsumption deploy from bicep - Stack Overflow

programmeradmin3浏览0评论

I try to deploy an azure function with its code using bicep template.

The problem is that i got 404 error when try to deploy.

after some investigation, it is look like that, as part of the bicep deploy it remove all deployment logs includes the logs of the current new code deployment and then the arm deployment got an 404 error (when try to get the status of current deployment).

When i look at the function deployment logs and refresh it, i found this error before the logs disappeared and the 404 error raised:

"The logs you are looking for were not found. In consumption plans, the instance will be recycled and logs will not be persisted after that. If you want them to be persisted ensure that Application Insights is enabled. See the Deployment Logs section here for more info: " ( the link is broken and the logs are found in insights ...)

an example bicep:

(the function blob container is already created when the storage account was created )

param location string = 'northeurope'
param name string = '<func-name>'
param storageAccountName string = '<my-storage>'
param artifactBlobName string = '<blob-name-in-storage>'
param subnetId string = '<subnet-id>'

resource appServicePlan 'Microsoft.Web/serverfarms@2024-04-01' = {
  name: 'sp-${name}'
  location: location
  sku: {
    name: 'FC1'
    tier: 'FlexConsumption'
    capacity: 2
  }
  kind: 'functionapp,linux'
  properties: {
    reserved: true
  }
}

resource logAnalytics 'Microsoft.OperationalInsights/workspaces@2021-12-01-preview' = {
  name: 'workspace-${name}'
  location: location
  properties: any({
    retentionInDays: 30
    features: {
      searchVersion: 1
    }
    sku: {
      name: 'PerGB2018'
    }
  })
}
resource applicationInsights 'Microsoft.Insights/components@2020-02-02' = {
  name: 'insights-${name}'
  location: location
  kind: 'web'
  properties: {
    Application_Type: 'web'
    WorkspaceResourceId: logAnalytics.id
  }
}

// Reference the existing storage account (used for code artifact storage)
resource functionsStorageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' existing = {
  name: storageAccountName
}

resource functionApp 'Microsoft.Web/sites@2024-04-01' = {
  name: name
  location: location
  kind: 'functionapp,linux'
  identity: { type: 'SystemAssigned' }
  properties: {
    httpsOnly: true
    publicNetworkAccess: 'Enabled'
    reserved: true
    vnetRouteAllEnabled: true
    serverFarmId: appServicePlan.id
    siteConfig: {
      appSettings: [
        {
          name: 'AzureWebJobsStorage__accountName'
          value: storageAccountName
        }
        {
          name: 'APPLICATIONINSIGHTS_CONNECTION_STRING'
          value: applicationInsights.properties.ConnectionString
        }
      ]
    }

    functionAppConfig: {
      scaleAndConcurrency: {
        maximumInstanceCount: 100
        instanceMemoryMB: 2048
        alwaysReady: []
      }
      runtime: { name: 'node', version: '20' }
      deployment: {
        storage: {
          type: 'blobContainer'
          value: 'https://${storageAccountName}.blob.${environment().suffixes.storage}/${name}'
          authentication: {
            type: 'SystemAssignedIdentity'
          }
        }
      }
    }
    virtualNetworkSubnetId: subnetId
  }
}

param deployTime string = utcNow('u')

// Generate a SAS token for the artifact blob (valid for 1 hour)
var serviceSasToken = functionsStorageAccount.listServiceSas(functionsStorageAccount.apiVersion, {
  signedResource: 'b'
  signedPermission: 'rl'
  // The canonicalized resource should be in the format: /blob/<storageAccount>/<container>/<blob>
  canonicalizedResource: '/blob/${storageAccountName}/artifacts/${artifactBlobName}'
  signedExpiry: dateTimeAdd(deployTime, 'PT1H')
}).serviceSasToken

// Build the full artifact URL including the SAS token and a query parameter to force a change
var artifactUrl = 'https://${storageAccountName}.blob.${environment().suffixes.storage}/artifacts/${artifactBlobName}?${serviceSasToken}'

// Deploy the OneDeploy extension to the Function App
resource functionOneDeploy 'Microsoft.Web/sites/extensions@2024-04-01' = {
  parent: functionApp
  name: 'onedeploy'
  properties: {
    packageUri: artifactUrl
    remoteBuild: false
  }
}

I try to deploy an azure function with its code using bicep template.

The problem is that i got 404 error when try to deploy.

after some investigation, it is look like that, as part of the bicep deploy it remove all deployment logs includes the logs of the current new code deployment and then the arm deployment got an 404 error (when try to get the status of current deployment).

When i look at the function deployment logs and refresh it, i found this error before the logs disappeared and the 404 error raised:

"The logs you are looking for were not found. In consumption plans, the instance will be recycled and logs will not be persisted after that. If you want them to be persisted ensure that Application Insights is enabled. See the Deployment Logs section here for more info: https://aka.ms/flex-deployments " ( the link is broken and the logs are found in insights ...)

an example bicep:

(the function blob container is already created when the storage account was created )

param location string = 'northeurope'
param name string = '<func-name>'
param storageAccountName string = '<my-storage>'
param artifactBlobName string = '<blob-name-in-storage>'
param subnetId string = '<subnet-id>'

resource appServicePlan 'Microsoft.Web/serverfarms@2024-04-01' = {
  name: 'sp-${name}'
  location: location
  sku: {
    name: 'FC1'
    tier: 'FlexConsumption'
    capacity: 2
  }
  kind: 'functionapp,linux'
  properties: {
    reserved: true
  }
}

resource logAnalytics 'Microsoft.OperationalInsights/workspaces@2021-12-01-preview' = {
  name: 'workspace-${name}'
  location: location
  properties: any({
    retentionInDays: 30
    features: {
      searchVersion: 1
    }
    sku: {
      name: 'PerGB2018'
    }
  })
}
resource applicationInsights 'Microsoft.Insights/components@2020-02-02' = {
  name: 'insights-${name}'
  location: location
  kind: 'web'
  properties: {
    Application_Type: 'web'
    WorkspaceResourceId: logAnalytics.id
  }
}

// Reference the existing storage account (used for code artifact storage)
resource functionsStorageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' existing = {
  name: storageAccountName
}

resource functionApp 'Microsoft.Web/sites@2024-04-01' = {
  name: name
  location: location
  kind: 'functionapp,linux'
  identity: { type: 'SystemAssigned' }
  properties: {
    httpsOnly: true
    publicNetworkAccess: 'Enabled'
    reserved: true
    vnetRouteAllEnabled: true
    serverFarmId: appServicePlan.id
    siteConfig: {
      appSettings: [
        {
          name: 'AzureWebJobsStorage__accountName'
          value: storageAccountName
        }
        {
          name: 'APPLICATIONINSIGHTS_CONNECTION_STRING'
          value: applicationInsights.properties.ConnectionString
        }
      ]
    }

    functionAppConfig: {
      scaleAndConcurrency: {
        maximumInstanceCount: 100
        instanceMemoryMB: 2048
        alwaysReady: []
      }
      runtime: { name: 'node', version: '20' }
      deployment: {
        storage: {
          type: 'blobContainer'
          value: 'https://${storageAccountName}.blob.${environment().suffixes.storage}/${name}'
          authentication: {
            type: 'SystemAssignedIdentity'
          }
        }
      }
    }
    virtualNetworkSubnetId: subnetId
  }
}

param deployTime string = utcNow('u')

// Generate a SAS token for the artifact blob (valid for 1 hour)
var serviceSasToken = functionsStorageAccount.listServiceSas(functionsStorageAccount.apiVersion, {
  signedResource: 'b'
  signedPermission: 'rl'
  // The canonicalized resource should be in the format: /blob/<storageAccount>/<container>/<blob>
  canonicalizedResource: '/blob/${storageAccountName}/artifacts/${artifactBlobName}'
  signedExpiry: dateTimeAdd(deployTime, 'PT1H')
}).serviceSasToken

// Build the full artifact URL including the SAS token and a query parameter to force a change
var artifactUrl = 'https://${storageAccountName}.blob.${environment().suffixes.storage}/artifacts/${artifactBlobName}?${serviceSasToken}'

// Deploy the OneDeploy extension to the Function App
resource functionOneDeploy 'Microsoft.Web/sites/extensions@2024-04-01' = {
  parent: functionApp
  name: 'onedeploy'
  properties: {
    packageUri: artifactUrl
    remoteBuild: false
  }
}
Share Improve this question edited Mar 10 at 15:52 Jahnavi 8,2211 gold badge6 silver badges13 bronze badges Recognized by Microsoft Azure Collective asked Mar 9 at 14:56 y. bsy. bs 5404 silver badges15 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The "Internal server error" might be occurring due to the below reasons:

Firstly, make sure that a service endpoint delegation is properly configured between the Function App and the virtual network subnet before integrating them.

Add below service endpoints block under virtual network configuration. If you are using an existing vnet from the portal, you can add it directly over there.

serviceEndpoints: [
 {
   service: 'Microsoft.Storage'
   locations: [ location ]
 }
 {
   service: 'Microsoft.Web'
 }
]

Refer SO worked by me for the relevant issue.

Also, check the available regions for deploying a flex consumption plan function app and deploy it those regions accordingly.

`az functionapp list-flexconsumption-locations`

Modified Bicep code:

param location string = 'eastus'
param functionPlanName string = 'asp-japroduct'
param functionAppName string = 'jahappprod'
param functionAppRuntime string = 'dotnet-isolated'
param functionAppRuntimeVersion string = '8.0'
param storageAccountName string = 'mystorejahst'
param logAnalyticsName string = 'worksjah'
param applicationInsightsName string = 'virtualinshg'
param maximumInstanceCount int = 100
param instanceMemoryMB int = 2048
param resourceNameNsgBusiness string = 'nsg-business-enb'
param vnetResourceName string = 'vnetlkenvironment'
param vnetAddressPrefix string = '10.0.0.0/16'
param subnetPrefixBusiness string = '10.0.1.0/24'
param resourceNameSubnetBusiness string = 'subnet--business'
var resourceToken = toLower(uniqueString(subscription().id, resourceGroup().name, location))
var deploymentStorageContainerName = 'app-package-${take(functionAppName, 32)}-${take(resourceToken, 7)}'
var storageRoleDefinitionId = 'b7e6dc6d-f1e8-4753-8033-0f276bb0955b'
resource nsgBusiness 'Microsoft.Network/networkSecurityGroups@2024-01-01' = {
  name: resourceNameNsgBusiness
  location: location
}
resource vnet 'Microsoft.Network/virtualNetworks@2024-01-01' = {
  name: vnetResourceName
  location: location
  properties: {
    addressSpace: {
      addressPrefixes: [
        vnetAddressPrefix
      ]
    }
    enableDdosProtection: false
    enableVmProtection: false
  }  
}
resource subnet 'Microsoft.Network/virtualNetworks/subnets@2024-03-01' = {
  parent: vnet
  name: resourceNameSubnetBusiness
  properties: {
    addressPrefix: subnetPrefixBusiness
    networkSecurityGroup: {
      id: nsgBusiness.id
    }
    privateEndpointNetworkPolicies: 'Enabled'
    privateLinkServiceNetworkPolicies: 'Enabled'
    serviceEndpoints: [
      {
        service: 'Microsoft.Storage'
        locations: [ location ]
      }
      {
        service: 'Microsoft.Web'
      }
    ]
  }
}

resource logAnalytics 'microsoft.operationalinsights/workspaces@2021-06-01' = {
  name: logAnalyticsName
  location: location
  properties: {
    retentionInDays: 30
    features: {
      searchVersion: 1
    }
    sku: {
      name: 'PerGB2018'
    }
  }
}

resource applicationInsights 'Microsoft.Insights/components@2020-02-02' = {
  name: applicationInsightsName
  location: location
  kind: 'web'
  properties: {
    Application_Type: 'web'
    WorkspaceResourceId: logAnalytics.id
  }
}

resource storageAccount 'Microsoft.Storage/storageAccounts@2023-01-01' = {
  name: storageAccountName
  location: location
  sku: {
    name: 'Standard_LRS'
  }
  kind: 'StorageV2'
  properties: {
    accessTier: 'Hot'
    allowSharedKeyAccess: false
    publicNetworkAccess: 'Enabled'
  }
}

resource storageAccountName_default 'Microsoft.Storage/storageAccounts/blobServices@2023-01-01' = {
  parent: storageAccount
  name: 'default'
}

resource storageAccountName_default_deploymentStorageContainer 'Microsoft.Storage/storageAccounts/blobServices/containers@2023-01-01' = {
  parent: storageAccountName_default
  name: deploymentStorageContainerName
  properties: {
    publicAccess: 'None'
  }
}

resource functionPlan 'Microsoft.Web/serverfarms@2023-12-01' = {
  name: functionPlanName
  location: location
  kind: 'functionapp'
  sku: {
    tier: 'FlexConsumption'
    name: 'FC1'
  }
  properties: {
    reserved: true
  }
}

resource functionApp 'Microsoft.Web/sites@2023-12-01' = {
  name: functionAppName
  location: location
  kind: 'functionapp,linux'
  identity: {
    type: 'SystemAssigned'
  }
  properties: {
    serverFarmId: functionPlan.id
    functionAppConfig: {
      deployment: {
        storage: {
          type: 'blobContainer'
          value: 'concat(storageAccount.properties.primaryEndpoints.blob, deploymentStorageContainerName)'
          authentication: {
            type: 'SystemAssignedIdentity'
          }
        }
      }
      scaleAndConcurrency: {
        maximumInstanceCount: maximumInstanceCount
        instanceMemoryMB: instanceMemoryMB
      }
      runtime: {
        name: functionAppRuntime
        version: functionAppRuntimeVersion
      }
    }
    siteConfig: {
      appSettings: [
        {
          name: 'AzureWebJobsStorage__accountName'
          value: storageAccountName
        }
        {
          name: 'APPLICATIONINSIGHTS_CONNECTION_STRING'
          value: applicationInsights.id
        }
      ]
    }
  }
}

resource Microsoft_Storage_storageAccounts_storageAccountName_storageRoleDefinitionId 'Microsoft.Authorization/roleAssignments@2020-04-01-preview' = {
  scope: storageAccount
  name: guid(storageAccount.id, storageRoleDefinitionId)
  properties: {
    roleDefinitionId: resourceId('Microsoft.Authorization/roleDefinitions', storageRoleDefinitionId)
    principalId: functionApp.identity.principalId
  }
}
param deployTime string = utcNow('u')

var serviceSasToken = storageAccount.listServiceSas(
  storageAccount.apiVersion, {
    signedResource: 'b'
    signedPermission: 'rl'
    canonicalizedResource: string('/blob/${storageAccountName}/artifacts')
    signedExpiry: dateTimeAdd(deployTime, 'PT1H')
  }
).serviceSasToken


var artifactUrl = 'https://${storageAccountName}.blob.${environment().suffixes.storage}/artifacts/${deploymentStorageContainerName}?${serviceSasToken}'

resource functionOneDeploy 'Microsoft.Web/sites/extensions@2024-04-01' = {
  parent: functionApp
  name: 'onedeploy'
  properties: {
    packageUri: artifactUrl
    remoteBuild: false
  }
}

Deployment succeeded:

发布评论

评论列表(0)

  1. 暂无评论