Cloudwatch is a cloud based monitoring service to monitor our AWS resources. It can be used to monitor all sorts of information about the services, like if the costs are getting too high, but also how much computing power is used, how much network bandwidth has been used, etc..
It can be very useful to know what is going on with all the services. Especially when for example a customer is claiming that the server is slow or cannot be accessed at certain times.
We commonly use cloudwatch to monitor the memory usage, disk space, load average, etc..
In the cloudwatch service, things that are monitored are called "metrics". By default there are some metrics available for EC2 instances, but they are not extremely useful when it comes to finding out what is wrong with a server instance. So in order to properly monitor the resources, we have to add some scripts to the crontab on the server.
Amazon has already made some perl script to be able to monitor the memory usage and disk space usage. The instructions on how to set up that script can be found here:
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/mon-scripts.html
To monitor the load average, we need to use another solution. We can do that by adding the following line to the crontab (assuming we are using AMI Linux and the aws cli tool is installed):
*/5 * * * * aws cloudwatch put-metric-data --metric-name load-average --namespace "Linux System" --value "$(cat /proc/loadavg | awk '{print $2}')" --timestamp "$(date --utc +%Y-%m-%dT%H:%M:%S.000Z)" > /dev/null 2>&1
This will send the load average of the last 5 minutes to the server, every 5 minutes.