Is it possible to scale Azure app services programmatically
Yes, we can do that use REST API or SDK.
I test the REST API using the fiddler, details please refer to the snapshot, for how to get the authorization, please refer to the document.
Header info:
Body info:
If C# code is possible, please have a try to use
Microsoft.Azure.Management.WebSites to scale Azure app services. More detail info about SDK please refer to the packages.config file.
How to registry Azure AD App and how to get Application ID, secretKey and tenantId please refer to the document. The following is the demo code.
var subscriptionId = "Your subscrption";
var appId = "Registried Azure Application Id";
var secretKey = "Secret Key";
var tenantId = "tenant Id";
var resourceGroup = "resource group name";
var servicePlanName = "service plan name";
var context = new AuthenticationContext("https://login.windows.net/" + tenantId);
ClientCredential clientCredential = new ClientCredential(appId, secretKey);
var tokenResponse = context.AcquireTokenAsync("https://management.azure.com/", clientCredential).Result;
var accessToken = tokenResponse.AccessToken;
TokenCredentials credential = new TokenCredentials(accessToken);
var webSiteManagementClient = new Microsoft.Azure.Management.WebSites.WebSiteManagementClient(credential);
webSiteManagementClient.SubscriptionId = subscriptionId;
var servicePlan = webSiteManagementClient.AppServicePlans.ListByResourceGroupWithHttpMessagesAsync(resourceGroup).Result.Body.Where(x=>x.Name.Equals(servicePlanName)).FirstOrDefault();
//scale up/down
servicePlan.Sku.Family = "P";
servicePlan.Sku.Name = "P1";
servicePlan.Sku.Size = "P1";
servicePlan.Sku.Tier = "Premium";
servicePlan.Sku.Capacity = 2; // scale out: number of instances
var updateResult = webSiteManagementClient.AppServicePlans.CreateOrUpdateWithHttpMessagesAsync(resourceGroup, servicePlanName, servicePlan).Result;
packages.config file:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Azure.Management.Websites" version="1.6.0-preview" targetFramework="net462" />
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.13.8" targetFramework="net462" />
<package id="Microsoft.Rest.ClientRuntime" version="2.3.5" targetFramework="net462" />
<package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.5" targetFramework="net462" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net462" />
</packages>
Check the result from the portal.
Note: If the Azure Service plan is updated, it will apply to all of WebApps in the Service plan.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…