使用 Azure 托管服务标识扩展应用服务计划,但已列出 none
Using Azure Managed Service Identities to scale App Service Plan, but none listed
我正在尝试设置一个 Azure Functions 来扩展我们的应用程序服务计划,使其在工作时间更大,而在工作时间以外更小。我取自 .
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 Functions 部署到 Azure 时,它找不到任何 App Service Plans。它命中 appservicePlan == null
块和 returns。我已经为已部署的 Azure 函数打开托管服务标识,并在我要为其扩展的应用服务中授予它贡献者权限。
我错过了什么吗?为什么
await webSiteManagementClient.AppServicePlans.ListByResourceGroupWithHttpMessagesAsync(resourceGroup);
当 运行 作为已发布的 Azure 函数的一部分时, 没有 return 任何东西?
问题是在应用服务(应用服务计划中的一个应用)上设置了权限。它需要在资源组上设置才能读取应用服务计划。
我正在尝试设置一个 Azure Functions 来扩展我们的应用程序服务计划,使其在工作时间更大,而在工作时间以外更小。我取自
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 Functions 部署到 Azure 时,它找不到任何 App Service Plans。它命中 appservicePlan == null
块和 returns。我已经为已部署的 Azure 函数打开托管服务标识,并在我要为其扩展的应用服务中授予它贡献者权限。
我错过了什么吗?为什么
await webSiteManagementClient.AppServicePlans.ListByResourceGroupWithHttpMessagesAsync(resourceGroup);
当 运行 作为已发布的 Azure 函数的一部分时,没有 return 任何东西?
问题是在应用服务(应用服务计划中的一个应用)上设置了权限。它需要在资源组上设置才能读取应用服务计划。