Build Efficient & Cost Effective Solutions Using Azure Functions by Prakash Tripathi
12K views
Nov 16, 2023
Conference Website: https://www.2020twenty.net/azure C# Corner - Global Community for Software and Data Developers https://www.c-sharpcorner.com
View Video Transcript
0:00
Thanks everyone for joining and thanks to
0:05
Esha Connor and and this entire team for organizing this Azure conference and having me here so this will be talking about how we can build
0:15
effective and efficient solutions using Azure functions of course we'll start with some context about serverless computing and how Azure functions fits
0:25
in there we'll talk a brief about its your functions and we'll take some
0:30
real use case that you know how a different function can be used to make
0:38
solutions cost-effective okay this is pretty much about at a high level you
0:44
know who am I and what did I do I think Stefan anyway I talked about it if you
0:49
want to follow me on Twitter here you can and you know we can have a chat
0:54
later okay without any further details what's going okay I know there were a bunch of talk
1:01
talk around this year and you might have of course might have seen this slide or something similar
1:07
slides which talks about or is pass for the serverless but just a brief in case you you
1:14
are joining the sessions as a fresh or now so of course you know pass is there in the system from
1:23
uh longer than serverless serverless is a bit uh modern uh flavor on on top of uh pass if i say
1:31
so of course if you look at all the services so we have is pass serverless you know sas of course
1:38
um so of course below this there's something called on like you know how you have everything
1:44
your own uh like bare metal or something like that so so basically what happens is that if you look
1:51
at the right side of diagram or I mean look the overlap of these two so certainly this is something
1:58
that gives you a little more abstraction on top of pass okay in pass you care about data and maybe
2:06
to extend you care about configurations some kind of settings that hey I want to scale my app on
2:14
when my, you know, either loads become or number of users are of certain number or my CPU utilization goes x times or my, you know, my storage or space becomes this much
2:33
So, it's been something new machine or something like that. In some or less, you need to care even those things
2:40
So application settings and basically settings or configuration things is also taken care and you just need to care about your pure application logic and nothing else
2:51
So everything goes, everything's basically, you know, takes care of its own
2:55
So you need to at least, the developer needs to worry, you know, least on this particular platform on serverless
3:03
Okay. Okay, moving on, serverless computing of course, you know, what is that
3:12
So there is a little bit of, sometimes people pick the serverless on a little sense and
3:18
they assume that, hey, there is no server at all. Of course, it's not for you as a developer, but it has to be there somewhere and it is
3:28
there as well. It is not visible to you, of course, because you don't have to care or you don't have to manage it
3:34
But it is there somewhere to help you write the code or take care about the application you care about
3:43
How it is helping? Because since I don't take care of configuration and multiple other things
3:53
It is easier to go to market, it is easier to code, easier to scale and what exactly we
4:01
want most of the time. And most importantly, from a dollar or from cost perspective also, it is better because
4:12
you really need to pay for the time you're going to use any resource, not like you need
4:18
to own it and you need to pay even if you're not using
4:22
So it is like more aligned on a piece of pay and use instead of owning something and just
4:29
keep for the sake of it and paying without it is actually being used
4:36
So serverless computing gives you that power as a developer or as an admin or as a whoever
4:43
owner or founder of any firm that you just need to pay for something that you are actually
4:49
going to use. Of course, we talked about some of the benefits of serverless computing
4:57
So you don't have to care about infrastructure, of course. Scalability is dynamic
5:01
If we compare with PaaS, we already need to put some setting in place that, hey, I want
5:08
scalability to begin on certain threshold, whether it's a scale up or scale out, but
5:15
you need to define some rules there. it's all dynamic of course since it is the focus is more on application and
5:23
logic instead of anything else so you will be faster to market and and I said
5:30
about cost that you're going to pay for the time you're not using any particular
5:34
source so you're you're more efficient using resources and you're paying for
5:41
the actual use okay okay now coming to the the the topic of this day about as
5:49
your function so it's your function as the implementation of serverless computing in Microsoft Azure and it lets you create serverless functions where
6:00
we just create some event driven kind of you know you just logic and either it
6:07
is triggered by some upstream or you can write some independent code as well that is triggered
6:14
by either some HTTP calls or it can be just simple as a timer and it is running on certain time
6:24
So it could be anything of that nature. We'll talk more as it progress
6:27
So basically, it is like you're writing not so thick logic, but it's like dividing logic into different pieces
6:39
And, you know, sometimes people loosely call it a nano service as well
6:42
So you just write a code that is that is there for certain business logic
6:48
And either it is running independently or it is integrated with certain upstream or even some downstream
6:57
So, you know, it is kind of, there are binding created. So it is getting triggered on certain actions from a stream and it is doing certain jobs
7:06
after, after completion. So all those scenarios can be, can be put together on this Azure function
7:14
Okay. Talking a little bit more on them. So as we, we can say that some residual functions or methods are put together into, into Azure
7:25
functions as the name suggests. and basically the concept of software development is reusability
7:33
So that is also being addressed with that, that you just put together something into your functions
7:39
and it can be used by multiple consumers of that particular logic
7:44
Some example could be that maybe some share market or kind of service
7:50
or some weather kind of service that the logic is there and it's just being used and, you know
7:56
there could be multiple consumers, okay. Event-driven we are saying because, of course
8:03
it can be configured to run, you know, on every minute or it can also be configured to run as some event
8:11
So, once something happens to that, those kind of scenarios come into play
8:16
on this kind of implementation, okay. Typically, it is designed to keep not so big code so that it is completing within a few minutes
8:29
Use cases where you want to run something for very long. Of course, in your template, we also got something called, they call it, you know
8:42
orchestrator kind of functions, durable functions, where you can expect those functions to do a little longer job
8:48
but in principle in ideal cases or in most of the cases one wants to go with some logic that is finishing in a few minutes maximum a few minutes maybe a few seconds as well
9:05
What are different things that are supported? Microsoft Azure is not just about .NET platform
9:15
It is it is to open source now from several years rather so
9:20
In the workload you have, there is typical my.net or Python or PHP
9:26
JavaScript, PowerShell, you know, all kind of all kind of basically with Java
9:32
all kind of language of platforms are supported. So you don't need to really know.net or C sharp
9:38
in order to work in, in order to write code in your functions. You can write pretty much what you
9:43
know and and I have highlighted something called CRM it is pretty popular in Linux
9:51
word or bash command or open of those commands so you can configure the timing
9:57
or like scheduling part you know using CRM expressions they can very much be
10:03
used and they're pretty easy way to schedule certain you know they're gonna
10:09
timeout kind of scenarios you can use and and you can have you have this this
10:15
thing set up yeah we talked about several as the functions and let's talk
10:25
about actually where it is used in real world right so for those on those to
10:30
those scenario where when you want to go for a dual function so I was talking
10:35
about what could be like, you know, you want a scheduler or some kind of timer to run on
10:42
For example, you want some mail to be sent, maybe let's say 10 a.m. or daily
10:49
or 10 a.m. Monday or whatever, right? You can set up those kind of logic in a timer function
10:59
So timer is one of the functions available for function app, one of the template
11:03
or type available for functionality right and that can help us this can help us on this kind
11:08
of scenario other could be that you want to do again uh for example once some document somebody
11:16
awarded on sharepoint you want to trigger some mail or you want to trigger uh certain uh other
11:23
you know actions like you want to insert some data into table or any income that anything uh
11:29
the required business case for you you can do that as well so it is like integrating upstream
11:36
system and down system and also those kind of use cases are supported uh a third example could be
11:44
for example you want to do certain actions on some message got at dinner in a you know in queue or
11:51
or something some notification came in uh on on this queuing scenario and those kind of things so
11:57
even though they're supported so it's pretty much most of the use cases if not all are supported
12:02
here and they're adding keep on adding multiple capabilities as i said recently not too recently
12:08
but yeah it is uh this is something called durable functions because there was a requirement from
12:14
developers community that hey we need something that can fit in for longer running uh scenarios
12:21
like orchestration scenarios where something is not completely competing in minutes but it is maybe
12:27
you know maybe 10 minutes or half an hour it is like long running kind of
12:31
thing so you should have something so they can have a durable function capability also okay we talked about this like for example timer based
12:41
processing some event processing you're doing or okay now you know like a stream
12:47
or okay enough scenario you can put together as you can say it is like 365
12:52
or so different integration points also available here so it's not just like
12:57
typical timer or HTTP or API kind of scenario but even even multiple business
13:03
process can also be indicated here you know with Azure functions kind of
13:11
template that we have this point there are many however I just put together the
13:15
the most commonly used could be timer could basically triggers blob event service for something
13:24
happening or some message added and you go to something so all kind of web hooks yeah so all
13:30
those kind of scenario commonly used of course there are more if you go to a job site you'll
13:34
find a big list i just put together as i said some mostly commonly used ones so yeah so as you
13:41
see like you know generally what we want to do on certain scenarios on on this case you know you
13:48
will get something of tablet there and just click it and you will you will get some kind of maybe
13:53
hello work enough easier example to explain you what exactly you know is being done there and you
13:59
can you can integrate you can make it make it more you can extend it based on your requirement and and
14:05
you know for real world application yeah okay uh how this function looks like right under the hood
14:16
what exactly it has so of course it has uh runtime it has to have the right how would it run um of
14:23
course when you get a function i would ask you whether you want dot and score you want java you
14:27
want python ruby php you need to tell actually to you know when you create a function so for example
14:35
I have some demo for this core so it will have a code dependency on time of course only it can it
14:41
knows that I'm gonna write C sharp language or C sharp program right then there's some configuration
14:49
they get to you know bindings basically that what if in case I have I have to get some you know or
14:55
I want this to be run this function to be done on certain triggers like some message came in my queue
15:01
or some service bus basically got triggered or something like that so those kind of configuration
15:07
needs to be there and and in the you know the top most thing where that comes to us is is a code right
15:13
the real code what i want to do when this particular function is you know getting called okay
15:19
so basically these three things together make this uh the function but but generally developers cares
15:26
about code and in some cases maybe configuration yeah dependencies are taken care of by the platform itself so you just need to select what kind of
15:37
platform you're targeting to and it will be taken care automatically for you we
15:43
talked about upstream downstream typically here the causes of from bindings basically there are things but like upstream monster systems like how
15:51
you want to connect your Azure functions so you know whether you want certain logic to be executed
15:59
on some messages it says some message came in your queue you want to know certain in team to the
16:04
tables you want to send some notifications on you know push notifications so open up bindings and
16:11
again some could be triggered like you know something happened at this point some could be
16:16
like some data came as input or something is going as the outputs all all those kind of scenarios
16:21
Like it would be bi-directional, of course, which is both input output. All those settings go into the function configuration
16:27
and you can do that. We'll be looking into this demo on how this looks like. OK
16:35
Coming to testing part, so it is fairly easy. You can test on the portal itself
16:43
although you can also test on a bunch of other tools available like post-manager, API kind of things
16:51
also have some plugins like you know they call it uh adjust for those kind of plugins in a studio
16:57
you can you can pull it and you can this so there are a bunch of things available for testing
17:03
okay now the crucial part of the talk right uh is it really cost effective or yes
17:09
do i have an example for it yeah i do okay how it looks like real example
17:16
let's talk it take a scenario so i have a i need to build i got a requirement that
17:24
of course this requirement when i when i talk about cost of course our example of course gonna
17:31
be that the person or the uh you know company is is fairly new and you know they want their cost
17:38
cost effective routes of getting a premium one And they want to go with maybe free know free or maybe to extend D when they call it there are some charge it not to be
17:52
quickly free in the plan and what they want is that they want to build a
17:57
cellular okay or timer so example they want to send mail to all the for example
18:04
it's a startup you know just maybe five people right so they want to send some enough task hey guys this task for you maybe daily 9 a.m whatever so so this is a requirement they
18:16
have and of course you know it needs to run otherwise how it will take automatically the
18:21
the next day nine right it has to run continuously right so and of course as i said the company is
18:28
fairly new um it does it it does not it can't afford the over budget and you know it's trying
18:33
a free tier or maybe D1 they call it a bit of price, you can't afford, you know, basic
18:40
standard or of course premium is not an option anymore. So, suppose this kind of setup you have and you created a job and, you know, here things
18:53
goes, things become interesting that, you know, in order to have a continuous run, you
19:02
need to basically however always on feature okay otherwise you can't run and
19:08
you know what this always on feature comes with minimum basic plan it doesn't
19:15
come with either free or D1 so you have no choice now you're you're stuck right
19:22
so you can't you can't have a bad job because you you did not buy for either
19:28
basic or standard or premium tier you are just free or maybe to the max even
19:35
and then you can go to as you see the pictures it gives you that you know you
19:39
have always on and for always on if you go to Microsoft Docs will find this
19:44
coming to you over there it will say hey your web app can be time out after 20
19:51
minute of activity if you don't do anything and you've got to have always
19:56
on and this feature is available on what as I said basic isn't that in Premiere
20:01
forget about f1 or D1 okay so what next for the savior so see there is function is your bedrock timer function that they're
20:16
not sorry is type over here so it's your function and a function okay web jaws was
20:22
the previous one where you we couldn't got a stuck and you can't really because
20:26
you you did not get up you know the premium tier so so a job function timer
20:31
from a timer timer function of a job function is a savior for you you can have
20:37
a shared expression similar with that job and we're good to go we'll see in a
20:42
demo in a while yeah let's jump to the demo now guys I hope I have time so yeah
20:48
So let me share. Simone, you're there, right? Yes, I'm there
21:00
Okay. Let me... Simone, you're there? Yes, I'm there. I think I just shared the deck, not the entire screen
21:09
So let me stop and quickly I'll open up for demo actually
21:14
Okay. Mm-hmm. So screen. Entire desktop. Where is that? Oh, is this
21:23
The first option. Right? Okay. Yeah. Cool. Yeah. I'll go with this
21:31
Learning new tools every day. Yeah. Here we go. So my machine is visible, I guess
21:39
Is it? Yeah. Your portal is visible. Oh, nice. Yeah. That's what I wanted to show. Okay
21:47
I get in just a time I get two demos before this call
21:51
So one is to talk about the constraint that was there. You know, that is kind of a, they said
21:57
I chose the basic tier or the free or shared tier and how I can be blocked if I don't use
22:05
in this case the function, right? So as you see here, this is my web job, you know
22:11
and it is pending restart. Why so? Let's go to log. okay select this and go to logs and you're gonna see what I just told you okay
22:27
what is saying saying guys forget this can run why because it doesn't appear to
22:34
be having this always on and blah blah and looks like you this feature is
22:41
available only in basic is in here and premium right forget about the shared and
22:45
free you you you it's not for you simple okay how I can look into this right so
22:53
let's go a little deeper so this my app service and what I'm going to do is
23:00
let's go on overview and and go to your plan as you can see it is free right it
23:09
it is free and if you go to configuration here, so we go there, it doesn't come in here one sec
23:19
So it comes into here configuration. And if you go inside general setting where
23:28
typically it's allow us to change this thing, look at here, it is simple for me
23:34
The reason is I'm a free tier guy in this case. Okay, so it is not for me
23:39
Then what is there for me? This is savior for me. So savior for me is Azure function and let's quickly look
23:47
Oh, I need to, what happened? Let me log in again. Okay
23:53
So the second example talk about how we can do this. How can I achieve this with Azure function
24:00
So I have, you know, I have, let me go to my own sources
24:07
I'll show quickly to you. So I have two app service. One is for, I just showed you
24:14
and another is for this one. So this app service, and this service again
24:20
has a free only, if you look at, how you can look at, let it load basically
24:26
So it is free. Come on, so yeah, see it is free only, okay
24:33
It's free tier. So how can I make it work in this case
24:38
and don't lose my requirements so this is my app uh is your function app service and this is the
24:46
app i created timer function okay when you create a timer function it will let you define how exactly
24:53
you want so i was showing you you guys the template so there are a bunch of templates here
24:58
and currently we're interested in this one timer so i if i can select timer so here it will let me
25:06
choose what schedule and the name so since I already have so I'm just going
25:12
to cancel I just want to show how it looks like okay so it is enabled let me
25:16
double click here and go into more details of how it looks like so as you
25:21
see it is okay so the CRN expression as you were seeing it was like every five
25:28
minute runs okay so I think maybe few hours back I spend this so let me go to
25:36
monitor I should have some message coming in oh yeah I ran bunch of times I
25:41
think it is running a bit five times so as you see look at the time guys here so
25:46
yeah so it is it has run 14 so I think it is taking UTC time here so I guess
25:54
what's UTC right now okay whatever so it is looks like it ran 419 59
26:05
basically 420 okay so now it will run as you see it is running every five minute
26:09
so and and it is running that's what I wanted and what message it is printing
26:18
hello Prakash it's your function timer so it is running actually every five
26:23
minute in in my free tier so it is doing my job whereas if you look at this guy
26:30
this is the job and it is saying hey you are not eligible you need to have this
26:34
is premium right Manually it can run yes And I run myself manually just for testing So if you look at some message will come in the down somewhere that you know hello Prakash Yeah hello Prakash came in but it was a manual run It can run automatic because always on is not on and always not on has to have this kind of your plan Whereas in this case timer case you don really need and you can go ahead or work around this case with
27:05
with Azure Function and timer template or timer type of Azure Function
27:11
where you can have a pre-tier and you can do the job, right
27:15
So we wanted that, you know, the requirement was the timer function
27:19
If I come back to my deck, what was the requirement? The requirement was that, hey, I just need, you know
27:26
a scheduler, I need, I'm in free tier, and, you know, I want continuously run
27:33
So basically every five minutes is fine. I can even reduce to one minute, right
27:37
So it is running continuously. Okay, so who said to me, so the function timer job saved me
27:44
So yeah, that's good. That's what I wanted. Okay, since I have time
27:51
so let me also talk a little more on this. I had some slides there for remaining time
27:57
So we'll talk more about the function. So demo is covered. Now let's talk about some little more deeper architecture side that given the Azure products
28:10
if I have been tasked to, hey, Prakash, you just come up with some kind of solution architecture
28:17
and you got to use Microsoft products as much as possible. And you also try to have a Azure function somewhere embedded and I need to come up with architecture
28:29
And these are some of the products or whatever you call it, capabilities or whatever you call it, I am about to use
28:37
So how I can proceed about it? So let's build some context here
28:44
So as an architect, I need to look into different things that how my sequence of communication is going to be
28:53
and what different building blocks needs to be there and what will be stored at what time and what product will have those
29:04
capabilities all kind of you know I need to have a big picture so I started
29:10
having those those basically what I call scenarios I put together it so how is
29:19
how gonna look like so you should be existing the web app in browsers and
29:23
and will have a signed feature typical to any e-commerce site I need to show
29:30
some product images of course I will be interested on putting them on or taking
29:37
them from CDN for faster access and yeah you know I should be able to query or
29:46
search different products and I may be interested on storing them into the
29:51
SQL database or SQL Azure in this case. What else? I want a product when I click
30:00
it should go to some kind of a queue so that it is there
30:05
and I can just keep shuffling it if I want to remove or add
30:09
those kind of thing. For performance reason, I may want to cache some data so that it is not going to
30:17
the back end every time and response is good. this could be capability
30:23
I want some logic to be built in so that when I click on pay order
30:27
or pay, all the items in my bucket or bin are based on the rates
30:36
or taxes, whatever. It is giving me the final amount, right, net price
30:43
And of course, this should also be capable to talk to some payment provider and upon
30:48
successful it should be able to make an entry into some persistent storage maybe database again or if
30:55
it is failure so the failure notification or the cause of the failure should be also be noted
31:01
somewhere so this is my requirement now how I'm gonna you know plug those requirements and put
31:09
together okay so usually comes in a browser of course there's a first
31:17
section CDNs storing my static images for for faster retrieval I have MFA for
31:28
extra security then I want to have basically so if you look at the
31:36
sequencing so that could be that you know then there's only even be login
31:40
right so you want to sell something you know any any any categories suppose I
31:46
want to get a phone so I'll be searching some model or some you know some phone
31:50
or brand so it should have it should load some data right so it will make a
31:55
maybe some search call and it will bring all those products of what my what I put
32:00
together in the UI so it needs to you know maybe typically it comes from
32:06
persistence so as initially at least literate passes but initially it comes
32:09
from back-end so SQL database can be the good option here how blob is coming so
32:16
blob is coming like you know I'm just keep playing about different items back
32:20
and forth I'm not sure maybe I I'm going to use our phone to to buy so maybe I'm
32:25
suffering like initially selecting Sun and the leader something else like that
32:30
I also want to cache so that my next response, if I'm searching something similar, it is
32:39
not again coming from backend, it is kind of returned from cache
32:45
Okay. I want some logic to be there so that when I'm clicking on paste, it is able to calculate
32:56
my entire amount and it is talking to or able to talk different like provider it
33:05
could be up I could bring thing else the bank or debit or whatever and in the end
33:12
it is also making an entry to the percentage stories like for example
33:16
sequel in this case so that I have a you know reference that what happened to
33:21
that order whether it's got success or failure or yeah so this how so how this
33:29
engine function is coming so if you look at it right it is what where you have
33:35
basket and it is it is calculating each line item on that basket and it is it
33:41
has the logic to get the the rate prevailing rate on certain if it is a
33:48
global sites of certain country and taxes and maybe discount, right? All those calculations can be put together into the function
33:56
Okay. Because it's like a state less, it will change based on the user
34:01
It is not something, you know, it will be like what you've chosen on your, your
34:06
side and it will give you the, the, the amount based on what you selected or what
34:12
you're buying rather. And so, you know, it can, it has the logic built in and it can give, give
34:18
the final amount that hey this is what you have you're buying and this is your
34:21
final value right so this kind of so you can be put together into into function
34:26
and also you can make a call to the to the payment provider and and has ability
34:31
to you know basically store the response of that payment provider into the
34:38
database in this case okay I have I hope I have time yeah I do a minute so I
34:45
I think I'm on time so guys these additional resources I use myself to
34:49
learn and on this so you can also find you know in these slides can be shared
34:56
later so you can if you want to can so thanks again this time I had and I think
35:02
I was able to meet the time and for any additional queries guys this is my
35:06
editor handle I generally able to respond to the faster than other
35:13
communication so use in case you want to talk to me later thanks thanks again for
35:19
decisions and having me see one over to you
#Distributed & Cloud Computing
#Networking