- Coming up, we'll look at serverless,
event-based automation with Azure Functions using PowerShell
to automate and manage your cloud-native, hybrid,
and even on-premises resources.
So today I'm joined by Eamon O'Riley,
who's leading our efforts at Microsoft
for serverless automation.
Welcome back to Microsoft Mechanics.
- It's great to be back Jeremy.
- And thanks so much for joining us today.
And before we get started,
if you're new to Azure Functions,
this is really Microsoft's event-driven
serverless compute in Azure.
For example, you can execute code or a script,
allowing you to automate rules,
configuration, even processes.
Like for example, triggering a notification.
And because it's on demand,
you only pay for the exact time needed
to run that function right down to the second.
- Yep, and it's not just to manage resources in Azure,
you can manage resources wherever they are.
In your local data center or even in other clouds.
- Right, and all the Azure services themselves,
obviously they've gotten native PowerShell support
for using automation with Azure services also.
- Yeah, that's right.
With native PowerShell integration,
with Azure Functions now generally available,
you no longer need a VM running
to execute your scripts.
And if you already have existing scripts,
those could work also.
- Right, so can you show us an example
of how all this kind of works together?
- Sure.
I'll show you a scenario most people need to be able to do.
And that is to shut down their underutilized resources
using PowerShell functions.
- [Jeremy] Okay.
- [Eamon] To do this,
I'm going to show you how we can listen
for the creation of a new VM using Event Grid.
Event Grid will then call an Azure function to add
start and stop tags to the VM.
The function then retrieves
the Teams webhook from Key Vault,
and sends a notification to the VM creator via Teams.
We then have two scheduled task functions
that start and stop the tag VM,
based on the time of the day.
- This is pretty cool because it's that shift to
eventing from polling.
You might've in the polling days
ran a PowerShell script on an existing VM
using the scheduled tasks that's running,
maybe get commands to see what VMs
might've been created since the last run,
and you're running a lot of times over and over again,
and not actually executing against it
because it's not meeting any news criteria there.
But in this case,
it's a lot more efficient using Event Grid.
- Right, it's only using resources
and running the script when an event happens.
And that's the whole point of event-based automation,
you only take action when the event occurs.
Let me show you how this works.
You can see here that I just got notified in Teams
that a VM has been created,
and now I click in and manage the VM.
I go to "Tags", and you can see there's a Cost tag
with auto start and stop times.
And I can adjust this as the owner of the VM.
- Okay, so why don't you tell us,
this is Mechanics, why don't you tell us
what's all behind this, making all this work?
- That's right.
So this is using Azure Functions.
So let's go back to the portal.
Here are my three Azure functions.
I have my start VM timer function.
I can look into the code.
You'll see how I've altered this
using standard PowerShell commands,
but let me show you our event-driven function.
You'll see that it parses in the Event Grid data
to get the VM name and resource group.
And then updates the metadata on the VM,
applying the tags, and it's integrated with Key Vault,
as you can see, retrieving our team's webhook.
And there's a notification to Teams.
- Okay, so those are all the functions that are created.
Now, what do we need to use
in order to get the eventing part of this to work
so we can actually call and run those functions?
- Yeah, so I'm using the Event Grid service
to configure this.
You can see I'm in my Event Grid subscription.
Click on the "AddTagToVMs Event Subscription,"
and you can see I've configured it
to listen for VM creation with my Azure function
by filtering the key on an operation
that contains a virtual machine write
so that we only get notified when VMs are created.
- Okay, so now we've got all the plumbing set up,
can we see all this in action, working?
- Yeah, we definitely can see it.
If we go back to the VM that I added a tag to
and go to the activity log,
I can see all the operations.
And I'll click on "Start Virtual Machine."
And here, we see the VM was started by the Azure function:
ContosoDemoSession.
- This is really great too
because I bet a lot of people
are familiar with the ability to stop a VM
using the virtual machine blade inside of Azure,
but now you've got an easy way
to basically start that VM on a schedule, in this case,
using Azure Functions along with your tags,
and kind of the parameters you set there.
But what if we didn't want to start it
just automatically on a schedule,
and we wanted to add some
approval workflow to our automation?
- Yeah, that's where we can use
another serverless capability with Logic Apps
and that's where it really shines.
Let me show you how you can trigger an approval process
and add it to an automation that scales your database
when it's under a heavy load.
- [Jeremy] Okay.
- [Eamon] You see it's currently set to basic.
I want to trigger my approval process
with my IT team to move up to standard
as I get close to my performance threshold
by sending an email,
allowing my IT team to approve or reject a request.
- Right, so now this looks really easy then as the approver.
But what's, again, the mechanics?
Getting all this stuff to work behind the scenes?
- Yeah, and that's where we'll start an Azure Monitor.
I'll click into "Create rule,"
I configured an Azure alert.
First, we need to point it to a resource.
In this case, my SQL database.
Then I need to give it a condition.
I want to make sure that if my SQL database
has a high percentage of usage,
it will call on an Azure Logic App
that's part of my action group.
And this will send an email
to increase the scalable size of the database
by moving onto a new database plan.
Here we've configured the alert
to look for DTU percentage greater than 80% as a condition
and move from basic to standard
so it can meet my app requirements
as the load size increases.
Next, I want to create an Azure function
to instigate the plan change upon approval.
I'll click on "Functions,"
then my Scale-Database function,
and you can see the code here.
And you can see our Azure function PowerShell.
First, I need to parse the request
to identify the database that I want to upgrade.
I'll take the resource group and the database name
and find the database edition.
It will see if the database edition is basic.
And if so, then I'm going to upgrade it to standard.
Finally, I'll send a notification
to my Logic App to tell it that the upgrade succeeded.
- Okay, so can you give us a look at the Logic App
that's actually going to run all of this behind the scenes?
- Sure. Let me show you how I created the process
to call that PowerShell function
that we just created earlier.
So here I'm in the Logic App designer,
and you can see the workflow is already running.
The HTTP request is receiving the alert,
and it sends an IT approve notification email.
And it will trigger the Azure function
to shift up the database plan upon approval.
In fact, it's waiting for manual approval email.
If we go back to our email and click approve,
and now we turn to our Logic app,
you can see it resuming the process.
And it's approved because it's moved to the next step
in calling my PowerShell function.
This will take a few moments,
and you can see it completed in about a minute.
And now if we go back to our database under "Configure,"
you'll see that the plan has moved to standard.
- So this is pretty neat because we're combining
the visual workflow then with Logic Apps
and also PowerShell and using all those
serverless resources together.
- Right, that's one of the main reasons
it's easy to get started with automation
and get it up and running.
But there's one more thing I want to show you.
If you look under Networking
for a function I created to manage hybrid machines,
you'll see we have full VNet integration
which means you can also set this up
to manage your on-premise resources.
- So this is really great because not only
can you automate everything inside of Azure,
but you can bring this control also
to on-premises or other clouds too.
So thanks again for joining us today, Eamon,
and sharing all the things that you can do
with PowerShell using Azure Functions.
To learn more, check out aka.ms/PowerShellFunctions.
Thanks for watching, we'll see you next time.
(upbeat music)