Bonjour hi welcome to this new video of cloud and five minutes I'm Frank Busch a
Microsoft cloud advocate based in Canada and today I want to show you a simple
way to clean up your subscription automatically
it happened to all of us you create some resources and then you forget about it
at the end of the month you receive your invoice and whoops a big surprise it's
way more expensive than you thought so in today's demo what I want to do is
show you a simple way to delete automatically expired resources I say
expired with air-quote because there's no built-in way to say expired on a
resource the way I will be using is adding a tag expired on and putting a
date as a value then we'll run a query against that thing and delete all the
resources let me show you how I did it so this is a PowerShell script well I
use PowerShell we'll explain in a few seconds
why so here I'm running the azor graph query and arriving the query to return
all resources that I have a tag expire on you know older then now the to date
time is one of the operator or function available in the query for Azure graph
then I will return all the project ID because that's only what I need the
resource ID to be able to delete a resource then I will loop through all
those that lists and one by one I will just say a remove a Z resources passing
the resource ID I will force it to be sure that I have
no prompt that it's working and right now because I'm in the moment I don't
want to be at deleting anything by mistakes around the what if of course in
production you will remove the what if parameter what if will execute the
comment but do nothing to know a little bit more about as your graph you could
watch my video I did previously search like a bus or go to bitly / AZ graph
there you have a lot of documentation to get you started with your first query so
that's perfect and right now I'm going at a resource level so if you have a
if you have tagged resource group they won't show up in that query because
unfortunately our graph doesn't manage or doesn't see the the resource group
good for us because this way if you want to delete all the resources inside a
resource group except let's say a storage to keep backups or things like
that then it will be possible unfortunately for us if you delete all
the resources inside a resource group then you're left with an empty resource
group so we need a little bit more to gymnastics or you know a little bit more
code to delete all those so the way to do it is to ask for all the resource
group and then we'll loop through them check if there's one or more child in it
and if it's empty then we'll delete it say again remove AG
resource group passing the name force as a parameter so we don't we avoid pop-ups
and then I'm still running the what if again if you're in production if you
really want that tool to be working you will need to remove the what if so
that's great so this is the code we wanna run every let's say every morning
five o'clock in the morning you want to run and clean our resources in Azure
there's many way to execute code based on a schedule you can use function logic
app and automation today I will show you automation and we'll execute this
PowerShell script oh talking about that the reason why I picked our shell is
just natively PowerShell will be able to manage the Jason result returned by our
graph query that's why otherwise I needed to import more module to
manipulate the answer that's it now let's go in a Azure portal to create our
automation to create the automation you will click the big plus button here and
you search for automation there it is so we will create one give it a name in
my subscription I will select my cloud 5-minutes group perfect is us an
important note here is to create this create as your run as an account it will
give us the permission to execute code and read through the
subscription because we need the code to access the subscription so this is
really important so we created it the Diploma must succeed so let's go to the
resource so now to put my code somewhere I need to create a run book so the run
book will be available here from the Left panel
I will just click here the run book and it will create a new one so create run
book okay I'll give it a name so subscription cleaner or something like
that subs the type is important in our case we need PowerShell but you could
run as you can see peyten graphical like it's different flow and things like that
for us right now it's PowerShell then give it a
subscription a description it's important you'll thank me later so now
we're here we're in this it edits mode we just need to paste the code that we
had previously so here it is and here I put a little part to use that run
account to so to connect with the identity and tenon and everything so our
code will be able to access and then it's the code I show you previously just
here perfect so now we save it and we'll publish it so now the run book is
created we need to add a schedule so it runs every morning great so you could
link to a schedule just here and then you click link to a schedule I have no
schedule let's create one so give it a name and I will start right tomorrow and
I will start at 5:00 you know what 503 just to whoops in the morning I said
morning so a.m. perfect East I'm canto and I want it
recurring every one day so this is my schedule it will be running every
morning at 503 a.m. Eastern Time Eastern Time Toronto every day perfect
create so now it's created I can associate it and that's it it's done it
will be running every morning and keeping my subscription clean all the
code will be available in my github all the link are in the description
but it's get up /f boucher slash easy subscription cleaner I also have a blog
post where I describe exactly what I did I did show you if you prefer reading it
in the Dadaab project I'm building right now deploy to Azure button so in one
click you could have it deploy in your subscription very simply I'm also
working in a version using Azure functions I hope this video was useful
and I hope you liked it if you have questions don't is they put in the
comment and like usual like subscribe and share
it's always very appreciated see you next time bye