gpt4 book ai didi

Azure Function App 基础设施重新部署导致应用程序中的现有函数因缺少需求而失败,并且还会删除以前的调用数据

转载 作者:行者123 更新时间:2023-12-03 00:54:04 26 4
gpt4 key购买 nike

我面临着一个很大的问题。我有一个由 Azure Bicep 按以下方式部署的函数应用程序:

param environmentType string
param location string
param storageAccountSku string
param vnetIntegrationSubnetId string
param kvName string

/*
This module contains the IaC for deploying the Premium function app
*/

/// Just a single minimum instance to start with and max scaling of 3 for dev, 5 for prd ///
var minimumElasticSize = 1
var maximumElasticSize = ((environmentType == 'prd') ? 5 : 3)
var name = 'nlp'
var functionAppName = 'function-app-${name}-${environmentType}'

/// Storage account for service ///
resource functionAppStorage 'Microsoft.Storage/storageAccounts@2019-06-01' = {
name: 'st4functionapp${name}${environmentType}'
location: location
kind: 'StorageV2'
sku: {
name: storageAccountSku
}
properties: {
allowBlobPublicAccess: false
accessTier: 'Hot'
supportsHttpsTrafficOnly: true
minimumTlsVersion: 'TLS1_2'
}
}

/// Premium app plan for the service ///
resource servicePlanfunctionApp 'Microsoft.Web/serverfarms@2021-03-01' = {
name: 'plan-${name}-function-app-${environmentType}'
location: location
kind: 'linux'
sku: {
name: 'EP1'
tier: 'ElasticPremium'
family: 'EP'
}
properties: {
reserved: true
targetWorkerCount: minimumElasticSize
maximumElasticWorkerCount: maximumElasticSize
elasticScaleEnabled: true
isSpot: false
zoneRedundant: ((environmentType == 'prd') ? true : false)
}
}

// Create log analytics workspace
resource logAnalyticsWorkspacefunctionApp 'Microsoft.OperationalInsights/workspaces@2021-06-01' = {
name: '${name}-functionapp-loganalytics-workspace-${environmentType}'
location: location
properties: {
sku: {
name: 'PerGB2018' // Standard
}
}
}

/// Log analytics workspace insights ///
resource applicationInsightsfunctionApp 'Microsoft.Insights/components@2020-02-02' = {
name: 'application-insights-${name}-function-${environmentType}'
location: location
kind: 'web'
properties: {
Application_Type: 'web'
Flow_Type: 'Bluefield'
publicNetworkAccessForIngestion: 'Enabled'
publicNetworkAccessForQuery: 'Enabled'
Request_Source: 'rest'
RetentionInDays: 30
WorkspaceResourceId: logAnalyticsWorkspacefunctionApp.id
}
}

// App service containing the workflow runtime ///
resource sitefunctionApp 'Microsoft.Web/sites@2021-03-01' = {
name: functionAppName
location: location
kind: 'functionapp,linux'
identity: {
type: 'SystemAssigned'
}
properties: {
clientAffinityEnabled: false
httpsOnly: true
serverFarmId: servicePlanfunctionApp.id
siteConfig: {
linuxFxVersion: 'python|3.9'
minTlsVersion: '1.2'
pythonVersion: '3.9'
use32BitWorkerProcess: true
appSettings: [
{
name: 'FUNCTIONS_EXTENSION_VERSION'
value: '~4'
}
{
name: 'FUNCTIONS_WORKER_RUNTIME'
value: 'python'
}
{
name: 'AzureWebJobsStorage'
value: 'DefaultEndpointsProtocol=https;AccountName=${functionAppStorage.name};AccountKey=${listKeys(functionAppStorage.id, '2019-06-01').keys[0].value};EndpointSuffix=core.windows.net'
}
{
name: 'WEBSITE_CONTENTAZUREFILECONNECTIONSTRING'
value: 'DefaultEndpointsProtocol=https;AccountName=${functionAppStorage.name};AccountKey=${listKeys(functionAppStorage.id, '2019-06-01').keys[0].value};EndpointSuffix=core.windows.net'
}
{
name: 'WEBSITE_CONTENTSHARE'
value: 'app-${toLower(name)}-functionservice-${toLower(environmentType)}a6e9'
}
{
name: 'APPINSIGHTS_INSTRUMENTATIONKEY'
value: applicationInsightsfunctionApp.properties.InstrumentationKey
}
{
name: 'ApplicationInsightsAgent_EXTENSION_VERSION'
value: '~2'
}
{
name: 'APPLICATIONINSIGHTS_CONNECTION_STRING'
value: applicationInsightsfunctionApp.properties.ConnectionString
}
{
name: 'ENV'
value: toUpper(environmentType)
}
]
}
}

/// VNET integration so flows can access storage and queue accounts ///
resource vnetIntegration 'networkConfig@2022-03-01' = {
name: 'virtualNetwork'
properties: {
subnetResourceId: vnetIntegrationSubnetId
swiftSupported: true
}
}
}

/// Outputs for creating access policies ///
output functionAppName string = sitefunctionApp.name
output functionAppManagedIdentityId string = sitefunctionApp.identity.principalId

