简体   繁体   English

使用Azure托管服务身份扩展App Service计划,但未列出

[英]Using Azure Managed Service Identities to scale App Service Plan, but none listed

I am attempting to set up an Azure Function that will scale our App Service Plan to be larger during business hours, and smaller outside of them. 我正在尝试设置一个Azure功能,该功能将在工作时间内将我们的App Service计划扩展为更大,而在工作时间之外则较小。 I drew from 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>",                
        };
    }
}

When I run this locally, it works, and actually performs the scale up on our Azure App Service Plan (I believe via Azure CLI). 当我在本地运行时,它可以工作,并且实际上在我们的Azure App Service计划上执行放大(我相信通过Azure CLI)。 However, when this Azure Function is deployed to Azure, it does not find any App Service Plans. 但是,将此Azure功能部署到Azure时,找不到任何应用程序服务计划。 It hits the appservicePlan == null block and returns. 它击中appservicePlan == null块并返回。 I have turned on the Managed Service Identity for the deployed Azure Function, and granted it contributor permissions in the App Service I want to scale for. 我已为已部署的Azure功能打开了托管服务标识,并在要扩展的App Service中为其授予了贡献者权限。

Azure应用服务权限

Am I missing something? 我想念什么吗? Why does 为什么

await webSiteManagementClient.AppServicePlans.ListByResourceGroupWithHttpMessagesAsync(resourceGroup);

not return anything when run as part of a published Azure Function? 作为已发布的Azure函数的一部分运行时不返回任何内容?

The problem was the permission was set on the App Service (which was an app on the App Service Plan). 问题是在App Service(这是App Service Plan上的一个应用)上设置了权限。 It needed to be set on the Resource Group to be able to read the App Service Plans. 需要在资源组上对其进行设置,以便能够读取App服务计划。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM