azure - Bicep 在新应用服务中设置 appSettings 时引用存储帐户,出现 StorageAccountIsNotProvisioned 错误

标签 azure azure-bicep

使用 bicep,我正在创建一个存储帐户,并创建一个应用服务。对于应用服务,它尝试将 AzureWebJobsStorage 应用设置设置为指向存储帐户。我收到以下错误:

...
"details": [
  {
    "code": "StorageAccountIsNotProvisioned",
    "message": "The storage account provisioning state must be 'Succeeded' before executing the operation."
  }
]
...

看来,即使我设置了依赖项,函数应用程序的创建也不会等待存储帐户成功完成,并且必须对函数应用程序的 appsettings 值进行验证。我认为该错误是指下面二头肌中 appService 资源的 AzureWebJobsStorage appsettings 键。

如果我第二次运行二头肌,一切都会正常,因为存储帐户已经创建。 bicep 文件中是否有某种方法可以确保在创建函数应用程序之前已成功创建存储帐户?

有趣的是,它永远不会在对应用程序见解的引用上出错,只会在对存储帐户的引用上出错,并且这两个引用都在应用程序设置中。

在一个模块中,我创建了我的存储帐户:

param tags object
param storeAccountName string
param location string = resourceGroup().location
param skuName string = 'Standard_LRS' 


resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = {
  name: toLower(storeAccountName)
  kind: 'StorageV2'
  location : location
  sku: {
    name: skuName
  }
  tags: tags
}

在另一个模块中,我创建了一个函数应用程序,并引用存储帐户:

param tags object
param name string
param location string = resourceGroup().location
param appServicePlanId string
param applicationInsightsName string
param storageAccountName string

resource appInsights 'Microsoft.Insights/components@2020-02-02' existing = {
    name: applicationInsightsName
}

resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' existing = {
    name: toLower(storageAccountName)
}

resource appService 'Microsoft.Web/sites@2022-03-01' = {
    name: name
    location: location
    kind: 'functionapp'
    properties: {
        serverFarmId: appServicePlanId
        siteConfig: {
            appSettings: [
                //Storage settings
                {
                    name: 'AzureWebJobsStorage'    
                    value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccount.name};EndpointSuffix=${environment().suffixes.storage};AccountKey=${storageAccount.listKeys().keys[0].value}'
                  }
                //Application insight settings
                {
                    //https://learn.microsoft.com/en-us/azure/azure-monitor/app/migrate-from-instrumentation-keys-to-connection-strings
                    name: 'APPLICATIONINSIGHTS_CONNECTION_STRING'
                    value: appInsights.properties.ConnectionString
                }
                {
                    name: 'APPINSIGHTS_INSTRUMENTATIONKEY'
                    value: appInsights.properties.InstrumentationKey
                }

                {
                    name: 'APPINSIGHTS_PROFILERFEATURE_VERSION'
                    value: '1.0.0'
                }
                {
                    name: 'APPINSIGHTS_SNAPSHOTFEATURE_VERSION'
                    value: '1.0.0'
                }
                {
                    name: 'ApplicationInsightsAgent_EXTENSION_VERSION'
                    value: '~2'
                }
                {
                    name: 'XDT_MicrosoftApplicationInsights_Java'
                    value: '1'
                }
                {
                    name: 'XDT_MicrosoftApplicationInsights_Mode'
                    value: 'recommended'
                }
                {
                    name: 'XDT_MicrosoftApplicationInsights_NodeJS'
                    value: '1'
                }
                {
                    name: 'InstrumentationEngine_EXTENSION_VERSION'
                    value: 'disabled'
                }
                {
                    name: 'SnapshotDebugger_EXTENSION_VERSION'
                    value: 'disabled'
                }
                {
                    name: 'XDT_MicrosoftApplicationInsights_BaseExtensions'
                    value: 'disabled'
                }
                {
                    name: 'XDT_MicrosoftApplicationInsights_PreemptSdk'
                    value: 'disabled'
                }
            ]
        }
    }
    dependsOn: [
        //Some Appsettings depend on resources that must be deployed first
        appInsights
        storageAccount
    ]
    tags: tags
    identity: {
        type: 'SystemAssigned'
    }
}

输出principalId字符串=appService.identity.principalId

我的主要二头肌文件使用以下模块:

