How to automate creation of App Service Plan in Azure using Azure SDK

Usually you may not be automating creation of App Service Plan in your azure infrastructure. Usually App Service Plan is something you create one time with required type of operating system and pricing tier. But if you are doing a true Infrastructure As Code deployment, then following code will help you in creating an App Service Plan using C# with Azure Sdk.

static void Main(string[] args)
        {
            Console.WriteLine("Cloud Robot - Start");

            var prinicipalCredentials = SdkContext.AzureCredentialsFactory.FromFile("./azureauth.info");
            var azureInstance = Azure.Configure()
                .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
                .Authenticate(prinicipalCredentials)
                .WithDefaultSubscription();
            var rgName = "my-rg";
            var rgLocation = "eastus";

            var rg = azureInstance.ResourceGroups.Define(rgName).WithRegion(rgLocation).Create();
            Console.WriteLine($"Resource group {rg.Name} created in region {rg.RegionName}");

            var appServicePlan = "my-webtest1-plan";
            var servicePlan = Task.Run(async () => await azureInstance.AppServices.AppServicePlans
                .Define(appServicePlan)
                .WithRegion(rgLocation)
                .WithExistingResourceGroup(rgName)
                .WithPricingTier(PricingTier.FreeF1)
                .WithOperatingSystem(Microsoft.Azure.Management.AppService.Fluent.OperatingSystem.Windows)
                .CreateAsync()).Result;

            Console.WriteLine($"App Service Plan {servicePlan.Name} created in {servicePlan.Region}");

            Console.WriteLine("Cloud Robot - End");
        }

In your code, you will have an external configuration file that provides settings like resource group name, plan name etc. Just for demonstration purpose, I have hard coded the names in code itself.

Search

Social

Weather

24.4 °C / 75.8 °F

weather conditions Rain

Monthly Posts

Blog Tags