CloudWatch Logs
CloudWatch is a service in AWS that allows us to monitor metrics for any other service in AWS. Here's their 2 minute marketing video:
That video doesn't go into any details, but it does list off a lot of features that cloudwatch is capable of.
Three big features of cloudwatch are:
- Logs: Collect and monitor logs from AWS resources, applications, and custom applications.
- Metrics: Collect and monitor metrics from AWS resources, applications, and custom applications.
- Events: Monitor and respond to changes in AWS resources and applications.
We will cover a lot more cloudwatch features later on, for now we're just going to focus on how to send logs to cloudwatch logs.
We will send logs from an application running inside an EC2 instance to cloudwatch logs. This means that we will be able to monitor and query the logs for an EC2 instance, without having to login to an instance with ssh just to view the logs.
We're only be scratching the surface of what CloudWatch Logs can do. But even just being able to easily access logs for a EC2 instances is a huge benefit! Any plan for monitoring an application should include easy access to the logs.
Sending Logs to CloudWatch
To send logs to cloudwatch logs from an EC2 instance, we need to do the following:
- Configure the EC2 instance to run your application.
- Setup an IAM role for the EC2 instance.
- Attach the cloudwatch agent server policy to the IAM role.
- Install the cloudwatch agent on the EC2 instance.
- Configure the cloudwatch agent to send logs to cloudwatch logs.
We will be setting up the qr code web app EC2 instance, and have it send logs to cloudwatch logs.
Setup The App
Do this on the instance that you already setup in the previous part so that it can access the S3 bucket and cloudwatch logs.
Once you've set everything up, the application should be working just as before and you should be able to view the application logs by running:
Or to tail logs in real-time:
Remember that you can show logs for any individual service with the -u
flag or all services without the -u
flag.