in this video I'm gonna show you how to use Azure function to send a message to
teams when a new resource is created in Azure
hello this is ciraltos and I'm Travis in a previous video I used as your automation to send an alert to
teams when a new resource group was created I'll post the link above
recently Azure functions announce preview support for PowerShell and I've
been looking for an automation example to give it a try I thought this would be
a great example to get started with Azure functions in this video I use an
azure function to send an alert to teams when a resource is added to an azure
subscription I modified the code from the previous example to alert on
multiple resource editions before we start please take a second to subscribe
and click the bell icon for notifications of new content thanks
let's get started I'm excited to start working with azure functions in
PowerShell but need to qualify this video and point out that I'm new to
Azure functions I'll outline the steps I took to get a functioning example and
include links to any documentation that helped along the way in the video
description with that said however I may not be
doing this in the most efficient manner here's the flow a resource is added to
an Azure subscription and event grid triggers and Azure function the
function accepts the input data from event grid and then uses that to format the
message to teams after that the Azure function triggers another webhook and
post the message to teams the code is similar to what I did with Azure
automation but I made some modifications to alert on multiple event types
specifically this code will alert on a resource group and server creation it
should be easy to add or modify the resource to what fits your environment
best here's what I'm gonna do in this video I'm gonna create a
consumption-based powershell function app a create an event grid trigger
function that I'm gonna create an event grid subscription for that function I'll
create a filter on that event grid I'll create a team's webhook lastly I'll
configure the function to send a message to teams and then we'll test it without
Go to crate resource and function app
here it is I'll select function app and create the app name has to be globally
unique no caps or spaces I'll just call it
cirfunctionappdemo that's good I'm gonna create a new resource group
I'll leave that as Cir function app demo and I'll just add an RG at the end I'm
going to leave it on Windows for the OS I'll leave the hosting plan to
consumption based with consumption base you're only charged for what you use and
hosts are dynamically added as events increase that's unlike the other option
which is app service plan which is billed per a set amount of compute I'll
leave location to central us and the runtime stack is going to be powershell
core and that is preview I'll leave the storage account has default and click
create and that will create my function app here I'm in function apps and my
cirfunctionappdemo has been created so I'm going to go into that and here is
the interface for function apps the next thing I'm going to do is create a
function app and I can do that right here with a plus next to functions and
I'm gonna modify this in Portal so I'll select that and click continue and I
want more templates I'm not going to use web hook or timer on this and I'm gonna
finish in view templates and I want event grid so there's one for event hubs
and another one for event grid I'm gonna select event grid it's gonna ask to
install an extension and I will click install alright it's done
you only need to install that extension for the first event grid trigger that
you install subsequent functions with event grid trigger won't require that
you'll already be there so I'm gonna click continue I'm just going to leave
it event grid trigger one that will be the name of the function now we're in
event grid trigger one a couple things I'll point out run dot PS one is the
actual script or application that's running and if I click over here there's
a couple files is also a function JSON this is control
information for the function but one thing I'll point out too is bindings you
can see the name is event grid event grid event that's required for this
input parameter of event grid event so for example if I code and I wanted to
change that variable I would need to set up a different binding but I'm just
gonna leave that as it is because that works fine also if you click down here
you can get the log and you can also get a council so you can get terminal access
to the Azure function and leave it on actually I'll just collapse that for now
the next thing I'm gonna do is modify this file just slightly ok I've added
some code this is some pretty simple write output statements that I have
we'll take a deeper look at what I'm doing with this in a little bit but just
all I'm doing is taking some information that's getting passed into this function
and I'm doing a write output so I can see what kind of data is getting passed
in but before I do that let's go over to add event grid subscription this is
creating a new event grid subscription I'm going to name this function test
sub01 under topic types I'm switching it to Azure subscription I'll select my
subscription and all I want is write success if you wanted to monitor for
like if something gets deleted you could change a resource delete success but I'm
just leaving that as write success for now this will trigger any time there's a
new resource added to the subscription or an existing subscription has been
written to so like an update for this demo though I only want to know when a
new resource group is created or when a new server is created so I'm gonna come
over to filters if you did watch my other video on doing the same width as
your automation this is gonna look familiar it's under data authorization
action that's the key the operator will be the string contains notice that
there's several operators in here that I could select and the first thing I'm
going to is Microsoft Resource subscription
resource group right so that is going to be the action of a resource group being
created or updated now the next thing I'm going to do because I want to know
when virtual machines are added I'm going to add Microsoft compute virtual
machines write now you may be wondering where I got that let me show you a
document from Microsoft that is the resource provider operations so under
compute Microsoft compute if we scroll down to virtual machines here it is that
took me a while to find Virtua Microsoft compute slash virtual machine slash
right create a new virtual machine or update an existing virtual machine so
this document has tons of different resource provider operations for pretty
much any resource that Azure has you can go through and you can find whatever
you want to filter on and just pull that in again I'll leave that link in the
description so you don't have to try to follow along with me scrolling ok let's
get back so those are the two values from that key that we're going to filter
on now click rate and notice I didn't have to manually add a web hook that's
created automatically ok I'm gonna save this I can't remember
if I did that before you do have to save it if you make changes and then I'm
going to add a resource group by adding a resource group I'll trigger this
function and then we're gonna see what kind of information is getting output
I'll just call it function demo test and I'll create ok here I am back at the
azure function and I'll be honest with you there's been a few minutes between
when I created that resource until now if I go to monitor here we can see the
output here are the logs for the last time and first time for that matter that
this function ran and it does take up to five minutes between when that function
ran and when you can actually see this data let's just go back here for a
second so I've got three pieces of information that I'm outputting here one
is event grid event so here I'm getting the data that was passed in and I'm just
doing an out string and writing output so with that I'm gonna be able to see
what is getting passed into this function the next one I'm doing a get
member on that data and that's just going to tell me what data type is it be
getting passed in and I do this because with Azure automation the data coming
in is JSON we're gonna look at how as your functions are treating data as it
comes in and then the last as we're going to convert it to JSON and write
the output so let's go down and look at what's happening here's the output data
this is the data that was passed into the function and I'm just doing a
straight write output on that here is the information from get member and the
interesting thing about that is it is a hash table so now I know that the data
coming into the function is handled as a hash table and I can use all the tools
available for a hash table to work with my code in the function the very last
thing I did convert to JSON and then a write output so this is all the JSON
data if I copy that data from curly bracket to curly bracket I can paste
that into a JSON document and examine some of the data at this point I pasted
the JSON data into a file and teams called eventgridevent.json and this
is purely just so I can see that data and try to get a feel for what's getting
passed in and identify certain values that I'll need for my function app so if
I go back to function Apps the first thing I'm going to need is the
subscription ID so let's see if we can find subscription ID I go back to event
grid event and we can go under data and some place down here here it is the
subscription ID so all I need to do is identify the value of event grid event
data dot subscription ID I'll go back here next is the type name type name is
the name of whatever was created so if it's a server it's gonna be the name of
the server that was created if it's a resource group the name of the resource
group that value can be different depending on what type of resource it is
that's the reason why I like to copy and paste this information into a JSON
document as it helps me just visualize and find it I want to find the name if I
go down to the subject there it is and here's this it's the values our
subscription subscription ID resource group and function demo tests that's the
name of the function so the value of event grid event that's subject and the
fourth value in that is the name of my research group okay here's the code that
I'm using and as the azure automation runbook that I did before I'm just
taking the value of event grid dot subject and I'm doing a split on the
forward slash and the fourth value is the name that location may be different
depending on the resource that's created for example servers it's actually the
8th item and that's where having the JSON version of that event grant
information is helpful you can run it once
visualize it find that value and then put it in the code the next thing I need
is the username let's see if we can find the username let's go up to event grid
events and then data claims so data and claims there should be an option in here
called name there it is so that's the name of the person who
created the resource so all I need to do is come over and that value I don't even
have to assign it to a variable I can just call it out during run time last
item is channel URL let's go into teens and create one okay here I am in teams
I'm gonna go down and go on down go into apps and search for webhook incoming web
hooks I'm gonna add it to teams I'll leave it at the general channel and set
up the connector I'll configure it I'll call it function demo I'll leave the
image as it is and create once it's created there'll be a web hook URL that
I could copy I'm just going to paste it in here great now I have all the
information I need to finish the Azure function here we are at
the Finnish runbook so I took care of this offline but I'll go through this
will be available on the github page I'll include the link in the description
as normal I still have the code in here that we looked at before that was just
doing output you can comment that out if you want to it's really not needed but
it doesn't hurt I have some error control in I've got my channel URL that that's a
channel we got from teams I've got some error correction in here but basically
I'm getting the subscription ID I already talked about that one and let's
see here I've got an if statement this is different from my Azure automation
code so I've got an if statement this one looks for Microsoft compute slash
virtual machines slash right so this is an event if a virtual machine is created
I said an activity type to server we'll see where that comes into play here
shortly an image URL you can update that as you need to next I have the type name
so in this case I'm pulling the data and it's gonna be the 8th item in the array
is the name of the server that's created I put this in an if statement so if it's
not a virtual machine slash right the next option is a subscription slash
resource group slash right this is an else--if statement that identifies if a
resource group was created and again I set the activity type to resource group
and then another image so it's all pretty much the same if you wanted to
alert on more than two items you could copy this code do some minor
modifications and get alerts on other events as well here's some more
information add for testing and pretty sure I commented that out on the version
I'm making public and then the body and here the body I rewrote it a little bit
so we can pull in some of that data it's a little bit more flexible in what is
pushed over to teams as far as the wording and then at the very end is the
invoked rest method where it sends the message to teams I'm gonna save that the
first thing I'm going to do is create another resource group I'll just call
this test one and create I've opened up logs in the azure function we should be
able to see when event grid fires off the trigger that runs this function okay
there we go we can see some of the informational outputs happening so that
trigger was fired let's pull open teams and see what it looks like so there it
is you can see the azure resource creation notification happened it's
telling me that a new resource group is created it's giving the name of the
resource group as well as the subscription and who created it let's
see what happens when we create a server here we are you can see I have a second
notification this time it's indicating that a new Azure server was created it
gives a server name the subscription ID and who created it so that's exactly
what I was looking for that's it for the demo in this demo we created a
consumption based app function I showed you how to view the data coming into
that function and even export it as JSON so you can review the data passed in
even further we set up an event grid and we set up filters I also shared the link
for the resource provider operations page so you can create filters based on
other Azure resource manager operations we created a team's inbound
web hook and we finish it off with a full code for the function that sent an
alert to teams when a new resource group or server is created that's it for the
demo that's it for the demo I hope you found this helpful I have more to come
on Azure function so please subscribe and click the bell icon for
notifications of new content thanks for watching