hello everyone in this video I'm gonna show you how to use a event grid and Azure
automation to automate purging data from an Azure CDN
hello everyone I'm
Travis and this is Ciraltos in this video I'm gonna walk you through setting
up a workflow in azure using Event Grid and azure automation for the example
I'm gonna run an automation to purge content from an azure CDN cache every
time new content is uploaded or deleted from the source this video has two
takeaways first if you want to quickly purge outdated content from Azure CDN
this video is going to show you how to do that second if you want to see
how to integrate event grid and azure automation for workflow automation keep
watching the interesting thing about event grid is it works with multiple as
your services the automation shown in this video can be applied to those
services as well before we move on please take a second to subscribe it's a
simple process and it's 100% painless in my previous video I walk through setting
up an azure CDN I'll post a link to that someplace up here this demo picks
up where the last one left off once content is cache across the globe it can
take time for newly published content to replace the old to speed up the process
we use the purge feature to clear files from the cache so new content can
replace it this example is going to show you how to automate that process with
event grid and azure automation here's the flow overview for the automation
content is uploaded to remove from the content source in this case blob storage
the upload or delete event will trigger event grid event grid fires a web hook -
to an azure automation runbook passing details about the event in JSON format
the runbook parses the JSON and runs the purge operation against the file that
was added or removed if you're not familiar with is your automation I have
a whole playlist of setting up and using it link above enough chitchat let's move
on to the demo buckle up everyone it's demo time so in this demo I'm gonna
cover creating a run book and a web hook then I'm going to configure event grid
on the storage account to use that web hook after that I'm going to build the run
book to actually purge the content and then we're gonna do some testing so with
that let's move on the first step in this process is going to build their
actual run book and I need to do that first because I need the web hook for
the event grid so I'm gonna build this run book with some pretty simple content
in it for now then come back later and actually build out the full run book so
I'm gonna go to run books I'm gonna create let's call this one CDN purge and
for the run book type is gonna be PowerShell and I'll click create I'm
gonna edit that and we're gonna call just do a write output and again this is
just a placeholder for the actual content I'm gonna put in but I need to
gather some information first before I can build that out so I'm gonna click
Save and we'll publish and then I'm going to add a web hook so I need the
web hook in place before I can set up the event grid trigger because the web
hook is the actual action so let's go in here and we'll call the CDN purge web
hook it's gonna be enabled and I'll set this for a year out remember that this
will expire on the date and it'll quit working now I've got some stuff blocked
out on my screen but here is the URL so this URL is important once we get done
craving this web hook we won't be able to go in and retrieve this value so I
don't care that you really see that one I'll just paste that into notepad save
that for later and I'm going to click OK I'm going in to modify run settings and
if there were any parameters for this run book they would be here and we're
gonna go to run settings if I wanted this to run and like a hybrid worker I
could set that here but that's not the case so simply click OK and create
that's going to create the web hook so now that webhook that i just created
and copied will trigger this run book next I'm gonna go over to my storage
account let's just go back this is the CDN I set up in the last my previous
video I'm gonna go into the storage account and we're gonna go to events you
know I have nothing configured so I'm gonna go here there's azure logic apps and
functions and more options no more options as webhook and that's what we
want so I'm going to call the CDN purge
we'll call it CDN purge event I'm gonna keep the schema here you can see that
the topic type is a storage account or the resource is the storage account and
under event types there's two options there's blob create and blob delete I'm
gonna leave these are the same but you could change them if you needed now I'm
gonna go to the endpoint type and select web hook next I'm going to select an
endpoint and here's where I'm gonna paste in that URL so what this is saying
is that if any new content is added or deleted it's going to trigger this web
hook and I'll click create now if I go back up and look at event subscriptions
and refresh here you can see this CDN purge event you can also see a couple
other ones that I have here from testing and you can ignore those for now so what
I want to do is test this again to make sure that it's working as I expect I
also need some JSON data that's passed into the run book so let's do that let's
first test by going over to storage Explorer and I'm going to copy just
gonna cop simply copy the index.html in and there it goes
so that just ran a copy which is gonna create a new file on the storage account
I'm going here it's gonna take it a couple seconds to show up but I'm gonna
come back to my run book and if i refresh the run book you can see job is
queued so what happened is I uploaded content into that storage account that
triggered the event in event grid which then kicked off the webhook which ran
this run book and we can go in now that it's complete and if we look at output
it worked as it says here so that webhook triggered this run book now what
I'm really interested in is the webhook data I click here I can copy and paste
all this information into a JSON file we can take a peek at it and I like to
format just that's a little bit easier to read so here's the data that event
grid is passing into the run book and under request body we scroll all the way
over here towards the end you can see a URL now when I run the purge operation I
need to tell what filed a purge what this is telling me is source slash index
dot HTML is the file that I want to purge so that's the value of URL so what
I'm gonna do is grab this from the JSON strip off the fully qualified domain
name and that will leave the path that I need to run the purge operation let's
look at the actual run book that I'm gonna use to do that so if I start here
you can see I've got my purge CDN run book I've got some notes up on top I do
have this available on my github page I'll post a link below you can go in and
download it and take a look at it as you like so the first thing I'm gonna do is
grab the web hook data the JSON data that's passed in with a parameter the
next thing I'm going to do in this run book is authenticate because this run
book has to authenticate to azure in order to have permission to run the
purge operation and if you're not familiar with there's your automation
and run books I have a whole playlist on it I'll put a link up top you can go
ahead and go back and look those over and should get you up to speed on as
your automation and run books the next thing I'm going to do is assign some
values to variables so I've got the profile name the resource group and the
endpoint name all of the CDN I also assign a value of the error action
preference to a variable and I use that later for error correction the next
thing I have here is I'm going to come the request body information from that
webhook data and assign that to a variable of request body and I've got
try-catch blocks in here so if anything goes wrong the catch block is gonna
capture the error message and write that to the error stream if you're interested
in learning more about error handling and try-catch blocks there's a great
ebook on powershell org on how to do that okay next is the convert the
request body to a file path here you can see is request body dot data URL so
that's the URL that I showed you before from the JSON and I've got a simple
replace command that's going to take the HTTPS and then the fully qualified domain
name and replace that with nothing so it's basically just stripping it out
and if you're doing this at home you would have to update this string with
your own information that's going to assign the actual file path in a file
name to the file path variable and at the end is the actual command the
unpublished-AZCDNendpointcontent that's going to actually run the purge
command so with that I'm gonna copy all this and just paste it into that run
book there are other ways to update run books but this is going to be the way I
do it today
I'll save and publish that one other note because I just thought of it I'm
using the unpublish az what is it unpublish-AZCDNendpointcontent
command which is an AZ command so it's the new as your module if you create a
new as your automation account it's probably going to still have the Azure
RM so if you have not updated your azure automation account to the AZ commands
you will need to use the change that in the script to azureRM instead of AZ
and also if we come back to if we come back to the automation account and go to
modules you can see I have the AZ.CDN module that does need to be imported
for this to work so again if you're using the AZ set in as your automation
you want to import the AZ.CDM if you're still using azureRM you'll
want to import azureRM.CDN you can do that through the module gallery you can
see it has both of them listed here and all you have to do to import them is
click on it and important ok so let's get back to where we were before so I'm
in the run books I have CDN purge published and now I want to give it a
try and see if it'll work I'm going to go back into the storage and again I'll
upload that file it's going to run a replace and there it's complete so now
if I come back let's just see if it shows
the first one we ran let's come back into my run books I'm gonna go into CD
and purge the run book that we just created and updated and here you can see
the job is queued so that tells me that event grid saw that content was uploaded
and it triggered the webhook to run now it's running this is gonna take a couple
minutes to run so I'm just gonna pause here and I'll get back once it's done so
that job is complete let's go in and take a look we can see it passed in the
JSON data there's no output for this job and under errors we have none and under
all logs there's nothing so let's go back to our storage account you can see
there is the one that just ran in resource groups I'm gonna go to CDN
endpoint and activity log here we can see a purge operation was ran it was
accepted four minutes ago started four minutes ago and succeeded two minutes
ago so this tells me that the purge operation was successful and we have a
working automation now so that every time content is uploaded to or deleted
from the CDN the purge operation is gonna run so that does it for the demo
thanks everyone for watching I hope you found this useful and informative don't
forget to subscribe and thanks for watching