Introduction
AWS CloudWatch is Amazon’s monitoring service which provides logging and alarm functionalities and used with almost every AWS service available.
Monitoring helps us drive to improvements of the experience for our stakeholders and architecture.
CloudWatch monitors AWS resources using matrices, alarms and alerts. Using the matrices, you can have alarms fire (e.g. CPU utilization is at 85%).
AWS Cloudwatch provides metrics about your various AWS resources and allows you to monitor the performance and health of your applications. It collects data from your cloud resources in the form of logs or events and creates visualizations of these metrics so that you can see how all your resources are performing.
CloudWatch has a lot of features and use cases, however, our focus will be application logs and we will see how CloudWatch can help us with our application logging and monitoring.
I will be using .NET Core and NodeJS code samples for the demos, but principals are the same if you use Java, Python or other supported languages/run-times. Also focus will be on the application side, rather than infrastructure.
CloudWatch Integration
CloudWatch is integrated with many AWS Services. Following are few of the key services
- SNS messages (email, sms, lambda execution etc.)
- EC2 autoscaling
- CloudTrail (API calls to AWS account, you can setup CloudWatch alerts)
- IAM
- can control who can use CloudWatch.
- can use CloudTrail to monitor when requests are made to service using IAM credentials.
- Lambda (e.g. Run functions based on alerts).
CloudWatch has a lot of features, you can find more detailed and up to date information on official AWS Website on this link.
CloudWatch Metrics
Each AWS service has CloudWatch metrics that you can use. They are typically defined as basic metric. Each metric is a set of data points published to CloudWatch over a period of time that you decide upon.
Defined metrics are standard resolution by default (e.g. 5-minutes). You can adjust these defaults based on your requirements and use case.
Also if we change it to frequent updates such as 1-minute, 30-seconds etc. then its going to cost more.
Metrics can be aggregated across AWS resources within each region (its a regional service).
Metric example:
- e.g. CPU usage of an EC2 instance.
- Data points are published to CloudWatch based on a repeating time period.
Amazon uses statistics to summarize what that data actually is:
- SampleCount
- Minimum
- Maximum
- Sum
- Average
- others…
Only as you monitor, you will discover what is best statistics to use.
CloudWatch Logs (.NET Application) – Basic Logging
Lets see few examples in a .NET Core application which is deployed to AWS Lambda. You can check this post for more details.
I’ve added a Controller SamplesController with some actions for these demos:
In above shown method code, we are using built-in logger to log different messages. Now if we call this method when application is running on AWS Lambda, following log entries were captured in the CloudWatch log:
So, this is a very easy way to do application logging using existing logger and those logs will be automatically available via CloudWatch.
Logging Error
Logging error is also not much different, e.g. we can use LogError function as shown below:
This will result in the following log entry:
Here is another example, where application code attempts divide by zero:
and the corresponding logs are:
and one more example, where the controller action is throwing an exception:
As you can see that all these logs are captured in CloudWatch and these are very handy when debugging the application for issues.
Next, lets see a NodeJS example with CloudWatch.
CloudWatch Demo (NodeJS application)
Here is an example CloudWatch log for NodeJS flavor of Lambda function. Once an error occurred in lambda function, following is log information reporting the error:
As you can see that regardless of language run-time, CloudWatch logging works in similar way. Here is the link to Lambda function post.
CloudWatch Alarms
So, CloudWatch logs are showing us when some errors are happening inside our application. But how should we proactively notify ourselves when things like this fails?
One of the easiest ways to do this with Lambda function is to setup a CloudWatch Alarm that is related to these logs and the matrices that are generated from them.
CloudWatch lets you create and set alarms so that you can be notified or trigger an automated response action when a certain metric reaches a threshold.
We can start creating an alarm from CloudWatch dashboard:
Select metric
In Step-1: We can select metric by clicking the button. Here I’ve selected Lambda By Function Name and selected Errors metric:
Also in the Graphed metrics tab, I’ve changed the statistics to Sample count (other statistics are available) as shown below:
SampleCount is the number of data points during the period
Once metric is selected, we can configure it further (e.g. give it a different name, conditions, notifications etc.)
Conditions
We can specify conditions as shown below:
This meaning, if there is anything greater than 0, then let me know. We can also do some additional configuration here, but we will keep those defaults for now and click Next.
Notification
Here we can setup notifications:
we created a new SNS topic and provided email address where these notifications will be sent.
There are some other actions we can take from this step, however, we will leave these without change for now and click next:
We can then give it a name and description:
On the next screen, we can review the alarm and create it:
Subscription Confirmation
The alarm we created in previous section, will also setup SNS subscription:
This subscription needs confirmation. Following screen shows that I received an email accordingly:
we can confirm subscription by clicking the link in the email and following picture shows that subscription is confirmed:
and now if you check SNS subscription List, it will be shown as confirmed in the web console.
Testing
So, we have created alarm with Error metric, selected to send an Email via SNS, confirmed the subscription email and now ready to test the workflow.
Ok, lets test the setup by causing an error to the application (e.g. by using ExampleController methods)
This will result in triggering our alarm and following picture shows In alarm state:
and I also received following email:
So, when an error occurs in our application, we will be notified via CloudWatch alarm.
Create another Alarm
I’ve created another alarm to target NodeJS function and set it up in similar way (used Sum statistics for the alarm though):
Now, when an error happens inside this function, the alarm will trigger:
and we received the email as expected:
So, this was a basics explanation and couple of demos of how to use CloudWatch for our application loggings and monitoring.
We saw that our application code doesn’t required to be modified in some special way to support CloudWatch Logs. There are many other use cases and options available via CloudWatch and you can find more information on the CloudWatch documentation.
Summary
In this post, we learned that Amazon CloudWatch provides a reliable, scalable, and flexible monitoring solution that you can start using within minutes. You no longer need to set up, manage, and scale your own monitoring systems and infrastructure.
We saw how to setup CloudWatch alarms and then triggered those alarms when some errors happened in our applications.
Let me know if you have some comments or questions. Till next time, happy coding.
Discover more from Hex Quote
Subscribe to get the latest posts sent to your email.