【发布时间】:2021-01-12 21:22:19
【问题描述】:
我正在使用 Microsoft.Azure.Management.*.Fluent API 将资源部署到 Azure。正在部署的资源依次为 {Resource Group、Storage Account、App Service Plan、Function 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.StorageAccount 是null。我登录门户,查看函数应用的部署模板中的存储帐户值,发现 azureStorageAccounts 集合也是空的。
【问题讨论】:
标签: c# azure azure-resource-manager