Start And Stop An Azure Web App On A Schedule Using Azure Automation

One of the most beneficial aspects of the Microsoft Azure cloud service is its Automation capabilities, which means that we can use PowerShell to get very creative and take advantage of paying only for what we use by turning off Azure services when not needed. There are plenty of examples and scripts available on the web on how to use Azure Automation to stop and start Azure Virtual Machines based on defined schedules, but when it comes to accomplishing the same for Azure Web App services, we couldn’t find any example scripts or guides, so here we are doing a lab to show how is done.

This process consists of three general steps, described in detail below

1) Create an Automation Account.
2) Create an Automation  Credential Asset.
3) Create the Runbooks to Stop/Start the Web Application.

Let’s jump into action; the first thing we need to do is to have a properly configured Azure Automation account setup with a credentials asset that will be an authorized user to manage our Azure Subscription.

STEP 1. CREATE AN AUTOMATION ACCOUNT:

  • We sign in to the Azure management portal, scroll-down the left navigation pane and click AUTOMATION, then click CREATE.

Create Account

  • On the ‘Add a New Automation Account’ screen, we specify a name and the region where we will store this account, then click the checkmark button to complete.

Add New Account

STEP 2. CREATE AN AZURE AUTOMATION CREDENTIAL ASSET

 

  • A good practice about this process: we can use any existing Azure Active Directory (AAD) user account on our subscription as long as we make it a co-administrator; or we can create a new one; we recommend creating a new one for the sole purpose of executing automation scripts for the specific service; i.e. our user for the credential asset for this lab is webappautomation@xxxxxx.onmicrosoft.com; we would create a different AAD user account to utilize for automation scripts that apply to Virtual Machines automation; i.e. vmautomation@xxxxx.onmicrosoft.com. This is just good security and identity management practice.
  • First we will create our AAD user that will be used for the credential asset. We click on the left pane of the Azure management portal on ACTIVE DIRECTORY, go to our AAD, then to the USERS tab and ADD USER:

 

2015-08-07_9-31-32

2015-08-07_9-33-19

  • We Copy the temporary password to use it later.

2015-08-07_9-34-22

  • Make the newly created AAD account a co-administrator for your subscription, go to SETTINGS on the Azure management portal left pane, ADMINISTRATORS and ADD:

2015-08-07_9-37-30

  • We will then sign out of the Azure Management portal and sign back in using the new account we just created with the temporary password, we would get prompted to change the password at this point.

 

  • At this point we are ready to create our Credential Asset:

 

To create a new variable with the Azure portal

1. From your automation account, click ASSETS at the top of the window.
2. At the bottom of the window, click ADD SETTING.
3. Click ADD CREDENTIAL.
4. In the CREDENTIAL TYPE dropdown, select POWERSHELL CREDENTIAL.

5. Complete the wizard and click the checkbox to save the new credential.

To create a new credential with the Azure preview portal

1. From your automation account, click the ASSETS part to open the Assets blade.
2. Click the CREDENTIALS part to open the CREDENTIALS blade.
3. Click ADD A CREDENTIAL at the top of the blade.
4. Complete the form and click CREATE to save the new credential.

  • NOTE: In this example we are using a credential asset in the PowerShell script to authenticate to the subscription and execute the management actions (start/stop); but it is also possible to use a certificate asset to authenticate; this method can be found in the link below; but the scripting for this method will be slightly different. Since the introduction of AAD user authentication for automation credential assets, it is easier and far simpler to use an AAD user. For that reason we are not going to review certificate authentication on this post.
    https://azure.microsoft.com/en-us/documentation/articles/automation-certificates/

STEP 3. CREATE THE RUNBOOKS TO STOP/START THE WEB APP:

We go to the AUTOMATION section in the Azure management portal, and click on the Automation Account we created in STEP 1, click on the RUNBOOKS tab and then click IMPORT:

2015-08-07_16-34-21

  • We browse to the PowerShell script that we will execute to stop the website (a .ps1 file, script 1 sample below), in line 3 of the sample script, the item “MyCredential” is the name of the Credential Asset you created in STEP 2. The item “MyAzureSubName” is the name of the Azure Subscription that contains the Web App, and “Webapplab” is the name of the Web App we want to stop.

 

  • SCRIPT 1

 

workflow Stop-FR-Website
{
$cred = Get-AutomationPSCredential -Name “MyCredential”

Add-AzureAccount -Credential $cred

InlineScript {

Select-AzureSubscription -SubscriptionName “MyAzureSubName”

Stop-AzureWebsite -Name “webapplab”
   }
}

  • If we need to edit the script after uploading it, we can edit from the RUNBOOK section, we open the recently uploaded script, and click on AUTHOR, this will allow us to edit the script and save it, as well as test it before setting a schedule to run by clicking on the TEST button at the bottom of the screen.

2015-08-07_10-38-07

2015-08-07_10-39-53

  • After testing we will need to publish our script in order to be able to schedule a job to run as a Runbook.

2015-08-07_10-42-01

  • We are almost done, now is time to create our scheduled jobs to execute the runbook, we go to the runbook we created to stop the website, click on it, and then click on SCHEDULE.

2015-08-07_10-42-42

  • We select “Link to a New Schedule”, create a name for the schedule, and click to the next screen.

2015-08-07_10-53-08

  • We configure our schedule to run one time, hourly or daily. Select the start date and the time, the time uses a 24-hour format and is not adjusted for daylight savings time. Set a start time that is at least 5 minutes after the time we create for this schedule, we can then utilize the “recur every number of days” field to run weekly, monthly, etc.

2015-08-07_10-56-12

  • Follow the same steps above to create a Runbook to Start the website, publish, and schedule it. Here is the sample used in this lab:

SCRIPT 2

workflow Start-FR-Website
{
$cred = Get-AutomationPSCredential -Name “MyCredential”

Add-AzureAccount -Credential $cred

InlineScript {

Select-AzureSubscription -SubscriptionName “MyAzureSubName”

Start-AzureWebsite -Name “webapplab”
   }
}

Sit back and watch your Web App stop and start automatically. We welcome comments and suggestions to improve and/or maintain this content. Thanks.

2 Comments

  1. Ben McKean April 6, 2016 Reply
  2. Steven Smith May 8, 2016 Reply

Leave a Reply