Azure Friday Live – Building serverless Python apps with Azure Functions | Azure Friday

[MUSIC]>>What a joy it is to be
here today at Build 2019, doing Azure Friday Live. We’ve never done it live
before. It’s amazing. We’re here with Asavari Tayal talking about Python and
Azure functions. How are you?>>Pretty good excited to be here.>>Yeah, I’m glad. Thanks
for coming to share some of the new things that are happening
in Azure Functions for us. You’ve got to make
a few demos for me. When I tried to go into
Functions before in the past, and I would say, it
probably maybe a year ago. I make a new function and I get excited about
Serverless but then they tell me to put all of my work in this little text box
in the Azure portal, and honestly, I left and I
didn’t looked at it after that. But I understand that there’s
been some fundamental changes.>>Yeah. Well, if you’ve
been following Functions, you might know that we recently
announced Functions 2.0, which is cross-platform, what that means is you can develop
on any platform, you don’t have to be
stuck to the browser, and you can develop with
the tools of your choice. So if you’re a Python developer
and you like Visual Studio Code, you could go in and use the
extension to build your Function.>>I understand that
the Azure Functions runtime can even be run
on my own computer. I could do Serverless in the Cloud but on
my machine on an airplane.>>Exactly. It’s open source, so you can run it on your machine, you could run it on
an on-premise device, you can containerize it and run it on a Raspberry Pi if
you wanted to Yeah.>>Can an Azure Function
be complicated? Like I said, I was doing
it in the browser, in a very small box,
but now I can do it in VS Code. Maybe you
can show us that.>>Sure. All right.>>Were going to switch over to her machine and take a look at that. So we are in VS code on your Mac.>>Yes, and I have my Azure
Functions extension installed. What I’m going to be showing
you today is how to develop a Python function app in Visual
Studio Code using Functions 2.0.>>Functions 2.0.>>So what this lovely extension
does for us is offers us these friendly utilities to help us work with the
function project. So for instance, I can say, hey I’d like to go ahead and
create a new function project. It’ll prompt me to pick a
directory for my project. Let’s go ahead and pick Demo1. It’ll ask me for the language I’d
like to use for my function app. So Azure Function supports
a wide variety of languages today. Let’s go ahead and use Python, which is our new offering. Now, as a next step, I’m going to be prompted
to pick a trigger for my very first function
in my function project. So we can pick HTTP trigger. We also support a wide variety
of other triggers. Things like events
in Azure Storage in messaging or eventing
systems, Azure Storage, Cosmos DB, Event Grid, Event Hub, and recently as of two days ago
we also added the Kafka triggers. You’re not seeing it here right now because there isn’t
a template for it, but we’re going to be
adding a template as well, to help you get started.>>When I saw that drop
down for the first time, it really changed the way that I
thought about Azure Functions. Because like I told you before, I had the wrong idea. I thought it was a simple thing for making little HTTP applications. But this is really
a fantastic platform for making large
event-driven applications, and these are all events
that can happen in other Azure services that can
then invoke my Azure Function.>>Exactly. We’re always watching
for those events so we can go ahead and provision the resources
and run your application.>>Fantastic. So what are
we going to trigger on?>>Let’s pick HTTP trigger
for our demo today. Now, what you’ll see is it’ll ask me for a name to give my function. Let’s just pick the default name
which is HTTP trigger. We’ll be prompted to pick
an authorization level. What this means is, if
I picked anonymous, it could be a public endpoint. I could also secure
it behind an API key, or if I wanted some more control
like OATH or AD Authentication, I could pick the Admin
Control as well. For the demo, let’s pick anonymous
and make it a public endpoint.>>All right.>>So what this is doing here, and you’ll see this little
processing box at the bottom, is create a new function
project for us. So if we go ahead and
look at the Explorer, a function project is, basically, the equivalent of a function
application in Azure. It can contain multiple functions that share the same
local configuration, hosting configuration, and even the requirements or
dependencies that they use.>>I see that it’s popping up a notification about
a Python interpreter. Do I have to have Python locally or does it depend on whether I’m doing
the work locally or not?>>Well, if you’re developing locally you do need to
have Python installed. We already support Python 3.6. What this will do is, it’ll give me an option of what Python
interpreter I want to use. So the Function extension already created
a virtual environment for us. I can go ahead and pick that. If I wanted, I could also pick
the python on my local bin path, depending on what your preference
is as the developer.>>Okay.>>So Let’s go ahead and pick
the virtual environment. We don’t have to
worry about this one. So the next step I can do is, we talked about Azure Functions
is open source, right? You can run the functions runtime right here on
your local machine, without even publishing to the Cloud, you can debug and test. Visual Studio Code makes it
so easy that all I need to do is go ahead and press “F5”
to start the debugger.>>Is this in the Cloud
or is this local?>>This is completely local.>>Wow. Okay.>>So what this did was it
started the functions runtime. It started what we call a
Python language worker process, and now, it’s already solving your function on
a local hosting endpoint. We can go ahead and
call this function. A cool thing I’ll show you
today is to set a break point. So what you need to understand is the Functions extension is
smart in the sense that it’s already attached the python debugger to the functions for us
that’s running locally.>>Okay.>>So when I go ahead and
call the function, we’d see that it actually breaks
on our breakpoint right here. So we can debug, view, call, stack and do all kinds
of fancy things as well.>>Yeah. I saw when you hovered over the request that you had
a whole ton of context. I mean this is
full rich debugging and the debugging that
a Python person would expect. But now they’re not thinking
about servers anymore.>>Exactly. So going back to
your point about portal development, this is not just limited
to the experience that you are seeing in
the web browser this is us bringing functions to the local development that you’re
used to as a python developer>>That’s really cool.>>Yeah. So if we go ahead and
continue and go back to a browser, I can, since this is
a Hello World Function, give it a friendly name
and continue again.>>So you’re passing
something in as a parameter, as a query string, and then changing and coming
back around the other side.>>Exactly, I already have
that local hosting endpoint. All you needed to look at, since this function got
generated from the template, I do want to walk
through a few things that really help you
configure the function. So if you look at the HTTP
trigger folder I have right here, that’s where my function
is actually located. The HTTP triggered function. We’ve got a script file right here, which is called the dunder init file. Then this other one which is
called the function JSON file. Now, this is the configuration
where I actually define the script file
that I want to invoke, the Python script file that I want to invoke when my function’s executed. That trigger that will
invoke my function. So in this case it’s an HTTP trigger. The templates already
populate things like, hey, it’s supposed to be
a Get or a Post requests. But you can obviously customize
and configure as per your needs. You can also define other bindings. In this case, we’ve already
defined the HTTP response. That’s what the template
populated for us. If you wanted you can even go ahead and add new bindings
to your function.>>This binding thing is
really really powerful. The first thing I thought
was okay HTTP in, HTTP out, some JSON in some JSON out, but it could be images dropped into storage that then
triggered the function, and then the out is
another storage bucket.>>Exactly. So what we see is, people are using functions
as the glue between several other services
that they may be using in Azure or outside of Azure. So make it super easy
with bindings for you to be able to speak
with these data sources, without having to understand what the Azure Storage SDK
for Python looks like, what the authentication
mechanism is etc. We do all of that for you. So looking at the Python script here, our entry point actually
was the main method, if you noticed, and it already
got an input of the HTTP request. This is the trigger
that we defined in the function raised
on file and it spits out a response or return value
that’s treated as an HTTP response. We already, in fact gave
it a root logo in Python. So you can go ahead and start
sending logs to the console when running locally and to application insights
when running in Azure. It also, since this is
a Hello World template, we simply grab a name parameter
on the query string and use it to construct an HTTP response that is
sent back from the function.>>You said that this was
a Hello World template, but when I go into
Azure Function in the portal, there’s a lot of different templates.>>Yes.>>There’s examples about how to
do all kinds of things including image manipulation or
calling cognitive services, or calling other Azure services.>>Right, exactly. You could start from
when I said this is a Hello World template for HTTP, we also have a few other
templates available for different events
sources and, in fact, I could go back and even say, hey, I’d like to add a new
function to the same project. That’s available. If you were looking for something a little
bit more complex, like an image processing. Example, we’ve got this thing called a Serverless library that gives you a bunch of samples that
the community has contributed, that we’ve contributed
to that you can already start using and leveraging.>>You had said something earlier
about a custom container?>>Yes. So the experience that I showed you right
here on the screen is, hey, I’m building
my Python script and that’s all I need to do to
publish a function.>>Right.>>But we also have
scenarios with Python, specifically, where folks
walk up to us and say, hey. I have a custom OS
level dependencies, something that doesn’t
necessarily have to be pip install but something that needs to be Apt-get installed
for instance, right? Or a scenario where I’m using
a custom distribution of Python, something like the Anaconda
distribution, maybe.>>Okay.>>Which you guys don’t support. You guys as an Azure, don’t
support out of the box. Can I bring in a custom Docker
container to support the scenarios? Now, the cool thing
with Python is that we actually introduce being able to bring in your custom Docker image and run it Serverlessly on the
Linux hosting platform.>>That is a moment where we need to pause and think about
that for a second, because I think of Serverless
is hiding computers for me, hiding service for me,
making life really easy. I think that Python is
a really high level language, but what you’re giving us is
the power of the low level, a native library, a custom container, a custom system call into Linux. But then, you’re still making it really convenient
and really flexible, and really scalable by using
the Functions run time in the Cloud.>>Exactly. So you get
the very same programming model. In fact, if you like I can show
you a demo of that as well.>>That’s good.>>Okay. Let’s switch over
from VS code for a second because I also do want to show
you our command line features. So if you didn’t use VS Code and you were more of a command line person,
and you said, hey, I’d like to use
the editor of my choice, or I’d like to use WIM, the same stuff that I was doing in VS code is also available in the CLI via a utility we call Func or the functions command line tools,
functions core tools. If you look at it, it lets you do all the same things that
we were doing in VS Code, in the CLI instead.>>Okay. So I can do the portal, VS Code or the command line?>>You cannot do portal
today, portal editing today. That’s because we heard
feedback from a lot of our Python developers and decided
to deprioritize it for now.>>That makes sense. I mean,
they’re going to spend their time with the command line
or VS code anyways. So you prioritize that
for this release.>>Exactly.>>Okay.>>So let’s go ahead and
use our command line now. Say, I already have directory, nothing in it right now. I’d like to create
a new function project. So I go ahead and click on “Func init” and it’ll prompt
me to pick a language, similar to how VS code did. I can go ahead and do that or let me just “Control C” out
of that for a second, and say, hey, I’d like to initiate
a project with a Docker file. So now, when I pick Python, what this is going to do is, not just populate
the simple function settings files for me but also
this Docker file, and if you look at
the Docker file right here, it’s already got a template where
it’s extending the functions, the Azure Functions base image, adding some console settings to it, going ahead and
setting the directory, and adding your function project, and pip install in your requirements. Now, I can go in here and say, hey, I’d like to apt-get install
the SQL driver, for instance, or I’d like to add
something that’s completely custom to my specific workload
right here in this Docker file. Once you customize it, you can go ahead and
build the Docker image. Before we do that,
one thing you’ve notice is, we’ve only initialized
the project right now. So let’s go ahead and also add
a function to that project.>>All right.>>So func new and we can go ahead, and this time maybe pick a
time a trigger function. Now, I’ve not only got my Docker file hosting local settings but also this time a trigger folder which
actually contains my function. I can go ahead and build the Docker container using the docker build command that
I’m already familiar with, and give it a name. So this is going to go
ahead and build a layers. Since I already had the same image
built on my local machine, it didn’t take a lot of time, since there was nothing custom
I was doing right now and now, I can go ahead, and push
this image to Docker Hub.>>When you push that to Docker Hub, will Azure Functions
pick it up and know that it’s something that can be used?>>Azure Functions won’t automatically
pick up but the ID or the identify you are going to use
is this image named right here.>>I see. So you’re
going to push it to Docker Hub then tell
functions about it, it’ll go get it and
then start running?>>Exactly. So once I
have this image ready, I’ll go to the portal and this is the management experience in the portal that you might
already be familiar with.>>All right.>>This is the new create blade for functions and if you go in here, give your Function app a name. You go ahead and pick a subscription, give it a resource group. So let’s give it
a resource group of Build demo. Pick Linux because we’re
running Python on Linux. You have an option of being able to pick either code or Docker image. What that means is, code, I’m simply bringing my Python files and I’d like to run
them serverlessly, or the Docker image approach, where I can actually go ahead, and configure the Container
that I’d like, should run my function.>>We’ve only got a few seconds left. So we’re going to have to go
and fast forward at this point. So you’re putting that into Docker Hub and you’re
going to go ahead, and enter in that tag,
and you hit “Apply”. It’ll pull that down and
immediately start running that now custom Docker image
in Azure Function?>>Exactly.>>Where can people learn about this new Python ability that we
have now an Azure Functions?>>Right. So all of our docs
talk about Python functions now. We’ve got two getting
started experiences. If you go to, you can start with
Visual Studio code or you can start with the CLI
and build a function, or if you wanted to do something
a little bit more custom, you could go dive into our Developer
Guide to learn more as well.>>Fantastic. We’re learning all
about Python and Azure Functions, and the power of Serverless, here
at Azure Friday Live, Advil. [MUSIC]

4 thoughts on “Azure Friday Live – Building serverless Python apps with Azure Functions | Azure Friday

Leave a Reply

Your email address will not be published. Required fields are marked *