Azure Auto Cleanup Resources
Problem
Developing for the cloud has opened opportunities that once required a ton of infrastructure. Microsoft’s Azure is an incredible resource to use for software project development and experimentation; however, it can become rather expensive rather quickly. There is a free account that can be used to play around with and develop on for a year. However, there is a limit to the available credit that can be exhausted rather quickly. So there is a need to minimize the cost as much as possible. Microsoft’s Azure is an incredible resource to use for software project development and experimentation; however, it can become rather expensive rather quickly. This is a way to help control costs by cleaning up created resources every night. The only caveat is that the platform and/or infrastructure have to be created every day. A good practice is to use code to create the platform and/or infrastructure as a matter of practice.
Solution
This is a way to help control costs by cleaning up created resources every night. The only caveat is that the platform and/or infrastructure have to be created every day. A good practice is to use code to create the platform and/or infrastructure as a matter of practice.
The cleanup process works by putting all resources in Resource Groups, which is the recommended way to create resources in Azure. All Resource Groups and resources will be deleted nightly except the one that will contain the cleaning resources. The nightly cleaning is done by Azure Automation and a PowerShell script. The first thing to do is create the Azure Automation, the steps are shown below.
-
Login to your Azure account
-
Create the Azure Automation Resource Group, I called mine AutoJobsGroup
-
Add the Automation resource to the newly created Resource Group, it can be found by clicking + Add and searching for Automation. Once it is created it will contain samples that can be deleted or kept.
-
In the Automation Account that was just created click on Runbooks. Click Create a runbook, name it something relevant, I called mine “CleanUp”, and select PowerShell as the Runbook type. Then click Create.
-
Once the Runbook is created click on Edit and the code below can be pasted in to the text entry area. Add the name of the newly created Automation Resource Group to the
$keep
variable’s list. -
Click Save and then Publish. Voilà, automation is all set. It just needs to be scheduled.
$connectionName = "AzureRunAsConnection" try { # Get the connection "AzureRunAsConnection " $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName "Logging in to Azure..." Add-AzureRmAccount ` -ServicePrincipal ` -TenantId $servicePrincipalConnection.TenantId ` -ApplicationId $servicePrincipalConnection.ApplicationId ` -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint } catch { if (!$servicePrincipalConnection) { $ErrorMessage = "Connection $connectionName not found." throw $ErrorMessage } else{ Write-Error -Message $_.Exception throw $_.Exception } } # Auto jobs Resource Group $keep = "AutoJobsGroup", "ResourceGroupKeep1", "ResourceGroupKeep2" $ResourceGroups = Get-AzureRmResourceGroup ForEach ($ResourceGroup in $ResourceGroups) { if ($ResourceGroup.ResourceGroupName -notin $keep){ Remove-AzureRmResourceGroup -Name $ResourceGroup.ResourceGroupName -Force } Write-Output ($ResourceGroup.ResourceName + " deleted ") }
Scheduling the automation to run is easy.
- Click Schedules in the Automation Account
- Click Add a schedule and add the time
- Set Recurrence to recurring and set the frequency
The one I use runs daily at 10:45pm. The clean up should be all set and developing can happen without the worry of leaving expensive resources running and running up the bill.
Happy coding!
Why
Automation is an easy way to give peace of mind and not worry if the free credits will be exhausted to quickly or run up the bill on simple proof of concepts.