输出用于授予 blob/queue 和一些 keyvault 内容的权限。此代码是在 main.bicep 中调用的单个模块。模块并通过 Azure DevOps 管道部署。

我有第二个存储库,其中有一些功能,并且我还通过 Azure Pipelines 进行部署。该文件包含三个用于部署的 .yaml 文件、2 个模板(CI 和 CD)和 1 个名为 azure-pipelines.yml 的主管道,将它们整合在一起:

函数-ci.yml:

parameters:
- name: environment
type: string

jobs:
- job:
displayName: 'Publish the function as .zip'
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '$(pythonVersion)'
displayName: 'Use Python $(pythonVersion)'

- task: CopyFiles@2
displayName: 'Create project folder'
inputs:
SourceFolder: '$(System.DefaultWorkingDirectory)'
Contents: |
**
TargetFolder: '$(Build.ArtifactStagingDirectory)'

- task: Bash@3
displayName: 'Install requirements for running function'
inputs:
targetType: 'inline'
script: |
python3 -m pip install --upgrade pip
pip install setup
pip install --target="./.python_packages/lib/site-packages" -r ./requirements.txt
workingDirectory: '$(Build.ArtifactStagingDirectory)'

- task: ArchiveFiles@2
displayName: 'Create project zip'
inputs:
rootFolderOrFile: '$(Build.ArtifactStagingDirectory)'
includeRootFolder: false
archiveType: 'zip'
archiveFile: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
replaceExistingArchive: true

- task: PublishPipelineArtifact@1
displayName: 'Publish project zip artifact'
inputs:
targetPath: '$(Build.ArtifactStagingDirectory)'
artifactName: 'functions$(environment)'
publishLocation: 'pipeline'

函数-cd.yml:

parameters:
- name: environment
type: string
- name: azureServiceConnection
type: string

jobs:
- job: worfklowsDeploy
displayName: 'Deploy the functions'
steps:
# Download created artifacts, containing the zipped function codes
- task: DownloadPipelineArtifact@2
inputs:
buildType: 'current'
artifactName: 'functions$(environment)'
targetPath: '$(Build.ArtifactStagingDirectory)'

# Zip deploy the functions code
- task: AzureFunctionApp@1
inputs:
azureSubscription: $(azureServiceConnection)
appType: functionAppLinux
appName: function-app-nlp-$(environment)
package: $(Build.ArtifactStagingDirectory)/**/*.zip
deploymentMethod: 'zipDeploy'

它们在azure-pipelines.yml中组合在一起:

trigger:
branches:
include:
- develop
- main

pool:
name: "Hosted Ubuntu 1804"

variables:
${{ if notIn(variables['Build.SourceBranchName'], 'main') }}:
environment: dev
azureServiceConnection: SC-NLPDT
${{ if eq(variables['Build.SourceBranchName'], 'main') }}:
environment: prd
azureServiceConnection: SC-NLPPRD
pythonVersion: '3.9'

stages:
# Builds the functions as .zip
- stage: functions_ci
displayName: 'Functions CI'
jobs:
- template: ./templates/functions-ci.yml
parameters:
environment: $(environment)

# Deploys .zip workflows
- stage: functions_cd
displayName: 'Functions CD'
jobs:
- template: ./templates/functions-cd.yml
parameters:
environment: $(environment)
azureServiceConnection: $(azureServiceConnection)

因此,当我还部署了基础设施代码时,这第一次成功部署了我的函数应用程序。导入完成得很好,部署了正确的功能应用程序,并且代码在我触发它时运行。

但是,当我去重新部署基础设施(二头肌)代码时,突然间,最新版本的功能消失了,并被以前的版本取代。另外,运行以前的版本不再起作用,因为我的所有需求都通过 pip install --target="./.python_packages/lib/site-packages" -r ./requirements.txt 安装在管道(CI 部分)中。突然找不到了,出现导入错误(即 Result: Failure Exception: ModuleNotFoundError: No module named 'azure.identity' )。请注意,这个版本以前工作得很好。

这对我来说是一个大问题,因为我需要能够更新一些基础设施(例如添加 APP_SETTING)而不破坏当前的功能部署。

我曾想过在基础设施更新后自动重新部署该函数,但我仍然错过了我需要能够看到的以前的调用。

我是否在上面的代码中遗漏了一些东西,因为我无法弄清楚这里会出现什么问题,导致我的功能在基础设施部署上发生变化...

最佳答案

查看documentation :

To enable your function app to run from a package, add a WEBSITE_RUN_FROM_PACKAGE setting to your function app settings.

1 Indicates that the function app runs from a local package file deployed in the d:\home\data\SitePackages (Windows) or /home/data/SitePackages (Linux) folder of your function app.

就您而言,当您使用 AzureFunctionApp@1zipDeploy 部署函数应用代码时,会自动将此应用设置添加到您的函数应用中。重新部署基础架构时,此设置将被删除,并且函数应用主机不知道在哪里可以找到代码。

如果您在二头肌文件中添加此应用程序设置,这应该可以工作:

{
name: 'WEBSITE_RUN_FROM_PACKAGE'
value: '1'
}

关于Azure Function App 基础设施重新部署导致应用程序中的现有函数因缺少需求而失败,并且还会删除以前的调用数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74266226/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com