hello world i want to go to the very
basics i want to talk about login and
monitoring using cloudwatch so i want to
start from the very start asking the
question what is cloudwatch cloudwatch
is the monitoring service from aws it
helps you to
keep your logs track some metrics
trigger some alarms it can also record
all the different changes in your
infrastructure so you can do things with
it like trigger lambdas if you have too
many instances running or
things like that it does all kind of
very interesting things so it's more
than just a login tool so cloudwatch
help you to bring the visibility from
your infrastructure into one place but i
think that's one of the biggest
challenges in aws to see what is going
on in all these different services and
clowatch is that place where you can
connect everything together we have been
using cloud watts to check the logs from
lambdas but you can also do other things
with it like check different metrics
in your
dynamo or even check the logs from the
api gateway if you enable all resources
will push some kind of information to to
cloud cloudwatch but specifically for
lambda cloudwatch is a great thing
because
when you have these execution
times that is very limited and ephemeral
and you want to respond as fast as
possible
you want that your login is not
consuming thing
kind of your execution time and when
you're working with lambda and
cloudwatch
lambda will be kind of streaming
asynchronously the logs to cloudwatch
and it doesn't consume any extra
execution time in your function and it
doesn't generate any performance issue
as with other libraries
external libraries from aws that might
generate some problems so that's why
usually it's a good practice to use your
cloud watch for logging in lambda and
then maybe use some external library to
see these logs or filter these logs or
something like that but
but keep the cloud watch as the login
place for your lambda so now i want to
show you
basics of cloudwatch let's go to
try out the metrics the login and the
dashboards
and let's go to the console to get that
done
let's start by creating a new empty
project this is going to be a very very
very simple project so just follow the
steps as always to create a simple
project with
aws node.js as the template we are going
to get into atom
and do some small modifications the code
is available in github but this is very
simple code so i just remove
all these comments
to make it simpler to read
and then i will create a trigger for the
hello function
so we can trigger this easily what we
are going to do here
after we create a trigger
is to do
a console log
because that's what we want to try i
will just change the message to
something and this console log will just
write hello
in the console hello was called so very
simple message so we will deploy now and
we will see what happens so we'll speed
up the deployment
and when we finish deployment i will
just trigger this
function with that url let's just
trigger it from postman so and we can
see the message so now we can go to
lambda in our account in aws in the
console and then if you go there and you
look for the function that you just
deploy
cloudwatch dev hello you can go to
monitoring
and there you can see the different
metrics for this function and to see the
logs you can just go to
logs this is one way to find the logs
and there you can see the logs for this
call and you can see hello was called
another way is a little bit more
straightforward but in a way not that
easy sometimes to find is to go directly
to cloud watch and then go to log and
there
look for the logs in the list of lambdas
if you have few lambdas then it becomes
very easy if you have a lot
like me this becomes very painful so
when you find your your lambda
then you can get into the log group
and then you can see exactly the same
another way to do that to see the logs
my favorite i will say is to see the
logs from the serverless framework
in your terminal so you just type sls
logs minus f the name of the function
and minus t to tail the log so this
if you keep on calling it will keep on
appearing if you don't put the minus t
it will just print whatever is there
another thing you can do with cloudwatch
as i said is to create dashboards with
metrics so i will just create a new
dashboard i'll put the name testing
cloudwatch you can put any name you want
and there you have an empty
dashboard and there you can add widgets
so these dashboards you can display in a
screen or you can have them open to see
how your service is performing
and when you add a new metric
you can look for the different
services
so we will just look for lambda
and then you can look by function name
and there is all the functions there
then i will just look for the serverless
cloudwatch and you can search so it
becomes
easier if you put the name of the
function it just shows that it filters
out the rest
and then if you see on the right you'll
start seeing some dots appearing
those are like the only time we invoke
this function so
there's only one but if this is called
many times then you will see lines so
you can define things like the order in
which these metrics are shown you can
also define the period that these
metrics are showing so either one second
of information or five minutes or one
hour and depending on that you are going
to see different things the period and
the statistics will make you see
different things so it's important that
you tune
your dashboard
to something that you understand
so if i put 30 seconds
the average it will be the average in 30
seconds but i want the sum
in 30 seconds
so it's a different value than the
average this case is the same because i
only call it once but if you have
multiple for some metrics the average is
good for others the sum is better and
then you have some graph options you can
put annotations you can
define what kind of
data you how you want to show the data
and that's kind of the graph then you
can add numbers those graphs that's
something i like
some things are
easy to see as a number
so let's just look again for the for the
same function
and then you can see
if i change the period
then the average just disappears
there's not enough data so now we have
it
one minute
and we don't have any data there so in
order to get some data what i will do is
i will trigger this function many times
so we can see in one minute
i don't know how many invocations we
have this takes a while
usually cloud watch is almost real time
so you need sometimes to wait a little
bit
now if i refresh this graph you can see
that there is some values and we can see
that there is some invocation and also
you can see now that we have called this
a couple of times so if i change the
period and i change the statistic you
start seeing things differently so you
have the amount of
i don't know invocations
in one minute there were four in one
hour
there's an eight
so
it's different
different values so
you can play a little bit with that but
always be careful on what is your period
and what is your metric because
sometimes those numbers can be deceiving
if you don't know
what you're putting there so
another thing you can do is you can
change the
period that you're showing the data
so even if the metric is being displayed
for every 30 seconds you can also show
what is happening in the last hour in
the last three hours in the last day or
then you can show what happened in the
last minute
or in the last 15 minutes
and things like that remember that you
need some data
this was the video for today i hope you
like it if you did give a big thumbs