Amazon CloudWatch (Part 2)

Kasun Dissanayake
5 min readJun 7, 2020

Now you know what is Amazon CloudWatch, What it does, and it’s operations. But to understand the capabilities of Amazon CloudWatch completely you should know about 2 important points of Amazon CloudWatch. Those are,

  • Amazon CloudWatch Events, and Charts.
  • Amazon CloudWatch Logs.

Amazon CloudWatch Events

Amazon CloudWatch Events is a part of Amazon CloudWatch which delivers ‘a near real-time stream of system events’ that allows you to ‘monitor and respond to the changes in your AWS resources’ by means of rules that route events to one or more targets.

Now let’s look at a few concepts related to CloudWatch Events.

  • Events — An event indicates a change in your AWS environment. AWS resources generate events when their state changes. Represent small JSON blobs. (Let’s say you terminate active EC2 instance. This EC2 instance has changed to Active to Terminated.)
  • Rules — Rules evaluate incoming events to determine if a threshold or out-of-bounds scenario exists. If yes then the event is routed to target.
  • Target — A target process the events that have been passed from a rule. A rule may invoke multiple targets.

Usecase

Let’s see Amazon Cloudwatch Events better with the help of use cases. Let’s create a system that closely mimics the behavior of Dynamic DNS. Update Amazon Route53 records when Amazon EC2 instance changes its state. Use services Amazon CouldWatch Events, Route53, and AWS Lambda.

(What is Dynamic DNS? Let’s say you want to access the internet at home. Then Internet service provider assigns you an IP address. This IP address changes because of this internet provider uses a different kind of online systems. It might be difficult for you to use this IP address to other services like webcam security cameras and etc. So this is called Dynamic DNS. What Dynamic DNS does is it assigns a custom domain name to an IP address. And this Domain name is automatically updated when the IP address changed. Dynamic DNS is a service that automatically updates a name server in domain name system.)

Amazon offers you a similar kind of service called Amazon Route53. We are going to update Amazon Route53 whenever the Amazon EC2 instance changed.

So in this use case, we are going to update Amazon Route54 whenever the Amazon EC2 instance changes its state. This use case actually works in this way. So whenever an EC2 instance changes its state Amazon CloudWatch event becomes aware of these operational changes and it triggers a Lambda function. This Lambda function uses different kinds of information regarding the instance like public and private IP address and it updates a record inappropriate Route53 hosted source.

So let's say you have an EC2 instance and you have terminated the instance So Amazon CloudWatch events become aware of this and it triggers a Lambda function and this Lambda function deleted the record from Amazon Route53. Similarly, if you have created a new instance again Amazon CloudWatch events become aware of this and it triggers a Lambda function. This Lambda function creates a new record in Amazon Route53.

Amazon CloudWatch Logs

Amazon CloudWatch Logs is used to monitor, store, and access log fi9les from AWS Resources like Amazon EC2 instance, Amazon CloudTrail, Route53,and others. Log files are nothing but a detailed record of events that occur when you are using an AWS environment.

You can view your log files on your own server as well. In Windows search for an app called Event Viewer and Select that and click on Windows Logs. Then select System then lists of Log files shown to you. All the details regarding that log file will be shown there. When we chose a particular log file you can see used keywords, Login times no of users log in, and various other details. Similarly, there are log files in your AWS environment as well. So you can consider these log files as a data repository. Most of the metrics are generated from this log data. So whenever a metric has generated a part of data is retrieved from this Log Data.

So you are designing a matrix according to your like and you are choosing a part of data from log data. So basically this log file is a primary data storage place.

Let's try to understand CloudWatch Logs with the help of some features.

Firstly you can use Amazon CloudWatch Logs to monitor your application and system log files. Let's say you are trying to deploy your error full application on Cloud. In this scenario, you can use ClodWatch logs to keep track of your errors and send a notification to you when an error increases a certain threshold. So that you can make avoiding errors here.

Then you have Log Retention. By default, Logs are kept indefinitely. But Cloudwatch provides you an option where you can set the period between 10 years to 1 day.

You can use ClodWatch Logs to store your log data and highly durable storage and in case of system errors, you can access data from this storage.

Then you have DNS Queries. You can use ClodWastch logs to log information about the DNS Queries that Route53 received.

Now let's have a look at few Concepts regarding Cloudawatch Logs.

  • Log Event — This is just a record of activity by resource or application being monitored.
  • Log Stream — A log stream is a sequence of log events that share the same resource.
  • Log Group — A log group represents groups of log streams that share the same monitoring and access control.

UseCase

In this use case, we are going to use Amazon CloudWatch logs to troubleshoot the system errors. You can see that there are 4 instances here and the CloudWatch agent monitoring all these three instances. So what CloudWatch agent does is it collects custom level matrics from all these EC2 instances and then all these metrics and logs collected by the agent store in the Amazon CudWatch Logs. Amazon CloudWatch Logs then continuously monitors these metrics as you can see here then you can set an alarm that will send you a notification when some sort of error occurs in the system. So whenever you received a notification saying that some sort of error on your system. You can access the original log data which is stored in Amazon CloudWatch logs to fond the error. So this is how you can use Amazon XCloudWatch Logs to troubleshoot the system errors. So basically looking at original data you can solve the error faster.

So this is all about Amazon CloudWatch. I hope this tutorial is informative.

Thank You!

--

--

Kasun Dissanayake

Senior Software Engineer at IFS R & D International || Former Software Engineer at Pearson Lanka || Former Associate Software Engineer at hSenid Mobile