Day 85: Azure Websites for IT Pros (Part 3)

If you are in the US, Happy Thanksgiving!

Welcome to Day 85 of the “100 Days of DevOps with PowerShell”! For background on our goals in this series, see Announcing the “100 Days of DevOps with PowerShell” Series here at SCC. If this is the first time you are viewing an article of the series and you are new to DevOps and PowerShell Desired State Configuration (DSC), I encourage you to read through the previous installments, particularly Day 1.

IMPORTANT: This post assumes you have already read parts 1 and 2 (“Day 76: Azure Websites for IT Pros (part 1)” and “Day 81: Azure Websites for IT Pros (part 2)”. If you have not worked through these posts already, please return to them and complete that work before returning to this post.

Objectives

In this post, I want to focus on lifecycle management, more specifically, I am going to demonstrate how to automate removal of the Azure website, Azure SQL Server, SQL databases and storage account and container created in the first two installments. I will share a sample PowerShell script that will remove all those resources automatically.

Requirements for Removal

The way the Azure website sample script works, provisioning of the Azure website and storage container are named in a predictable way. However, the name of the Azure SQL Server instance is chosen by Azure at provisioning time, so if you do not have a process for recording what you provisioned, you will need to go look that information up.
While we could write a more complex script that automatically looks up that info, but given that every environment is different, this might not be the best idea. For example, in the real world, you might not deploy a dedicated Azure SQL Server instance and storage account for every deployment. For this example, I am going to keep this simple and focus on deprovisioning the Azure website and SQL resources provisioned in the first two installments on this topic.

Sample Deprovisioning Script

Here is the sample deprovisioning script, which removes the Azure website, SQL Server instance (along with the two databases it hosts, as well as an Azure storage account. You will notice that we do not supply the name of the storage account, because it was simply named the same as the Azure website name, with the word “storage” on the end. The script is a bit simpler than you might imagine it would be, as we are forcibly deleting the Azure SQL Server instance, which removes the SQL databases hosted by the instance as well. The same is true when deleting the Azure storage account; containers go automatically.

NOTE: In testing, I found the name of the storage account when provided to Remove-AzureStorageAccount is case sensitive. Make sure you  pass in the name of the website in same case in which it appears in the Azure Portal.

Function RemoveAzureWebEnv([string]$SiteName, [string]$DBServerName) {
#Delete the Azure Website
Remove-AzureWebsite -Name $SiteName -Force

#Delete the Azure SQL Server and all databases
Remove-AzureSqlDatabaseserver –ServerName $DBServerName -Force

#Delete the Storage Container
$StorageAccount = “$($SiteName)storage”
Remove-AzureStorageAccount -StorageAccountName "$StorageAccount"

}

Save the script as DeprovWebEnv.ps1. In our example here, I will save it to the same directory as the provisioning script.
And here is the syntax for running the script

.\DeprovWebEnv.ps1 –SiteName <site name here> -DBServerName <db server name here>

Now, let us talk for a minute about variations on the original theme. For example, I mentioned you might not want to create a dedicated Azure SQL Server instance for every environment. In that case, you would want to simple delete the SQL databases provisioned in a share Azure SQL Server instance. To do that, you would need to add some logic to your script (a simple function perhaps) you could use to delete the databases, like this

Function RemoveDB([string]$DBServerName, [string]$DBName) {
 Remove-AzureSqlDatabase -ServerName "$DBServerName" -DatabaseName "$DBName"
 }

If you opted for an existing storage account and container rather than one dedicated to each web environment, you would need to do some additional work to remove resources from Azure storage as well. If the frequency of these deployments doesn’t result in a massive number of containers, going the dedicated storage account option makes cleanup easier.

Conclusion

Now you have working script samples you can reuse freely to create new multi-tier web environments in Microsoft Azure, a working sample to automate deployment of content for your developers and now a sample for deprovisioning to ensure you can implement a process keep cloud hosting costs at a minimum where Azure websites are concerned.

Previous Installments

To see the previous installments in this series, visit “100 Days of DevOps with PowerShell”.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.