Automate creation of Web App in Azure

In previous post Automate App Service Plan, I showed code snippet for automation of App Service Plan. Now that we have the plan, next step will be to add a Web App service to it. Following code snippet show automation of Web App service using 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.StandardS1)
                .WithOperatingSystem(Microsoft.Azure.Management.AppService.Fluent.OperatingSystem.Windows)
                .CreateAsync()).Result;

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

            try
            {
                var webApp = Task.Run(async () => await azureInstance.WebApps
                    .Define("my-webapp1-qa")
                    .WithExistingWindowsPlan(servicePlan)
                    .WithExistingResourceGroup(rg)
                    .WithHttpsOnly(true)
                    .WithNetFrameworkVersion(NetFrameworkVersion.V4_6)
                    .WithWebAppAlwaysOn(true)
                    .WithWebSocketsEnabled(true)
                    .WithPlatformArchitecture(PlatformArchitecture.X64)
                    .WithManagedPipelineMode(ManagedPipelineMode.Integrated)
                    .CreateAsync()).Result;
                Console.WriteLine
                 ($"Web app {webApp.Name} create for {webApp.NetFrameworkVersion} in region {webApp.Region}");
            }
            catch(Exception ex)
            {
                Console.WriteLine($"{ex}");
            }
            

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

There is one difference between code from previous code. I have changed the App Service Plan from Free Tier to Standard Tier. When you are selecting a tier for any type of Azure resource, it is important that you understand limitation of that tier. You may have some specific requirements for application that you want to deploy. If you choose a wrong tier then either the automation code will fail or you will end up with a resource that will not work for you. You will have to go back and delete that resource and probably App Service Plan as well to recreate resources. E.g. Free tier for Web App does not allow running on 64bit architecture. When I run the above code with Free tier App Service Plan, I will end up with an exception saying that there is a conflict. I had the following exception.

"Code":"Conflict","Message":"There was a conflict. 64 Bit worker processes cannot be used for the site as the plan does not allow it. For more information on pricing and features, please see: https://aka.ms/appservicepricingdetails "

In case of conflict, Azure chose best possible options and created the App Service. But I had to go back and delete it. So, make sure that when you write automation code, you choose right set of options.

Search

Social

Weather

-0.9 °C / 30.4 °F

weather conditions Snow

Monthly Posts

Blog Tags