{"id":18343,"date":"2015-03-20T15:04:22","date_gmt":"2015-03-20T09:34:22","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=18343"},"modified":"2015-03-21T20:11:36","modified_gmt":"2015-03-21T14:41:36","slug":"logs-monitoring-using-aws-cloudwatch","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/logs-monitoring-using-aws-cloudwatch\/","title":{"rendered":"Logs Monitoring Using AWS CloudWatch"},"content":{"rendered":"<div>\n<p><span style=\"color: #000000\"><span style=\"font-size: 24px;line-height: 36px\"><b>\u00a0<\/b><\/span><\/span>Suppose, you have multiple servers running a simple web application on apache or Nginx and you want to put all the error\/access logs on a centralized place so that you can troubleshoot your system in case of any error after getting alert notification configured on your logs .<\/p>\n<p>Here is an amazing feature of Amazon Web Services Cloud Suite by which you can achieve this task.<\/p>\n<p>Amazon CloudWatch now provides us the flexibility to monitor, maintain, store and access our custom log files, log files from EC2 Servers, CloudTrail and other resources. We can also generate alerts on those logs. This will help us in troubleshooting our servers by monitoring all the appplication-specific logs on CloudWatch in real time.<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Untitled-Diagram1-4.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-18379\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Untitled-Diagram1-4.jpg\" alt=\"Untitled Diagram1 (4)\" width=\"630\" height=\"564\" \/><\/a><\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">Implementation:-<\/h3>\n<p><span style=\"text-decoration: underline\"><strong>Step 1<\/strong><\/span><strong>\u00a0:-<\/strong><\/p>\n<p>Firstly we need two policies attached to an IAM role which we will assign to EC2 instances so that the logs from the instances can be pushed to the CloudWatch.<\/p>\n<p>There is a need of an CloudWatch agent which will do the task to push logs onto the CloudWatch.An agent-configuration file is necessary which we can store in our S3 bucket and at the time of launching an instance we will use that agent-configuration file.<\/p>\n<p>The two necessary policies are :-<\/p>\n<p>&nbsp;<\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">Policy 1\u00a0:-<\/h3>\n<p>This policy will allow your EC2 instance to access the agent-configuration file stored in your S3 bucket.Here you would give the name of your S3 bucket.<\/p>\n<p>{\u00a0\u00a0<br \/>\u00a0\u00a0\u00a0&#8220;Version&#8221;:&#8221;2012-10-17&#8243;,<br \/>\u00a0\u00a0\u00a0&#8220;Statement&#8221;:[\u00a0\u00a0<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{\u00a0\u00a0<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8220;Effect&#8221;:&#8221;Allow&#8221;,<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8220;Action&#8221;:[\u00a0\u00a0<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8220;logs:*&#8221;,<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8220;s3:GetObject&#8221;<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0],<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8220;Resource&#8221;:[\u00a0\u00a0<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8220;arn:aws:logs:*:*:*&#8221;,<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8220;arn:aws:s3:::your_bucket_name\/*&#8221;<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0]<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<br \/>\u00a0\u00a0\u00a0]<br \/>}<\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">Policy 2\u00a0:-<\/h3>\n<p>This policy will allow your EC2 instance to push the log file stored in your CloudWatch.So here I am assigning all the permissions to my EC2 instance so that it can create log group,log stream and other necessary files.<\/p>\n<p>{<\/p>\n<p>&#8220;Version&#8221;:&#8221;2012-10-17&#8243;,&#8221;Statement&#8221;:[<\/p>\n<p>{\u00a0\u00a0<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8220;Effect&#8221;:&#8221;Allow&#8221;,<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8220;Action&#8221;:[\u00a0\u00a0<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8220;logs:*&#8221;<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0],<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8220;Resource&#8221;:[\u00a0\u00a0<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8220;arn:aws:logs:*:*:*&#8221;<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0]<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<br \/>\u00a0\u00a0\u00a0]<br \/>}<\/p>\n<p>&nbsp;<\/p>\n<p>So create an IAM role and assign two policies to it and at the time of launching EC2 instance you will be assigning this role to your EC2 instance.<\/p>\n<p><span style=\"text-decoration: underline\"><strong>Step 2<\/strong><\/span><strong>\u00a0:-\u00a0<\/strong><strong>Understanding Agent-Configuration File (stored on S3)<\/strong><\/p>\n<p>Now, create an agent configuration file Cloudwatch_agent_conf \u00a0and paste the content of \u00a0following file \u00a0and edit it accordingly and upload it to your S3 bucket.<\/p>\n<p>[general]<br \/>\nstate_file = \/var\/awslogs\/state\/agent-state<\/p>\n<p>[\/var\/log\/syslog]<br \/>\nfile = \/var\/log\/nginx\/access.log<br \/>\nlog_group_name = nginx_server<br \/>\nlog_stream_name = nginx_access_logs<br \/>\ndatetime_format = %b %d %H:%M:%S<\/p>\n<p>&nbsp;<\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">Parameters in agent-configuration file\u00a0<strong>:-<\/strong><\/h3>\n<p><strong>1.file :-\u00a0<\/strong>The file specifies the file in which your actual logs are stored on your EC2 instances. This is the log file whose content you want to push on CloudWatch logs . I want to push my nginx access logs onto the CloudWatch so I am specifiying the path of nginx access log file.<\/p>\n<p><strong>2.log_group_name :-\u00a0<\/strong>It refers to the destination log group. A log group will be created automatically if no log group exists in your CloudWatch.<\/p>\n<p><strong>3.log_stream_name :-\u00a0<\/strong>It refers to the destination log stream.A log stream can be {instance_id}, {hostname}, {ip_address} or a combination of these.<\/p>\n<p><strong>4.datetime_format :-\u00a0<\/strong>It specifies how the timestamp is extracted from logs.<\/p>\n<p>%b specifies month (Jan,Feb..)<\/p>\n<p>%d specifies\u00a0day of month (01,02..)<\/p>\n<p>%H specifies Hour (24-hour clock)<\/p>\n<p>%M specifies Minutes (01,02..59)<\/p>\n<p>%S specifies Seconds (01,02..59)<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-decoration: underline\"><strong>Step 3<\/strong><\/span><strong>\u00a0:-\u00a0<\/strong>Now, we need a script which we can pass in the User data at the time of launching an EC2 instance which will make all the configurations for CloudWatch agent to push logs to the CloudWatch.<\/p>\n<p>#!\/bin\/bash<br \/>\nwget https:\/\/s3.amazonaws.com\/aws-cloudwatch\/downloads\/latest\/awslogs-agent-setup.py<\/p>\n<p>chmod +x .\/awslogs-agent-setup.py<\/p>\n<div>\n<p>\u00a0.\/awslogs-agent-setup.py -n -r us-east-1 -c s3:\/\/your_bucket_name\/Cloudwatch_agent_conf<\/p>\n<div>\n<p>\u00a0sudo service awslogs restart<\/p>\n<\/div>\n<\/div>\n<p><span style=\"text-decoration: underline\"><strong>Step 4<\/strong><\/span><strong>\u00a0:-\u00a0<\/strong>We are almost done with the configuration . Now, launch a new EC2 instance, assign it the IAM role with two policies which you have created above and pass the above script as a userdata to the instance.<\/p>\n<p>You can also append other packages&#8217; installation steps after these lines in userdata script like installation of nginx or apache.<\/p>\n<p>Now go to your AWS CloudWatch console.Go to \u201cLogs\u201d in Dashboard, you will be able to see the log group name which you mentioned in your agent-configuration file.<\/p>\n<p>&nbsp;<\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">SNS Integration on Logs to create alerts<\/h3>\n<p><span style=\"text-decoration: underline\"><strong>Step 1<\/strong><\/span><strong>\u00a0:-\u00a0<\/strong>Go to the Log Groups in your AWS CloudWatch console .Click on Create Metric Filter.<a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/cw1.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-18380\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/cw1.png\" alt=\"cw1\" width=\"904\" height=\"130\" \/><\/a><\/p>\n<p><span style=\"text-decoration: underline\"><strong>Step 2<\/strong><\/span><strong>\u00a0:-<\/strong>\u00a0Now, you can set pattern to be searched in logs.<\/p>\n<p>For Example, I want to create alarm according to my GET requests.Click on Assign Metric.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-18381\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/cw2.png\" alt=\"cw2\" width=\"983\" height=\"561\" \/><\/p>\n<p><span style=\"text-decoration: underline\"><strong>Step 3<\/strong><\/span><strong>\u00a0:-\u00a0<\/strong>Now, assign a metric to your metric filter.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-18382\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/cw3.png\" alt=\"cw3\" width=\"677\" height=\"293\" \/><\/p>\n<p><span style=\"text-decoration: underline\"><strong>Step 4<\/strong><\/span><strong>\u00a0:-\u00a0<\/strong>Your metric filter is created.Now click on Create Alarm .<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-18383\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/cw4.png\" alt=\"cw4\" width=\"707\" height=\"179\" \/><\/p>\n<p><span style=\"text-decoration: underline\"><strong>Step 5\u00a0<\/strong><\/span><strong>:-\u00a0<\/strong>Here you can create alarm by setting thresholds and ARN for your SNS.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-18384\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/cw5.png\" alt=\"cw5\" width=\"685\" height=\"450\" \/><\/p>\n<p>Finally your alarms are configured on your logs.You can check your alarms in CloudWatch alarms.<\/p>\n<\/div>\n<div><\/div>\n<div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0Suppose, you have multiple servers running a simple web application on apache or Nginx and you want to put all the error\/access logs on a centralized place so that you can troubleshoot your system in case of any error after getting alert notification configured on your logs . Here is an amazing feature of Amazon [&hellip;]<\/p>\n","protected":false},"author":170,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":19},"categories":[1174],"tags":[1262,1547,1694,1696,1695],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/18343"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/170"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=18343"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/18343\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=18343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=18343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=18343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}