param location string = resourceGroup().location
param storageAccountSKUName string = 'Standard_LRS'
param functionAppServiceSKUName string = 'Y1'
param logAnalyticsSKUName string = 'Standard'

var storageAccountName = 'mystorage'
var logAnalyticsWorkspaceName = 'mylogs'
var appInsightsName = 'myinsights'
var appServicePlanName = 'myappservice'
var tags = {...}

module storageAccount './modules/storage/StorageAccount.bicep' = {
  name:'storageAccount'
  params: {
    tags: tags
    location:location
    storeAccountName:storageAccountName
    skuName:storageAccountSKUName
  }
}

module logWorkspace './modules/loganalytics/LogWorkspace.bicep' = {
  name: 'logAnalyticsWorkspace'
  params: {
    name: logAnalyticsWorkspaceName
    tags: tags
    skuName: logAnalyticsSKUName
    location: location    
  }
}

module appInsights './modules/loganalytics/AppInsignts.bicep' = {
  name: 'appInsights'
  params: {
    name:appInsightsName
    tags: tags
    location:location    
    workspaceId:logWorkspace.outputs.resourceId
  }
}

module appServicePlan './modules/services/AppServicePlan.bicep' = 
{
    name: 'appServicePlan'
    params: {
        tags: tags
        name: appServicePlanName
        skuName: appServiceSKUName
        location:location
    }
}

module appService './modules/services/AppServiceReceiver.bicep' = {
    name: 'appServiceReceiveHL7v2POI'
    params: {
        tags: tags
        name: appServiceReceiveHL7v2POIName
        location:location
        appServicePlanId:functionServicePlan.outputs.resourceId
        applicationInsightsName:appInsightsName
        storageAccountName:storageAccountName
    }
}

最佳答案

您的 AppService 模块没有等待存储创建。

module appService './modules/services/AppServiceReceiver.bicep' = {
    name: 'appServiceReceiveHL7v2POI'
    params: {
        tags: tags
        name: appServiceReceiveHL7v2POIName
        location:location
        appServicePlanId:functionServicePlan.outputs.resourceId
        applicationInsightsName:appInsightsName
        storageAccountName:storageAccountName  //This line is wrong
    }
}

需要将 DependsOn 添加到应用服务模块调用中。这可以是显式的,也可以是隐式的。

明确(不是很好的做法);

module appService './modules/services/AppServiceReceiver.bicep' = {
    name: 'appServiceReceiveHL7v2POI'
    params: {
        tags: tags
        name: appServiceReceiveHL7v2POIName
        location:location
        appServicePlanId:functionServicePlan.outputs.resourceId
        applicationInsightsName:appInsightsName
        storageAccountName:storageAccountName
    }
    dependsOn: [
        storageAccount
    ]
}

或隐式

module appService './modules/services/AppServiceReceiver.bicep' = {
    name: 'appServiceReceiveHL7v2POI'
    params: {
        tags: tags
        name: appServiceReceiveHL7v2POIName
        location:location
        appServicePlanId:functionServicePlan.outputs.resourceId
        applicationInsightsName:appInsightsName
        storageAccountName: storageAccount.outputs.name 
    }
}

您目前在appService 模块内部有dependsOn,但为时已晚。在引用模块之前您需要它才能正确构建依赖关系链。

关于azure - Bicep 在新应用服务中设置 appSettings 时引用存储帐户,出现 StorageAccountIsNotProvisioned 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76535492/

相关文章:

azure - Microsoft.Web.Deployment.DeploymentManager 的类型初始值设定项引发异常

Azure(策略/RBAC/MFA)-如何阻止用户

azure - 如何结合多订阅和多区域功能在 Bicep 中创建 Azure 警报

azure - 从二头肌中的 Azure 应用服务访问应用程序(客户端)ID

azure-resource-manager - 如何使用 Bicep 引用其他订阅中的资源?

azure - 是否可以在azure应用程序服务上禁用HTTP,而不仅仅是将其重定向到HTTPS

asp.net-mvc-3 - 将 Azure 报告添加到 MVC 应用程序

php - SQLSTATE[01002] Adaptive Server 连接失败(严重性 9)

azure - 如何使用 Bicep 引用对象数组中的对象

azure - 在企业笔记本电脑中安装 Bicep cli 错误