Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I automatically start and stop an azure website on a schedule?

Even asking this question, I'm wondering how something so simple can be so difficult for me. All I want to do is automate the stopping and starting of an Azure WebSite on a schedule. At first, I looked at WebJobs, and created a powershell script that would stop the WebSite that used the stop-azurewebsite commandlet:

stop-azurewebsite [my site]

I then created a .cmd file to call it using powershell.exe to execute the powershell file

PowerShell.exe -ExecutionPolicy RemoteSigned -File stopit.ps1

I created a WebJob to run the powershell command to stop the site, but it errored with a message saying:

stop-azurewebsite : The term 'stop-azurewebsite' is not recognized as the name 
of a cmdlet, function, script file, or operable program. Check the spelling of 
the name, or if a path was included, verify that the path is correct and try 
again.

So, I figured I'd go the REST API route. I created a post request using fiddler and the proper management certificate to make a call to:

https://management.core.windows.net/[my subscription id]/services/WebSpaces/[webspace]/sites/[website name]/stop

Turns out, there is no 'stop' command. There's 'restart', but that's obviously of no use in this situation. So, all that said, what is a reasonable way to automate the stopping and subsequent (but much later) starting of an Azure WebSite on a specific time schedule?

UPDATE:

I've figured out how to issue the http request to stop the site using a PUT to

https://management.core.windows.net/[my subscription id]/services/WebSpaces/[webspace]/sites/[website name] with a body of {"State":"Stopped"}, but I still don't have an obvious way of 'PUT`ing' to this URL in a WebJob.

like image 731
sonicblis Avatar asked Aug 03 '14 00:08

sonicblis


1 Answers

Use Azure Automation, create a runbook and add the code below to get all 'Running' websites/webapps and shut them down.

# Get Websites/webapps
$websites = Get-AzureWebsite | where-object -FilterScript{$_.state -eq 'Running' }

# Stop each website
foreach -parallel ($website In $websites)
{
    $result = Stop-AzureWebsite $website.Name
    if($result)
    {
        Write-Output "- $($website.Name) did not shutdown successfully"
    }
    else
    {
        Write-Output "+ $($website.Name) shutdown successfully"
    }
}

Publish the runbook and you should be able to schedule it straight in Azure Portal. Make sure your automated user is authenticated and your subscription selected in the runback and there should be no problems.

Similarly for starting up all websites/webapps just change 'Running' to 'Stopped' and 'Stop-AzureWebsite' to 'Start-AzureWebsite'.

like image 74
Jordan Cassem Avatar answered Sep 30 '22 03:09

Jordan Cassem