in this video I'm gonna show you how to use Azure Automation and event grid to
send a message to Microsoft teams when a new resource group is created hello
everyone my name is Travis and this is Ciraltos and that was a long title did
you know that Microsoft teams will accept messages by webhook I didn't
until recently in this video I use event grid and Azure automation to send a
message to a team's channel every time a resource group is created in Azure I
found an example of code on Microsoft Docs that includes setting tags and
sending a webhook when a new resource is created this video uses that code as the
base but I removed references to Azure tags I'll include a link below the idea
of this is to track when new items are added to a subscription I decided to use
a resource group as that's probably good indication of additions to a
subscription that is limited I know a new resource could be added to an
existing resource group and the event grid would not trigger but with some
modifications this example could alert a new server creation new sequel database
creation or any other resource creation before we go any further please take a
second to subscribe and click the bell icon to get notifications of new content
thanks this video assumes some knowledge of Azure automation you'll also need
an azure automation account and a Microsoft team's account if you want to
follow along if you're new to add your automation check out my playlist to get
started I'll add the link above once finished the flow will look like this
event grid will send a web hook when a new resource is created the event grid
webhook starts the Azure automation run book that formats the message with
specifics like the resource group name and who created it Azure automation
then sends a web hook to teams with the body of the message if all goes as
planned teams will display the message here's
the steps we're gonna take to get this set up all code is available on my
github page I'll add a link to that in the description below we're gonna create
the run book and then we're gonna create the run book webhook we have to do that
first because you cannot create a web hook to a run book until that run book
is published next we're going to create an event grade subscription then we're
going to trigger an event so we can view the data
coming into the run book once we have that data we're gonna create a filter in
the event grid so we only get notifications of new resource groups and
then we're gonna create the team's webhook and configure that to send the
message to teams once that's all done we'll test it buckle up everyone it's
demo time so let me walk through what I've got here what we're gonna do I have
some information on this page this is the template for the team's run book and
I need some information I need my Azure automation run book
webhook and in order to get that I first have to create a run book I also need a
resource group name and a subscription ID that information will be passed into
the run book by the event grid webhook I'm gonna pull that webhook data and
identify a way of pulling that information out of the web hook I also
need a username that's gonna be the user name of the user who created the
resource group and I'll also need a channel URL for the team's webhook so to
get started the first thing I need to do is get that webhook for the run book but
before I can do that I have to create a run book so let's do that right now Here
I am in Asscher I'm gonna go to my automation accounts pick the account I'm
using for the demo and go to run books and I'm gonna create a new run book I'll
name this team's demo one and the run book type will be PowerShell and I'll
create it I'll move this over a little bit so we can see better I'm gonna add
some code what I add is really not important at this point what's important
is that we're creating a run book and we're gonna publish it that way we can
create a web hook to interact with this run book as again all I care about right
now is what's going into the run book not what's going out so in order to get
that web hook I need a publish run book so I'm gonna save and publish there it
is now I can get the web hook so I'll create a new web hook I'll call this
team's web hook a1 make sure it's enabled it expires in a year I'll leave
that as it is I'm going to copy the URL to the clipboard remember to copy and
paste this someplace safe consider it a security item and you
cannot get access to this URL after we close out I'm going to go back into vs
code and I'll pay right here under Azure automation
runbook webhook seems like a good spot for it okay that's great now I have the
web hook that I'm gonna use for a vent grid so let's go in and set up a vent
grid oh you know what I copied that to the clipboard but I didn't save this is
important I have to click okay I'm gonna come back to configure parameters and
there's no parameter so I'll click OK and then crate if I miss that step this
would not work as well as I would want it to okay so now I've created the web
hook and now I can go and create the event grid subscription so I'm going to
go to all services and events and grid subscriptions I'm going to add a
subscription we'll call this teams demo event oh one
I'll leave the schema to event grade schema under topics I'm gonna change the
topic to Azure subscription and the subscription will be my pay-as-you-go
you can select whatever subscription you want this to apply to under event types
I only want the right resource right success so I can uncheck all the rest
let me explain what that does anytime a resource is created or modified so
there's a right action against it and it's successful this event grid will
trigger I don't need a Venn grid to trigger on failures or cancellations I
only want success at this point and one other thing to note this will fire when
a resource group is created or modified because that's a right action as well as
you can see here there's no option for resource created or new resource it's
just a write success endpoint type will be webhook I'm gonna select an endpoint
and paste in that URL and we'll create I can't see it here because I have the
wrong event type so if I go to Azure subscriptions we'll see I have a couple
in here and this is the one that I just created so let's go back to vs code so
good I have the run book set up and I have the webhook information I have an
event grid firing now I need to figure out a way how to get the resource group
name subscription ID and you name out of the JSON data that's passed
into the run book so in order to do that the first thing I'm gonna do is actually
trigger that event because if the event grid is working correctly and I trigger
the event the run book should run and I'll be able to see the input data so
let's go and create a resource group
I'll just call it event grid test1 and create I'm gonna go back into automation
accounts go and in my demo automation account and go into run books let's take
a look at the run book that I set up it looks like there's a job queued so I'll
just refresh and based on past experience with this I'm actually gonna
see two jobs show up one of them will be the event trigger for the event grid
creation because that was a right action I created something new so that will
fire the event grid and then after that the second job will come from event grid
firing with the resource group creation good they're both in here now I think it
should be this one so this is the second one that happened I'll probably have a
lot of this blanked out but event grid test one that's the name
of the resource group so this is the one we want the other one again was the
event grid creation which I don't need the data from that I want the resource
group creation data so I'm gonna copy all this from curly bracket to curly
bracket I'm going to go back into vs code I'm gonna go into the web hook data
and paste that information in okay I have that data paste it in but it's
really not in a format that I can read it so because this is a JSON document
and it's JSON data all I have to do is go ctrl shift P and vs code and format
the document I'll scroll back over to the left here and there it is so I have
the contents of webhook data that the data that's passed in that's the
variable that's input into the run book is the webhook data but what I want I
need to get some more information and there's this request body I'm gonna copy
it from curly bracket to curly bracket you don't need this square bracket let's
see there we go so this represents data in webhook data dot request body so I go
over to this document which I named as you might guess web hook data dot
request body dot JSON paste it in I have a problem with this data
Microsoft includes an escape character before the quotes and that escape
character is a backslash and that is not valid JSON we go ctrl F to find I'm
gonna do a fine to replace replacing the backslash with nothing there we go and
I'll replace all now if I do the ctrl shift P again now it's in a format we
can read it let's go back at my run book demo here and first we need the resource
group so if we go into this data so this is web hook data dot request body and we
go to let's see here subject and here is the name of the
resource group it's probably in here a couple times you can see it's in under
scope as well but I'm gonna take the subject and I'm gonna try to pull the
resource group name out of this string and I can do that pretty easily let me
just paste this in I can do it pretty easily with the split on the forward
slash so here what I'm doing is I'm taking the request body dot subject and
I'm splitting the content of that string on the forward slash and then I'm
assigning the fourth object in that array I guess technically fifth if it's
counting by zero but it works out either way to the resource group name so let's
take a look at that so I'm in our web hook data dot request body subject one
two three four there it is so that works exactly the way we want it
subscription ID I'm gonna do something similar let me paste that in as well
here I'm pulling the data from request body topic so if I go into request body
dot topic I think that's all the way at the end yep and I'm taking the second
value which is my subscription there we go so it's just splitting again on that
text string and then taking the second value of that array username is gonna be
a little bit easier actually I go back to request body data claims let's see
here data claims and then name there's my name so that's all I need to identify
my name so just to recap what I'm doing all I'm doing is pulling that data out
from the job so I can see what's coming in I know that it's assigned to the
value of weblog data and then I'm just breaking that up in vs code for no other
reason just to visualize it and see what I've got and how I can use that within
my run book maybe there's better ways to do it but this is the way that makes
sense to me so let's see where we're at now we reviewed the webhook data we
identified how we're going to capture that data that we want to put into our
team's message now that we have all that let's go back to event grid the way it's
configured currently any right action on the subscription will trigger the event
we don't want that we only want it to trigger with resource group creation we
need to add a filter to the event grid trigger so it only triggers on resource
group creation so let's go back into event grid have been grid subscriptions
we'll change this to Azure subscriptions pick our event grid and
we're going to go to filters and at the bottom is advanced filters we're going
to add a new filter so it's going to ask for a key value pair let's go back to
that JSON to determine what it is we need to add here so if I go into request
body under data authorization and action right here we have an action
of Microsoft resources subscription resource groups right this is the action
we want to filter on so we only want the key of data authorization action and the
value of Microsoft resources subscriptions resource group right let's
go back and add that in so that key was data authorization action and here we
have some operators I'm gonna select string contains but you can see there's
a few of them there's a number as in number is not in we've got a bunch of
strings we have a boolean operator as well I'm just gonna do string contains
so the value of that key was Microsoft dot resources slash subscription slash
resource groups slash right there we go I'm gonna save it there it is so now
with that filter it's only gonna trigger when a new resource group is created or
when an existing resource group is updated again because we can't filter
just on creation it's a write operation I'll save that
here I'm back in vs code and we've got just about all the information we need
we've got the filter working on the event grid trigger we figured out how to
get all the data out of the JSON information that's passed in the run
book is created and we got the web hook set up the next thing we need to do is
create a web hook for teams so I'm gonna go into teams I have a channel setup
called Azure automation demo and on the very bottom here I'm going to go
into more apps and search for web hook there's an option for incoming web hook
I'm going to add it to the team Azure automation demo and install I'm going to
leave this to go to the general channel and set up I'll call this Azure
automation web hook you can select an image I'm gonna leave it as default and
create now if I scroll down here is the URL that I need I'm going to copy that
to the clipboard and click done I'll close that out I'm gonna come back
to vs code and paste that in now that we have all this information we're ready to
finish the Azure automation run block so I'm going to go back to Azure
automation here I'm back in Automation in that run book we set up I
did upload and made some changes here so I've just got the final run book created
I'll make sure this is available on github so you can use this as you'd like
here is the channel URL that will need to be updated with your channel URL from
teams I do have an image file that I'm pointing at I'll probably pull that I'm
just using that for the demo right now but you can insert an image into the
body of the message that is sent to teams here I'm converting some webhook
data so I'm getting the webhook data dot request body and converting it from JSON
I do have some error handling in here as well if you're not familiar with the
try-catch error handling there's a great resource on powershell org I suggest
taking a look at it in the next step I get the resource group name and we
already went over how that's done here I'm getting the subscription it's the
same code as we saw before I do have some information here that I commented
out this is just for troubleshooting I was able to output all the data to make
sure it was what I was looking for this section actually came from Microsoft
I'll leave a link to the article there's a couple that I found with this code but
I did not build this myself but I did modify it slightly to fit this use case
this is the actual body of the message that gets sent to teams and that's just
assembling it all and then at the very bottom I have invoke rest method and
that's gonna post the channel URL the body and if everything works correctly
when a new resource group is created we'll see a message in teams so I'm
gonna click publish I already saved it now let's go to resource groups and I'm
gonna create a resource group I'll call it event grid test rg2
I'm going to review and create so that should trigger our event grid let's go
back in Azure automation and we'll go into the run book we'll give it a minute
we should see a job start up the job is queued okay it's complete there it is so
here is the notification it's showing that a
group was created I can see the resource group name the subscription ID and the
user all within the body so that worked correctly that's it for the demo that's
how you use a bent grid and Azure automation to send a notification when a
resource group is created in teams cool as I said at the start this is a fairly
specific task but with some modifications it could be made to send
notifications more relevant to your environment I hope you found this useful
don't forget to subscribe thanks for watching