>> Learn how to keep your Azure subscription clean
in this episode of Azure Tips and Tricks.
Most teams that work in
an Azure subscription forget to delete unused resources.
This makes it difficult to see which resources are still being
used and it makes you pay for resources that you don't use.
Here for instance, I have an
Azure storage account that I don't use.
Let's clean that up with a script.
This is a PowerShell script.
It cleans up unused resources.
The first thing the script does is log
into Azure using a service principle.
We'll see how we can use one of those later on.
Next, I used the Azure Resource graph
to get the resources that should be cleaned up.
Unfortunately, there's no easy way to
determine which resources should be removed,
so we need users to create a tag for those resources.
A tag called expireOn
with a value of the date when it should expire.
Here, I get all resources with a date that is earlier than today.
The script loops over the resources and removes them.
Finally, because Azure Resource graph
doesn't return resource groups,
we get all of those and iterate over them,
and when a resource group is empty, it gets deleted.
Tagging resources is easy.
You can do it in the portal with
the Azure CLI or in an Azure Resource Manager template.
Here, I can add a tag to the storage account,
expireOn and the date should have to format year, month, day.
That's it. To automate the process,
we'll run the script with Azure Automation.
Let's create an Azure Automation account.
There we go. First, I'll give it a name,
and next, I'll select a Resource group,
and I'll leave this.
This creates the service principle that
the script uses to log into Azure.
I'll skip to when it is done.
Here is the Azure Automation account.
The scripts uses PowerShell modules
that aren't installed by default.
So we have to install them first.
Click on "Browse Gallery" and we can pick them from here.
First, I need AZ Accounts.
That is this one here, and import it.
Next, search for AZ Resource graph
that we use to query the resource graph,
import it, and finally,
search for AZ Resources.
That's it. Now, import this as well.
Let's skip to when these are imported.
Now, we'll create a Runbook.
This is the mechanism that we use to
run the PowerShell script in Azure.
First, give it a name
and select PowerShell for the runbook type and create.
This is the runbook.
Now, paste in the script like this and
save the runbook and publish it so that we can use it.
Done. To make it run automatically,
I'll add a schedule to it.
There we go, and create the new schedule.
Also, give this a name.
Now, select a start date and time.
I'll tell it to recur every one day. That's it.
This will run the script every day.
I can also run the runbook to test if it works.
Let's do that. Here it goes.
This can take a while to spin up.
It is done and it ran successfully.
So now, when I look at my Resource group,
the storage account is removed. The script worked.
It is important to remove unused Azure resources to
keep your costs in check and your Azure subscription clear.
You can do it with a script and automate it with Azure Automation.
Go and try it out.