Showing posts with label cloudwatch. Show all posts
Showing posts with label cloudwatch. Show all posts

Monday, March 21, 2022

AWS Management & Governance - Cloudwatch




AWS CloudWatch Log Docs

AWS Cloudwatch is a monitoring service on the basis of metrics.

by default, when you create an EC2 instance, there will be a default monitoring at one of the tabs, but if you want to have your own custom monitoring (like using alarms or anything extra) you can use AWS Cloudwatch.


States of Alarm

In Alarm: - If the metric value is greater than a threshold value. E.g if you have set the threshold value of CPU utilization is > 50 and if EC2 instance CPU usage is > 50 then it is In Alarm State.

OK: If the metric value is less than a threshold value

Insufficient data: If the data which is collected by CloudWatch is not sufficient to determine the alarm then it is in an insufficient state.

EC2 Actions: These actions are performed on selected EC2 instances if an Alarm is triggered.

  • Stop 
  • Terminate 
  • Reboot
Ensure you have the IAM role: CloudWatchFullAccess.


To create a CloudWatch Alarm:

Step 1: Select CloudWatch Service.

Step 2: Click on Create Alarm Button

Step 3: Click on Metrics

Step 4: Select EC2 instance because we are monitoring EC2 instance.

Step 5: Click on Per instance Metrics

Step 6: Copy Instance Id in the search box

Step 7: Select Instance Id and Metric as CPU Utilization.

Step 8 Click on Select Metric Button.

Step 9: Keep all the properties same except below

            Threshold Type: Static

             Select : Greater Than (you can chose any option like >=,< etc)

            than : 50 ( I am selecting 50 so that I need to set the alarm if Cpu utilization is more than 50% of selected EC2 instance)

Click on Next Button.

Step 10 : Select In Alarm

Step 11 : Select SNS topic for notificaiton.

Step 12: Select EC2 Action and select Terminate Instance.

Step 13: Click on Next Button

Step 14: Give the Alarm Name and Click on Create Alarm Button.    


CloudWatch Log Monitoring

Step 1: Configure your IAM role or user for CloudWatch Logs

             Create a Role (Cloudwatchrole) for EC2 instance

             Attach CloudWatchFullAccess permission

Step 2: Install and configure CloudWatch Logs on an existing Amazon EC2 instance

            Connect to EC2 instance and install the AWS Cloudwatch logs agent:

           >>> sudo yum update -y

           >>> sudo yum install -y awslogs

           If required change /etc/awslogs/awslogs.conf to monitor the file. change the log_group_name so that it will reflect in the aws console (easier to identify).

[/var/log/messages] datetime_format = %b %d %H:%M:%S file = /var/log/messages buffer_duration = 5000 log_stream_name = {instance_id} initial_position = start_of_file log_group_name = /var/log/messages-irfan

           By default, the /etc/awslogs/awscli.conf points to the us-east-1 Region. To push your logs to a different Region, edit the awscli.conf file and specify that Region.

            >>> sudo vi /etc/awslogs/awscli.conf

[plugins] cwlogs = cwlogs [default] region = ap-southeast-2

            >>> sudo systemctl start awslogsd

            >>> sudo systemctl enable awslogsd.service 

            >>> sudo systemctl status awslogsd

now go to cloudwatch, ENSURE YOU ARE IN THE RIGHT REGION, then check your logs.



Fluentd

Open-source log data collector > why logs? - for compliance (auditing, company, business) - for security (transparency, monitoring, admin...