【问题标题】:Why is the Storage Account is not listed in the function app?为什么存储帐户未在函数应用中列出?
【发布时间】:2021-01-12 21:22:19
【问题描述】:

我正在使用 Microsoft.Azure.Management.*.Fluent API 将资源部署到 Azure。正在部署的资源依次为 {Resource GroupStorage AccountApp Service PlanFunction App}。

我很好奇为什么我的存储帐户没有分配给我的函数应用

//Create Resource Group
Logger.Info($"Checking for Resource Group: {ResourceGroupName}");
resourceGroup = (await azure.ResourceGroups.ListAsync()).FirstOrDefault(rg => rg.Name == ResourceGroupName);
if (resourceGroup != null)
{
    Logger.Info($"\tFound: {resourceGroup.Name}");
}
else
{
    Logger.Info($"\tNot found. Provisioning resource on Azure...");
    resourceGroup = azure.ResourceGroups.Define(ResourceGroupName).WithRegion(Region.USCentral).Create();
    Logger.Info(
        await azure.ResourceGroups.ContainAsync(ResourceGroupName)
        ? $"\t{ResourceGroupName} Created"
        : $"\tFailed to create Resource Group: {ResourceGroupName}."
    );
}


//Create Storage Account
Logger.Info($"Checking for storage account: {StorageAccountName} in resource group: {ResourceGroupName}");
storageAccount = (await azure.StorageAccounts.ListByResourceGroupAsync(ResourceGroupName))
        .FirstOrDefault(sa => sa.Name == StorageAccountName);
if (storageAccount != null)
{         
    Logger.Info($"\tFound: {storageAccount.Name}");              
}
else
{
    Logger.Info($"\tNot found. Provisioning resource on Azure...");
    storageAccount = await azure.StorageAccounts.Define(StorageAccountName)
        .WithRegion(Region.USCentral)
        .WithExistingResourceGroup(resourceGroup)
        .WithBlobEncryption()
        .WithGeneralPurposeAccountKindV2()
        .WithFileEncryption()
        .WithOnlyHttpsTraffic()
        .WithSku(StorageAccountSkuType.Standard_LRS)
        .WithHnsEnabled(true)
        .CreateAsync();

    Logger.Info(
        (await azure.StorageAccounts.ListByResourceGroupAsync(ResourceGroupName)).Any(x => x.Name == StorageAccountName)
        ? $"\tStorage Account: {StorageAccountName} Created Successfully"
        : $"\tFailed to create Storage Account: {StorageAccountName}."
    );
}


//Create App Service Plan
Logger.Info($"Checking for App Service Plan: {AppServicePlanName} in resource group: {ResourceGroupName}");
appServicePlan = (await azure.AppServices.AppServicePlans.ListByResourceGroupAsync(ResourceGroupName))
        .FirstOrDefault(asp => asp.Name == AppServicePlanName);
if (appServicePlan != null)
{  
    Logger.Info($"\tFound: {appServicePlan.Name}");
}
else
{
    Logger.Info($"\tNot found. Provisioning resource on Azure...");
    appServicePlan = await azure.AppServices.AppServicePlans
    .Define(AppServicePlanName)
    .WithRegion(Region.USCentral)
    .WithExistingResourceGroup(resourceGroup)
    .WithConsumptionPricingTier()
    .CreateAsync();

    Logger.Info(
        ((await azure.AppServices.AppServicePlans.ListByResourceGroupAsync(ResourceGroupName)).Any(x => x.Name == AppServicePlanName))
        ? $"\tApp Service Plan: {AppServicePlanName} Created Successfully"
        : $"\tFailed to create App Service Plan: {AppServicePlanName}."
    );
}

//Create Function App
Logger.Info($"Checking for Function App: {FunctionAppName} in resource group: {ResourceGroupName}");
functionApp = (await azure.AppServices.FunctionApps.ListByResourceGroupAsync(ResourceGroupName)).FirstOrDefault(x => x.Name == FunctionAppName);
if (functionApp != null)
{
    Logger.Info($"\tFound: {functionApp.Name}");
}
else
{
    Logger.Info($"\tNot found. Provisioning resource on Azure...");
    functionApp = await azure.AppServices.FunctionApps
        .Define(FunctionAppName)
        .WithExistingAppServicePlan(appServicePlan)
        .WithExistingResourceGroup(resourceGroup)
        .WithExistingStorageAccount(storageAccount)
        .WithRuntimeVersion("~3")
        .WithHttpsOnly(true)
        .WithWebSocketsEnabled(true)
        .WithPlatformArchitecture(PlatformArchitecture.X64)
        .WithSystemAssignedManagedServiceIdentity()
        .CreateAsync();

    Logger.Info(
        ((await azure.AppServices.FunctionApps.ListByResourceGroupAsync(ResourceGroupName)).Any(x => x.Name == FunctionAppName))
        ? $"\tFunction app: {FunctionAppName} Created Successfully"
        : $"\tFailed to create Function App: {FunctionAppName}."
    );
}
Logger.Info("\n");

在构建函数应用时,我指定了.WithExistingStorageAccount(storageAccount),我相信它会将存储帐户添加到函数应用中。

虽然,当我检查返回值时,functionApp.StorageAccountnull。我登录门户,查看函数应用的部署模板中的存储帐户值,发现 azureStorageAccounts 集合也是空的。

【问题讨论】:

    标签: c# azure azure-resource-manager


    【解决方案1】:

    资源组实际上正在被添加到 azure 函数中 - 但不是作为 ARM 模板本身的一部分。

    使用.WithExistingStorageAccount(storageAccount) 添加的存储帐户将作为键值对添加到应用程序设置中。

    azureStorageAccounts 属性是另一个资源 Microsoft.Web/sites/config 的属性,它依赖于函数应用。

    【讨论】:

      猜你喜欢
      • 2018-02-21
      • 1970-01-01
      • 2014-06-28
      • 2022-12-21
      • 2021-10-12
      • 1970-01-01
      • 2014-05-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多