我正在尝试设置一个 Azure 函数,该函数将在工作时间内将我们的应用服务计划扩展至更大,并在工作时间之外扩展至更小。我取自the answer to this question .
public static class ScaleUpWeekdayMornings
{
[FunctionName(nameof(ScaleUpWeekdayMornings))]
public static async Task Run([TimerTrigger("0 0 13 * * 1-5")]TimerInfo myTimer, ILogger log) // Uses GMT timezone
{
var resourceGroup = "DesignServiceResourceGroup";
var appServicePlanName = "DesignService";
var webSiteManagementClient = await Utilities.GetWebSiteManagementClient();
var appServicePlanRequest = await webSiteManagementClient.AppServicePlans.ListByResourceGroupWithHttpMessagesAsync(resourceGroup);
appServicePlanRequest.Body.ToList().ForEach(x => log.LogInformation($">>>{x.Name}"));
var appServicePlan = appServicePlanRequest.Body.Where(x => x.Name.Equals(appServicePlanName)).FirstOrDefault();
if (appServicePlan == null)
{
log.LogError("Could not find app service plan.");
return;
}
appServicePlan.Sku.Family = "P";
appServicePlan.Sku.Name = "P2V2";
appServicePlan.Sku.Size = "P2V2";
appServicePlan.Sku.Tier = "Premium";
appServicePlan.Sku.Capacity = 1;
var updateResult = await webSiteManagementClient.AppServicePlans.CreateOrUpdateWithHttpMessagesAsync(resourceGroup, appServicePlanName, appServicePlan);
}
}
public static class Utilities
{
public static async Task<WebSiteManagementClient> GetWebSiteManagementClient()
{
var azureServiceTokenProvider = new AzureServiceTokenProvider();
var accessToken = await azureServiceTokenProvider.GetAccessTokenAsync("https://management.azure.com/");
var tokenCredentials = new TokenCredentials(accessToken);
return new WebSiteManagementClient(tokenCredentials)
{
SubscriptionId = "<realSubscriptionIdHere>",
};
}
}
当我在本地运行它时,它可以工作,并且实际上在我们的 Azure 应用服务计划上执行扩展(我相信是通过 Azure CLI)。但是,当此 Azure Function 部署到 Azure 时,它找不到任何应用服务计划。它到达 appservicePlan == null
block 并返回。我已为已部署的 Azure Function 打开托管服务标识,并在我想要扩展的应用服务中授予其贡献者权限。
我错过了什么吗?为什么会这样
await webSiteManagementClient.AppServicePlans.ListByResourceGroupWithHttpMessagesAsync(resourceGroup);
作为已发布的 Azure 函数的一部分运行时不返回任何内容?
最佳答案
问题是在应用服务(它是应用服务计划中的应用)上设置了权限。需要在资源组上进行设置才能读取应用服务计划。
关于azure - 使用 Azure 托管服务标识来扩展应用服务计划,但未列出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57891809/