>> Hey, friends, Azure Functions is
about more than just running simple scripts.
It comes with multiple language support,
integration with other services like Azure Key Vault,
and Azure DevOps, and it provides flexible hosting options that
will support even the most mission critical enterprise workloads.
Jeff Hollan is here to show us how it
works today on Azure Friday.
[MUSIC]
Hey, friends. I'm Scott Hanselman
and it's another episode of Azure Friday.
I'm here with Jeff Hollan. Hello.
>> It's so good to be back on the show.
>> I am thrilled to see you
because everyone's talking about serverless,
which is a really complicated way
to talk about other people's computers.
There's a lot of servers involved in saying serverless.
>> That's right. Yes.
>> But what's great about Azure Functions is if I understand
correctly I don't have to think about virtual machines.
I don't have to think about computers at all.
I just think about functions.
>> Yeah, that's exactly what we're going for
because as an organization,
what we're hearing time and time again is I
need to be able to get these applications out the door faster.
When that message comes
down to teams and developers, they're like,
"Hey, I'm trying to build
these modern applications so they may be able to scale.
We need to be able to make sure that we're only
paying correctly for the right resources that we need."
Sometimes that involves some really
complicated problems that have to be solved,
and what serverless is all about,
and what Azure Functions is helping to provide is just
take the code that you care about that powers your application,
publish that to Azure,
and let Azure take care of all of the stuff
of scaling, operating, securing,
making sure that this thing is resilient,
so that your teams in
your organizations and your developers are more productive.
>> I love that. I've joked in the past about how in the old days,
I had a multi-page resume.
The second page of my three-page resume was
scaling a web farm, but now it's a checkbox.
So I just ripped page two out,
and now I've got page one and page three.
>> I hope you're not passing this resume around anywhere.
>> Not anymore.
>> That's good.
>> What's great about serverless is I get to focus
on the code and think less about the infrastructure.
>> That's exactly right. So let's take a look at it now.
So one of the things that's impressive about Azure serverless
and functions specifically is
the integration with development tooling,
unsurprisingly giving Microsoft's care
for developers Visual Studio Code,
Visual Studio, but even integration with
other editors like IntelliJ or Eclipse,
if you're writing a Java function.
>> Let's wake up your machine here.
>> Yes, so let's check out,
I'm in Visual Studio Code here.
I have an Azure extension and I can choose
to create a function app right here in Visual Studio Code.
So I'll go ahead and choose this folder,
and the first thing to call out as we mentioned,
we actually have a lot of languages here that I could choose from.
So I can write this in Python, JavaScript C#, Java,
and we even just have this new PowerShell preview as well
so I can choose which flavor of Azure functions I want to use.
>> Wow, that's cool.
>> So we'll go ahead and just use a simple Python function here.
The next thing I want to surface is the amount of
sources that you can choose to run your code on.
So often when people think about functions,
they're thinking about an HTTP like I'm going to send a request,
and it makes sense it's really easy to
send an API back into our mobile back-end,
but I have these nice options to do these background tasks,
where maybe I'm listening on a queue or an IoT Hub,
or a Cosmos DB database change,
and I want to run some JavaScript code anytime I add
a document to Cosmos DB. That's an option as well.
>> This is so important to remember that
the HTTP trigger is an obvious one.
If you made a website before,
but it just did one thing,
why not just do an Azure function.
But also I think people think about functions
like they're going to write some code that says "Well,
true," and then do some stuff,
and then go to sleep when they really need to
think about event-driven applications.
There's no reason to pay or have
it doing anything if nothing's happening.
When the event occurs in any of
the many Azure applications
in Azure services that we have available,
they'll tell Functions and Functions wakes up.
>> That's right. It's actually a nice way to think about
your code and then I really do just want this to run on an event.
I don't want to have to worry about that while
loop and constantly listening and pulling.
>> Fantastic.
>> So if I chose an HTTP trigger here and
just finish with some of the default options.
Visual Studio Code is going to set up for me
this exact project with all of the files
and pieces that I need here so that I can get off and running.
In this case my Python app
running just a few lines of code here saying,
I'm going to get my HTTP request.
I'm going to see if somebody saying hello,
and return back my response,
and this is now where I could replace this with
my own code that I want to run for an HTTP request.
>> This is interesting. I'm not a Python person,
but I speak Python a little bit,
and I see you've got a main here,
but is this a console app.
This isn't really an app that I can run at the command line is,
or if I need, but I did that,
I'd need some infrastructure around it.
>> That's right. So normally,
if I was setting up an HTTP server,
I need something like Flask in the case of Python.
So we want to make it feel like a console app
because personally when I'm just like writing stuff,
I like just having my main,
but you're right there's actually a lot of
power here behind the scenes,
where I'm not having to build up
that infrastructure framework code,
but it feels just like a very simple console app.
>> Right. Ordinarily, with main whether it be in C or C#,
I go main and then args where args is an array of stuff,
and here you're passing in an HTTP request
and returning an HTTP response. It's very natural.
>> That's right. Now, I want to show
even how this can get extended
further because there's
some pretty impressive capabilities here as well.
So I'm going to switch over,
this is the same HTTP trigger,
but I'll switch over to a project
where I've written a little bit more.
So in the same case I see main here still. I'm still in Python.
I'm getting an HTTP request.
But in this case, there's a common problem,
or I guess a common pattern,
where when I get that HTTP request,
I actually need to interact with some data.
Maybe I get the request in and it's a put,
and I need to go put that in a database behind the scenes.
So what I'm doing here is I'm actually specifying
an output variable that I'm saying it's called doc.
This is a document.
Now, what this maps to using
the Azure Functions tooling is that this document
will now automatically integrate and push
whatever I set the value to into Cosmos DB for me.
So in this case, I'm getting an HTTP request in.
I'm going to be doing some processing on that request,
and right down here,
I'm setting the document variable,
and that's going to automatically push that to Cosmos DB for me.
>> Interesting. So in my naive mind,
I would have thought I would make an HTTP request,
and I would post it myself over into
Cosmos as if Cosmos was a REST API because it is,
but you've integrated that so
I don't have to do any of that extra work.
You've probably saved me 11, 12 lines of code.
>> Exactly, and the way I've defined this here is there's this
function.JSON file which can
be auto-generated depending on your language.
In the case of Python,
I use the tooling here to just specify,
"Hey, this is going to Cosmos DB.
Here's the database, here's the collection.
So rather than having to write those 11 lines of code,
figure out the Cosmos DB SDK,
I just defined some metadata here,
and Azure Functions is actually doing the integration work for me.
So I could connect to Cosmos DB queues,
even do things like send a text message with Twilio,
all without having to manually write that integration code myself.
The older I get, the more I
appreciate code where I don't have to scroll,
and you've got this whole function fitting on one screen,
and your fonts are big.
This is pure win for me.
>> The last thing I'll show you well here in Visual Studio Code
is just the debugging and development experience.
So you mentioned earlier like, "Hey, this looks I a console app.
Can I run it locally?"
Well, it's not quite a console app.
I can't do things like set a breakpoint
here and come in here to Visual Studio,
and start a debug session.
What this is going to do is it's
spinning up the Azure Function's Runtime.
You'll probably see some fancy ASCII art
here pop up in just a second.
So this is the same runtime
that it will be executing on in the Cloud,
scaling to potentially millions of events.
But here, it's running for me locally.
It's even expose for me a local endpoint,
so that now I can use this to test and debug my app.
>> Really. I am running Azure Functions on my local machine.
I'm doing serverless on an airplane.
>> That's right. Yes. It can be on an airplane and it's
the exact open-source runtime that will be running in the Cloud.
So let me tell you a little bit what this code is doing,
because you mentioned it is only a few lines of code,
but I like what it does.
So I'm getting in an HTTP request.
I'll show you here what a request might look like.
It's just a simple JSON object.
It's got an ID and it has some text.
>> Okay.
>> Now, I'm going to take that text and I'm going to
pass it to an Azure Cognitive Service,
and it's going to pull out the sentiment of the text,
is this positive texts,
is this negative texts.
We see a lot of companies doing things like this where,
"Hey, I'm getting reviews that are submitted."
When somebody submits to review,
this want to run some code,
do some analysis on that review,
and maybe drop it into something like a Cosmos DB database.
So that's exactly what this is doing.
Let's go ahead and send that request to that local endpoint here.
As soon as I send that,
right away, my breakpoint got hit.
I could come through with stepped through,
debug my code, understand how everything is behaving.
So I get this nice local development experience.
This looks good to me, so I'll just go ahead and push run,
and I got my response back,
and it gave this with a very positive sentiment for being amazing.
If I even came over here to Cosmos DB,
I should see here that I now have a new document that
got added to Cosmos DB for me as well with that data,
now stored in Cosmos DB,
just in those 30 lines of code.
>> It's so nice that you're in
Visual Studio Code which is a code editor,
but you've got this extension on
the left there with functions, with storage.
With Cosmos DB, you haven't been doing a lot at the command line,
you haven't been running off into the portal,
you're just in the environment that you're comfortable in,
and it's all there for you.
>> Yeah. That's it. We really want to make, again,
this really productive experience.
So that I just have the little bit of code I want
to write; PowerShell, Python, whatever.
Let me do that as fast as possible without having to worry about
the pieces that I really don't want to have to go build again,
that I can rely on something like Azure Functions to do for me.
>> So you're showing a really clean way to talk to Cosmos DB.
That line 24 is amazing.
I just didn't really get my brain
around how much is going on in that line,
and the idea that functions
speaks Cosmos by default is really powerful.
There are other Azure Services that Cosmos also speaks.
Can I talk to Key Vault,
can I talk to DevOps,
can I talk to storage,
and make it easy as well?
>> Yes. So we have ways to do this in code.
But the other way that's nice too is
sometimes I want my code to integrate with
these services and sometimes I
actually want my app to integrate with them.
So just in the last minute or two,
I want to show you some things that once this is
up and ready to go when I publish this to the Cloud,
we want to make sure that your whole app as a part of
this nice ecosystem and it can
integrate for some nice capabilities.
>> All right.
>> So for example here,
just a nice preview.
Let's say that I have a secret in my code where.
In this case, when I talked to cognitive services,
I need an account key to actually get the sentiment score back,
or maybe I have some connection string,
I need to have that secret securely taken care of for me.
One of the things that we provide for you
when you publish to the portal
and this is an Azure function that's in Azure already,
is we actually give you a way to connect to
this app directly to an Azure Key Vault,
so that I can go pull that secret from Key Vault.
It's never stored in source control,
it never ends up in my code,
it can never be leaked to a developer or an operator,
and we're just going to do that meshing together for you.
So without having to again write the Key Vault SDK,
I'm pulling that secret into my account.
>> I was wondering where that key was
an API key that you'd need to go and talk
to cognitive services was, and of course,
I know that I could use
Azure Key Vault and bring in NuGet packages for C#,
for bringing a Python package but
you're surfacing that as an environment variable.
>> That's right. In the app settings of this app,
I have some of my environment variables,
and if I actually show the value here,
my secret isn't actually the secret value.
>> No, it's a pointer to a secret.
>> I'm saying go to Key Vault and grab the secret for me.
We'll do the identity for you,
you can manage permissions here,
no usernames and passwords,
nothing in source control,
nothing in my code itself.
We're just going to connect those dots for you.
>> That is so clever.
>> Because I know someone's looking at that saying, "Oh,
they've just exposed my secrets as an environment variable."
They've exposed, in a comfortable way,
to call environment variable,
then you see that call and
functions runtime takes care of the work.
>> That's it.
>> Very clever and very clean,
and all of this works today.
If you've got Azure Functions,
you've been quietly upgraded
to all the goodness and all the great stuff.
So if you're new to Functions, you have it,
and if you're old to Functions, you also have it.
>> That's exactly right. We're constantly adding these new ones.
The last one I'll show here monitoring and diagnostics.
So I just write my little bits of code.
Maybe I haven't thought a lot about how am I going to log this,
how are we going to manage this at scale.
In the same way, every function app that gets published,
we're going to turn on sittings like application insights.
So I want to show you here another app.
This is Azure Application Insights.
I've done nothing to my app here.
I just published a few functions,
and I can see here in Application Insights,
it's letting me know that there's some failures,
and you get nice features here,
I can actually open up what's called an Application Map.
Just because you're running on this fully managed service,
we're emitting the right stuff for you.
So in this case actually have like
six different functions and they're
all somewhat dependent on each other in different ways.
Sometimes, in this case, this is a shipping process.
>> These are all Microservices effectively.
>> Exactly right. Microservices' functions,
but just out of the box as part of the framework,
we're going to emit the right stuff so that you can
get these nice application maps out of the box,
trace where messages are coming,
understand, "Okay, I'm getting a failure here,
but the message actually started over here,
and I can do that full end-to-end traceability
for my serverless microservices."
>> Are those instances that's telling you how each of them
is scaling and deciding the right amount of traffic.
>> That's right. So I can actually
observe the servers behind the scenes of
serverless to make sure that the load is being used appropriately,
or if I have some bad code that might be consuming a lot of CPU,
I can make those changes and improve my app.
>> All built in, you get that just when you start
your very first function and the billing is.
>> Per execution.
>> You're only charged when your functions' actually running.
So if nobody calls that Cosmos DB function, I don't pay a dime.
The next day a million people call it,
will scale out to
as many infrastructures as we need or as many services we need,
and you only pay for each of those individual calls.
>> I love it. I'm going to go and reevaluate some of
my projects that maybe just need to
be moved to functions, it'll be a lot simple.
>> Sure, absolutely.
>> Fantastic, I am learning all about serverless functions
for event-driven applications today on Azure Friday.