Utilizing PowerShell to create and modify an Azure App Service plan is as simple as using a cmdlet. Learn how to do this command as well as other options for using PowerShell to interact with the Azure App Service.
- [Instructor] Up until now, we have been creating and configuring our apps using the portal. Now, let's go ahead and take a look at how we do some basic configuration using PowerShell. As you can see, I have already logged into the PowerShell Integrated Scripting Environment, or ISE, and I have logged in in administrative mode. Next, I'm going to go ahead and open a script that I already have prepared. You will find those script in the exercise file folder under CH02_03.
And the script is called AzureRMWebAppCmdlets. The first thing I'll need to do is actually login to Azure. To do so, I am going to use the command login -azurermaccount. And I have logged in, but I can see I'm not working in the correct subscription. I like this deployment to be in my subscription, pay-as-you-go.
To do so, I'm going to use the Azure command or commandlet, you'll hear me use those two words interchangeably, Select-AzureRMSubscription, my subscription name. And my subscription name happens to be pay-as-you-go. Yours will probably be different. I can now see I'm in my pay-as-you-go subscription. I'm just using the command clear. When I create scripts, I like to use variables. It just saves me from typing the same thing over and over and over again.
Let's step through the variables that I have created. First of all I have one for RG which equals my resource group. And my resource group will be called MyCompanyRG. The Location will be in the East US. My App Service Plan is using the variable AppSP. And the name I have associated with that happens to be MyCompanyAppSP. I know, not very creative. And that lack of creativity carries forward through my two web apps. I have WebApp and WebApp2 and of course for any names of my company app SB and SB2.
Let's go ahead and set up these variables. I'm going to scroll down a little bit here. The first thing I need to do is create my resource group. And to do so I'm using New-AsureRMResourceGroup, providing the name and this is where my variable of RG will be passed which we can see is MyCompanyRG. And our location information's also being passed from that variable being East US.
Let's go ahead and run that. And we can see we now have a new Resource Group with the name MyCompanyRG in the East US. Next we're going to go ahead and create a service plan. To do so we will be using New-AzuerRMAppServicePlan. We'll be providing a name. Again, I am passing the variable information here from AppSP as well as the location, the East US, and the Resource Group, name of RG. Again, you can see why I like using variables, otherwise I'd have to type these over and over and over again.
Next I'm selecting my tier. In this case it'll be premium. My worker size will be large, so it's the size of the tier. And then the number of instances in this case is 10. I'm going to go ahead and run the selection. And this will take a minute for this to be created. After about 30 seconds, the service plan was created, we can see we have the service plan name of MyCompanyAppSP.
We can see the maximum number of workers. Now we input 10 but it actually states 20 here. Remember that premiums queue, will allow for up to 20 workers, even though we've only defined 10 to be running. We can see it falls in the East US. If I wanted to see this information again, I could go ahead and use a Get-AzureRMAppServicePlan. And then here you just need to provide the service plan name again using that variable.
And this will just bring up the same information one more time. Next let's go ahead and change that service plan. To change it we'll use the command Set-AzureRMAppServicePlan, again providing the name, and the resource group. But we're going to change the tier from a premium to standard. So it'll be medium sized standard tier. And we're going to reduce the number of instances to nine. I'm going to go ahead and run this line.
And this will take about 30 seconds. And we see that our tier has now been changed. We have now maximum number of workers from 20 down to 10. Again, only nine will be used. I will show you all this in the Azure portal in a moment as well. If I wanted to view the details of this service plan again, I would use the command Get-AzuerRMAppServicePlan, again providing the name. As that information is already here, I'm going to skip that. Next I'm going to go ahead and create our two web apps.
To create a web app, I'm going to use the command or commandlet, New-AzureRMWebApp, I'm providing the name again I'm passing the variable here, providing the AppServicePlan name, again using our variable. The resource group and the location. Again, using all the variables that I've already previously defined. If I scroll up a little, we can see we have our site name.
We can see that state is running. My host name. And my resource group. Let's go ahead and create a second web app. Again using the same commands. The only difference I'm using here is my name will be pulled from my WebApp2 variable. If I scroll up a little bit we can see that our second web app has been created. If we want to pull up all the information about the web apps within that resource group we can use the command Get-AzureWebApp and in this case I'm going to specify the resource group.
Again, using a variable. This will take a moment for all that information to be pulled up. There we go. I'm going to scroll up but here we can see all the details about each of those apps. And finally if I wanted to restart an Azure Web App, I could do using the Restart-AzureRMWebApp, specifying the app that you'd like to restart and the resource group name. In this case, I'm actually going to change this and this is one of the reasons I really like ISE is you can change this on the fly.
I'm actually going to stop this app. So I'm going to go ahead and run this line. And now we can see that that app is now in a stopped state. Now let's take a look at this in Azure and see what has been created for us. I'm currently in Azure. I'm looking at the resource groups.
But I can see that new resource group isn't here yet. Therefore I'm going to refresh. We have MyCompanyRG. We can see that we have our Service Plan and our two web apps. And if I take a look at MyCompanyAppSB, this should be in the stopped state. It is. And I love the warning here. Even though our app is stopped, our service plan charges still apply.
Keep that in mind when you're stopping your apps. It does not stop you from being charged. If I wanted to go ahead and restart that app, I'd just go ahead and click start. And that's all there is to it. You'll find using PowerShell and scripts a lot faster to deploy your apps as well as other services within Azure. If you haven't had the chance to start playing in PowerShell, there are a lot of courses within the library that you can use to build your PowerShell skills.
- Deploying Web Apps
- Creating App Service plans
- Deploying and moving apps
- Configuring app settings
- Managing Web Apps using PowerShell and CLI
- Configuring alerts
- Enabling diagnostic logs
- Retrieving and streaming logs
- Configuring Web Apps for scale and resilience