So here we are. Another talk. This time joining micro services with serverless
very interesting topic of course you can ask the questions and ready to talk on
our bot and that's it enjoy the talk thank you
this is weird I'm sure this isn't I mean I'm not the first person who said this
but you know trying to my ears and you know right now it looks like a
movie from 1960s where they show you the future and everyone is
like headsets connected to PCs with cables and so on so forth that's
interesting okay so this is a talk about microservices and the serverless framework
just to get your you know to understand the audience a little bit I'm gonna ask
a couple of questions I just need your hands anyone who did anything with the
serverless framework two three all right cool
anyone building micro services cool for the people building micro services what
are... that's not a question.. what do you use as an Orchestrator okay that's
interesting or do you have micro services in containers in production that
makes sense okay great so let me start what is serverless I am very very
excited about this thing the serverless and the functions and the microservices
a little bit as well but the the idea when the cloud came out for the
first time right have you heard the word about the cloud they always you know
explained it like you pay for what you use right that's the first description
the second is you can scale indefinitely right because the cloud I mean depending
to the vendor for some when it started it was essentially scalable VMs for
others it was you know platform as a service offerings for some it was
infrastructure as a service offering so the vendors had different the concepts
of scalability in different layers depending on that the pay-per-use wasn't
actually without a commitment so imagine you are going into the cloud and you've
got to have a deployment the first thing that most of the time you need to tell
the vendor is how big of a pool you need right they do have pricing tiers at least.
If I'm going to be paying for what I use why do we have pricing tiers why
do you want me to select how many units I am gonna use I might be able to use
how much scaling I might need why do you want me to predict what my footprint
will be in the cloud and if you ever build a software-as-a-service type of
solution you know how hard it is to come up with a pricing model you need to
protect yourself right there might be customers imagine imagine a mailing
service write a paid mailing service that asks you how many emails are you
gonna send per month that never happened because that ecosystem those
services they come they did come naturally no one really planned for it
but it's the right and the good scalability of the platform across tenants
when you are using an office 365 account where you're using a Google email
account you are actually a tenant you got an email address you are a customer
of that service you're a tenant and no one asks you for how much email you
might send you might send millions you're gonna pay the same amount you
might say just one per month you will still pay the same in the underlying
infrastructure there is some balancing going on. Not every customer is making
money for the vendor some of them are making very good money but some of them
are actually not the customers that we really want to have because we're losing
money because of their usage but this didn't happen with the cloud staff
because when there's wanting to protect themselves so we ended up with a cloud
where we had to tell vendors what type of a, you know, scalability we need how
much of a commitment we can do and then we could scale according to the limits
that was defined for us the serverless thing when we say
pay-pre-use. imagine and actually it's a reality
imagine you are paying for the cpu time you are paying for the CPU cycles you're
using you will see units like gigabyte per second you are paying for the memory
you are using every second for for a second and then you are using for the
CPU time you are using you are paying sorry so that's the unit of consumption
that's the unit that you're gonna be charged so that's what I call the true
cloud because when I go out there and I have zero consumption I'm not gonna pay
and I'm gonna pay only for the compute resources that I use and I don't need to
tell you any type of commitment I don't need to tell you that hey I need to
start with two servers two instances first because I need some kind of a
failover I'm gonna do load balancing I don't need
to here's my code make it work make it scale I don't care about anything else
just tell me how much consumption I get the CPU time the memory I used and I'm
gonna pay for it so that's like the dream at least I wanted to have from the
beginning and it's happening so there isn't that I guess a session without the
quote from Martin Fowler right so here's how he defines the serverless
architecture applications that are either called back-end as a service or
function as a service. FAAS is the most more popular I guess
and he uses the word container that doesn't necessarily mean that we got a
container is in the sense we understand today but you know any type of isolation
in the sense of container in the future there are you know, I mean, I think
it there might be a future where a container might one to one map with a
function but it might be overhead as well because
a function is much more simpler than a container a container for me means I got
dependencies I'm bringing with myself or at least I'm aware of my dependencies a
function needs to be you know higher than dependencies what does that mean
there's a platform your code works on it or not that's it you don't bring any
image with yourself you don't bring a container definition with yourself it's
just the code it just needs to be the code it needs to stay simple at least
that's that's what I think but it looks like there might be you know some
services that more you know converge the two and come up with the concept that
you can define a function with a container and deploy with that so these
are the three items I want to make sure I go through these so, worry and care
just about your code pay-per-use measuring units at maximum optimization
that means just the memory, just the CPU no commitment hardware provisioning I
wanted to you know say are you sure because some of the you know vendors out
there are still asking for some type of a commitment especially if you go with
the micro services path and if you don't have a managed service by a vendor you
are still going into the underlying layers and you still need to manage your
pool let's say you get thousand different micro services and you are
fluidly right orchestrating them within the pool so what if the total amount of
resources you need increases you still need to know about it you'll still need
to monitor it and you still need to scale your underlying infrastructure if
it's not managed by the vendor. Does Serverless equal to micro services I
know this a little bit in conflict with the session title it's like having a you
know every relationship is not a marriage and marriage is a relationship
so you might micro services and serverless
are essentially two different things when you go serverless and you are
forced to build functions you are forced to go a little bit into
the microservices space as well because of the separation because of the
fact that a function needs to be a very small unit of working functional code
you are being pushed into the micro services world a little bit
besides the orchestration part but what if I have you know 500 functions that
are all you know connecting to the same database is that micro services that's
an open question so it's not exactly every function is not exactly a micro
service the size might be micro but is it designed with what we call micro
services it depends you might build a big behemoth you know function that runs
for five minutes for example on azure the timeout is 10 minutes for a single
function it should never happen a function should never run for 10 minutes
but it looks like people are doing it so they have it so what is the serverless
with this specific you know Icon that's the name of the framework that's that is
a little bit more confusion I know the concept of serverless is one thing and the
name of the framework is serverless as well and from this point on we are
gonna talk about the serverless framework not the concept and the idea
of this framework is to you know again that was our dream the cloud or you know
any type of platform we are developing today we always want it to be vendor
free right we don't want to build specific stuff for a particular platform
we want to be able to move from one platform to the other and as you all
know cloud is kind of the biggest opportunity to vendor-lock customers
for these vendors imagine you are building an app and it's just
just about the storage you're a building in a PAAS, SAAS app right on AWS and
you're using the storage system right it has REST API and yeah
you built the app to talk to that can you move that to Google cloud functions
sorry leave the functions can you move that to
Google Cloud can you move that to Azure no because the storage api's are
different so even with that slight storage API usage you are actually
locked into the vendor and every day you built your app when a platform as a
service offering you are using their api's and you are using you know their
services and you are being vendor-locked
it gets harder and harder to move
away from that provider so the serverless framework is trying to
solve that problem the serverless framework is trying to
solve the problem of requiring to migrate a function from AWS lambda to
Google Cloud or whatever right and the platforms obviously they have
additional tooling and so on and so forth it's not just you know trying to
it the whole framework the only purpose isn't just to make it you know vendor
free but that's the that's the biggest point in my humble opinion and these are
the platforms they support today so when you build a function you can deploy your
function to all these platforms with the same API set with the same comments you
use the same developer tooling you don't need to know don't quote me on this you
don't need to know the specifics of the platform actually you do but we will
talk about it oops so this is how you install it you go to you know a terminal
npm install' then you call serverless login and you're gonna type in your
credentials and then you can create a new project from scratch and you say
serverless deploy you deploy your function and here you go you got the URI
if it has an HTTP event trigger if it has an HTTP endpoint then you can
trigger it through the HTTP obviously not every function has to have an HTTP
endpoint but in this example it has so you can hit it from HTTP 80
so those are the high-level comments so let's go and start doing some stuff okay
I'll lost my mouse because I got a secondary screen what's going on okay
let me minimize this so I do have a backup in case things doesn't work so
I'm here let me zoom in oops okay so the first thing that we're gonna do is to
create a new project oops
I'm sorry about this okay let's make it as big as possible
okay so the first thing that we need to do is to create a new function and I'm
gonna go here I'm gonna say here you go
oops serverless create template is gonna be AWS nodeJS and I'm gonna just give a
folder and hit enter there we go we created this one and let's go in it
and see what we got there so we only have two files that's it
we got the handler js and then we got a yml file let me open up vs code to
see what we got in there so this is the first file that you're gonna need to you
know edit in case you want to change something we call the service the
service name we give the provider right for this case we're gonna use the AWS
provider to connect AWS obviously and the runtime is gonna be
NodeJS that out so when I go down here you're gonna see that we got some
additional options and one of them is the function itself and the handler so
here I'm creating an hello function and the handler is in the JavaScript code so
let me show you what handler that hello is which is the this handler.js file
and there we go so this is simply putting out the whole
event data plus a message out there so obviously right now we didn't define how
this function is going to be called right so let's go into the yml
down a little bit I'm gonna zoom in here it says we can define events so these
events do differ per platform so AWS has a different set of you know events that
you can use to kick your functions Azure has different events most of them
they go together but the naming is a little bit different and this is the
space where you start doing some you know vendor specific stuff it was
it's not supposed to be that way but it is so I'm gonna comment out a couple of
lines here this one I'm gonna have events I'm gonna have an HTTP event we
will do HTTP GET and the path will be hello okay so that's it the function
itself I'm not gonna change it um let me open a terminal here or down there so
now I'm gonna deploy this thing into AWS the first time you call this server
let's deploy it's gonna ask you for all the credentials obviously that's
something I did set up prior to this session you go ahead you get the
credentials from your management portal on AWS and bring it those down here so
that the the runtime can talk to it but once you do that it's just saved locally
and you can keep using it so right now we are deploying a function with an HTTP
endpoint and I guess we're just we got it so let me copy this thing and hit it
so I'm gonna hit this send an HTTP request and I got it back where is it
yeah there we go the input is the event and this is the
message so right now we actually deployed an HTTP service you can call
this micro service or whatever you want this is a scalable you know function
that exists out there and we're just gonna pay for the CPU time that this
function has used and for the memory that this function has used and
obviously the bandwidth in this case because it has an HTTP
endpoint but that's it you don't need to worry
anything else because we're just you know out putting a simple message but
the idea is that's done all of these worries about scalability and all that
stuff is is not here so I'm gonna change our function a little bit for that I'm
gonna copy some code let me copy it from here this is pretty simple what I'm
doing is I'm getting the query parameters I'm gonna pass in a counter
parameter and just increase it that that's it nothing nothing too complex
I'm gonna save this in order to deploy this again I can do a full deployment or
I can just deploy a single function in order to deploy a single function I say
function oops hello and deploy it so this makes sure you you only deploy
the specific function you don't deploy the whole projects if you have multiple
functions so let's go ahead and call this again this time we are gonna say
counter equals one go ahead see the response oops
didn't I save it
let's go with a full deploy oh it gave an error because yeah I said hellow
with w anyways that was my mistake it's really tough to you know manage the
mouse on a on a table which is not flat it's just going down and down
whenever I leave it okay come on there we go
let's send another request and hopefully this time we got to okay good so let's
do something else I'm gonna close this thing go to my services I will go here
and this time I am gonna do you know a deployment with another provider right
so I will go here and say hey great me template for Azure NodeJS call it
you know daronserverless app and create it
there we go so this time I created the function for a for another vendor and
let's see what we got in this case it's pretty much the same so we again got the
handler do you see the difference between the two so let me give you some
spoiler this was another slide actually but let me show you what I mean so it
yes these are the handlers for the four platforms I told you that it's gonna be
vendor free right it's not happening it's very close but it's not happening
so the handlers are different and not just the handler the yaml is a
little bit different as well for every vendor so you might say like
it what's the point of this whole presentation this is as close as you can
get this is as close as you can get at least right now and that's very close
oops so this was a spoiler I was supposed to show this a little bit later
so we got a function in here as well it's just outputting the the string into
the body let me look into the yaml this time I got the service defined the
provider is Azure, the data center is there and there's a plugin
Serverless Azure functions that plugin helps with the compatibility with Azure
because the serverless framework by default doesn't have support for it but
it comes with a plugin so I I mean it adds that plugin down there and then
with the azure template what's different is this you know by default it comes
with an HTTP endpoint I don't know I why they did it this way but there is an HTTP
endpoint defined it says events HTTP true then there is this vendor specific
setting who says authorization level is anonymous because you can have you know
keys for every HTTP endpoint in Azure by default it has three different layers
of authentication you can have an admin key a function based key or anonymous
so you need to you know somehow specify that in the yaml and then the direction
direction is out and the name is rest this is gonna be the parameter that we
are going to use in the function to access the response so the name that we
give here is actually the variable name that's going to be used when you are
coding the function so right now it's RES and it's an out parameter that
means here okay you can actually give another parameter and save as and that
parameter will one-to-one map to the response you don't need to go through
your contacts rest if you want so let's copy past some code to make this a
little bit more interesting as well I'm gonna get some code from here from my
cheat sheet and there you go so what I'm doing here
I am I'm gonna do an HTTPS call to AWS lambda from the Azure function I'm
gonna send the parameter to that obviously I need to I need to copy the
the namespace let me get that here the URI where is that oops this is the one
I need there you go I'm gonna send the parameter that I get from the URI so I'm
gonna send the counter to Azure functions it will get the counter it
will just hand it out to AWS Lambda then call an HTTP API on AWS Lambda
Lamda will do we will increment the counter and send it back and I will
just you know parse it and put it into the body that's a lame demo but yeah
it's just to show how it works so let me close this one we do have a couple of
additional things to that we need to do about this one we could before we can
make it work so let me go here and put the mouse down here so that I can write
okay okay the first thing I need to do is to go into the folder and call NPM oh
by the way in order to be able to use the HTTPS I need to define it in my NPM
dependencey list let me copy that one here as well
there we go save it now I'm going to go back here and call NPM install
okay
this is just in case you want to run it locally the same NPM install
command will be run by the deployment agent in Azure when we deploy our
function to it okay serverless deploy so this time oh it's not gonna work because
there it's gonna give us an error because I didn't define the environment
variables holding the credentials for my Azure account and I can't find it in my
notes which isn't good okay I found it oh this is oh yeah this is gonna work
sorry this is the PowerShell session that I already defined credentials so that's
good so right now is creating the deployment user it's gonna copy the file
and so on and so forth and we're going to have an HTTP endpoint that we can use
to to hit the function on Azure and it's gonna hit the AWS lambda and then
respond back
all of these packages that is creating those are under the dot serverless
folder in the function folder and you can even version them and you can even
roll back to an older version of the function and these are part of the
Serverless CLI I do have the commands in the slide deck that I'm gonna show okay
there we go so this should be the endpoint let me hit that
you got it okay I sent one I got two in that increment happened in
in AWS lambda so it looks like I deployed you know functions to two
different providers in a couple of minutes and I got it I got it running so
one other thing that you can do is to start monitoring so let me go and say
for example serverless logs
Oh
okay serverless logs and the function name is a parameter right now
I'm connecting to Azure and I got real-time telemetry coming to me and the
telemetry is actually all that you write output into the log so as you see
there's a couple of you know places it looks like there's only yeah okay
there's two here's what I log something and here's where I logged the
full data into the logs which doesn't make sense but still so right now I'm
gonna call this function and we did connect to Azure to the log streaming
service through serverless I don't need to worry how it works with Azure or
you know AWS I just use the serverless framework and then I'm gonna send an
HTTP request from here and when I go back I see the trace so you can do some
tracing you can do some logging without worrying about the vendor how how the
vendor implements it okay
so this is overall that the full code I just wanted to make sure it's part of
the slide deck as well when you guys get it so that you have it so this is kind
of overall the full definition of this function if you're going to deploy it
to Azure it has you know the request response staff and the definition says
it's going to have an HTTP event in addition to that actually you can have
different events like a timer trigger right let me go back and copy that one
as well actually let me copy the full thing and
if I go here and so I'm gonna pass there just to show it to you this is supposed
to be in the YAML file let me go into the YAML and in the triggers in the
events I'm gonna say hey I do want to have a timer trigger okay if I want to
access the timer trigger through the code the parameter name will be timer
object it will pass on to the function so that I can you know work with if you
figure out how many times it's been run the last execution time so on so forth
and then I have this this cron where I define how many times the function is
supposed to run in this case it's gonna run every five seconds so this is just
another event besides the HTTP event so when I hit the
API through the HTTP it's gonna run if I don't it's gonna run every five seconds
anyway so I am having multiple events kicking this function you can have the
same with AWS you can have the same with all these vendors obviously it did the
events and all these triggers they do differ per vendor so you're gonna need
to look into the documentation to figure out what's what you exactly need to type
into the ammo and this is the AWS lambda that I just use as a part of the demo it
has an HTTP event as well and we hit that
to increment the parameters real-time logging is something I already showed
you so I'm going to skip this one and these are the additional comments that
you might want to keep keep in mind the serverless deploy function deploys
just a single function instead of the full thing because if you got you know
hundreds of functions you don't want to deploy it every time you can invoke a
function locally or remote it depends how you set it up you can call the
serverless info give it the function name and it's gonna invoke you can actually
have a test JSON file that's that's a you know built-in functionality if you
have a test JSON file in the same folder when you invoke the function whatever
happens to be in the test JSON file will be sent over as a test data to the
function so you can have some dummy data in the same folder to be able to
simulate instead of like me going into fiddler and go sending HTTP request
serverless metrics this is available as far as I know I might be wrong about
this but this is only available for AWS this gives you how long it took for the
function to run how many times it's been called and so on and so forth a couple
of you know metrics that you can look into and roll back again this only works
with AWS from what I know you can when you do a deployment you can give it a
version name and then it's gonna be locally you know saved in here let me
show you in this folder like this and from that point on you can you can
essentially roll back to an older version of the app if you want to
yeah it looks like I finished a little bit early so any questions and just
raise your hand if if you have a question none did I do a very good job
because and everything was crystal clear okay thank you yeah thank you for being
here and enjoy the rest of the conference
Không có nhận xét nào:
Đăng nhận xét