Bonjour hi welcome to this episode of cloud in five minutes I'm Frank bouche a
Microsoft cloud advocate and today I want to show you how to use the azure
functions to clean up your subscription by deleting there's resources that you
don't need anymore
we must admit we all did it at least once
forget to delete some resources that we don't really reuse or need anymore
in a previous video I show you how to use the azure automation to run a script
every morning and delete those resources there's no built-in way in Azure to know
when a resources is expired that's why I use that tag expired on with a specific
date to decide it when the resources expired in today's demo I want to use
Azure functions to run every morning or whatever the schedule you want and
delete those resources ready let's get started so I'm in Visual Studio in
today's demo I will use the extension for functions I will put the link in the
description or you can search it directly from the Avs code so to create
a new function you open the extension just here and you will click on the
first icon create a new project well create a new folder let's call it a Z
sub cleaner and we'll select this folder for today's demo we'll do it in
PowerShell and I want a time trigger so it's the last option so we need to give
it a name so I will call it a Z sub cleaner time trigger and now we need to
specify the schedule this used the cron expression I will put link in the
description so you could learn a little bit about it but if you want it to run
just like me every morning at 5:00 a.m. then it's 0:05 and star star stars let's
open this new solution inside the current window now it will take a few
seconds to create the file that we need great it's done so let's close this
terminal we don't need it in WoW we'll do is add the code so I copy the code
from the previous script let me just explain quickly what it does so I'm
using the azured resource graph to where we are the resources where the tag
expired on where the date is older than now and then I returned all the project
ID this way instead of retrieving all the ID the resources and looping through
them I only receive the resource that interests me it's way more efficient
this way and then I loop through to delete those resources
unfortunately resource graph doesn't support resource group so I need to do
it the old way and retrieve other groups and then looping through them checking
if they are empty and finally deleting them I used the force here so I don't
have I don't get any prompt and my script were will run without an error
before I can deploy in Azure my script use here the search a Z graph and for
that I need a module there's a new way very simple to add module inside an
azure function so first from the root will create a new so not in the time
trigger but at the root we'll add a new folder and we'll call it modules now we
need to copy the resource graph module inside that folder if you already have
it locally it's very simple so what we will do now is open the terminal voila
let's just expand it so you see more a better and now we'll execute the common
get module list available AZ resource graph and this will give us the location
of the module we'll go there and copy everything inside our folder if you
don't have the module install you call already install it with install module
AZ dot resource graph and to get all the information out to do it go at bitly /
AZ graph the link will also be in the description in fact all links are always
in the description so now for me let's copy this thing will open the folder and
let's go to this location now I need the AZ resource graph so that's the one I'm
looking for so I will copy it and paste it inside the folder I just create there
it is so now I have it and if I expand in visual zoo code now everything is
there perfect now I have all I need in my code so I
could deploy in Azure with the visual studio extension is very easy we go here
and we'll upload so that's the last one that upload but deploy define the
function so go there you pick your subscription so Frank power so for me
and then we'll create a new one let's call it a Z sub cleaner well you pick
the location close to you I'm in Canada in Montreal so ecos is perfect for me
and I think now it's the artists time you just need to wait but you're lucky I
will speed that up for you ok the deployment is ready but we're not there
yet we need to do a little bit more thing so see our code right now and
let's close the terminal so the code right now request access to the
subscription to first read them and then delete them so we need to give it
permission if we check in the code if we go in the where is it
profile in partial there's a profile there there's a way to pass an identity
so we need to do that and also give the identity permission to access or
subscription this is really easy to do let me show you so we'll go in the
portal so let's go in the function app here it is a Z sub cleaner so we'll go
here and now in platform feature I will click on that and I have a property just
here named identity and I'll click on this Wow
by default it's turned off so let's start it on and save and yes this will
create a principle that we will be able to use inside a sure to give access to a
subscription I just saw the notification everything is ready
so now we need to go in the subscription I have it here in my favorite but if
it's not the case you can always find your subscription by going in all
services and typing subscription should be very quick to found you click there
you will go in your subscription and now the third option from the top is the
access control I am you click on this and now what we want to do is add a
permission or a role assignment so you click on the add role assignment to be
able to do what we want the easiest will be contribute if you want to be a bit
more specific you can't but I will let you decide what you want to do in this
sample I will use contributor and then we need to search for the specific
principle we have so the principle was called in our case a Z sub cleaner see
this is the logo of web app because that's my web app that is getting that
principle so we click on this and then we say safe perfect now it's done if I
want a double check I can go here find my function here it is and I will see
that I have the role contributor there it is
you are all set ready to go now the only thing missing is some resources tag so
you could delete them you should keep the abbot of adding those tag by default
in your own template or when you're creating new resources this way it will
be more way more efficient let me show you how to create atom 1 tag using the
portal so let's go in in the resource group let's say the dog
demo I don't need that anymore so I will use the storage and then any resources
what you could you always find is here always on the top is tag so you click on
this and now what you need to do is just add expired on and honestly it depends
how you wrote your code I said it has expired on because it makes sense to me
if you prefer something else just add the tags you like and adjust
the code in consequences there is no problem in there and then for the value
use a date using the format here month day and the here must be four-digit it's
ready now don't forget to save and voila tomorrow morning this storage will get
deleted now if you're interested to have this solution it's available in my
github I will put all the link in the description I hope this video was useful
and I hope you liked it if you have any questions don't hesitate to ask them in
the comment or reach me on any social media as usual like subscribe and share
it's always very appreciated thank you for watching
Abhaya