{"id":1674,"date":"2022-11-10T10:30:19","date_gmt":"2022-11-10T10:30:19","guid":{"rendered":"http:\/\/cloudaliv.com\/stage\/?p=1674"},"modified":"2023-08-24T13:34:10","modified_gmt":"2023-08-24T13:34:10","slug":"pushing-custom-metrics-application-logs-to-cloudwatch-using-cloudwatch-agent","status":"publish","type":"post","link":"https:\/\/cloudaliv.com\/stage\/pushing-custom-metrics-application-logs-to-cloudwatch-using-cloudwatch-agent\/","title":{"rendered":"AWS CloudWatch: Custom Metrics &#038; Logs"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"1674\" class=\"elementor elementor-1674\">\n\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-35e18981 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"35e18981\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-222ff78a\" data-id=\"222ff78a\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-ea3010e elementor-widget elementor-widget-text-editor\" data-id=\"ea3010e\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p><\/p>\n<p class=\"wp-block-paragraph\">Earlier, Custom metrics and application logs are pushed to CloudWatch using the CloudWatch monitoring scripts. They are now deprecated. AWS recommends using the CloudWatch agent to collect metrics and logs.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"wp-image-1679 size-full aligncenter\" src=\"http:\/\/cloudaliv.com\/stage\/wp-content\/uploads\/2022\/10\/Cloudwatch.png\" alt=\"\" width=\"500\" height=\"500\" srcset=\"https:\/\/cloudaliv.com\/stage\/wp-content\/uploads\/2022\/10\/Cloudwatch.png 500w, https:\/\/cloudaliv.com\/stage\/wp-content\/uploads\/2022\/10\/Cloudwatch-300x300.png 300w, https:\/\/cloudaliv.com\/stage\/wp-content\/uploads\/2022\/10\/Cloudwatch-150x150.png 150w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/p>\n<p><\/p>\n<p><\/p>\n<p class=\"wp-block-paragraph\">AWS CloudWatch service has the capability to store custom logs and process metrics generated from application instances.<\/p>\n<p><\/p>\n<p><\/p>\n<p class=\"wp-block-paragraph\">Use cases for custom metrics and logs:<\/p>\n<p><\/p>\n<p><\/p>\n<ol class=\"wp-block-list\">\n<li>EC2 instance custom metrics (Disk, Memory, etc)can be pushed to CloudWatch.<\/li>\n<li>Webserver (Nginx, Apache, etc) access or error logs can be pushed to Cloudwatch logs which acts as central log management for applications running on AWS.<\/li>\n<\/ol>\n<p><\/p>\n<p><\/p>\n<h6 class=\"wp-block-heading\"><strong>Pushing Custom Metrics &amp; Application Logs To AWS Cloudwatch Workflow<\/strong><\/h6>\n<p><\/p>\n<p><\/p>\n<ol class=\"wp-block-list\">\n<li>Create a custom EC2 IAM role with CloudWatch log write access<\/li>\n<li>Install CloudWatch agent &amp; Pushing Custom Metrics to CloudWatch<\/li>\n<li>Configure the log path in the CloudWatch agent log configuration file.<\/li>\n<li>Start the CloudWatch agent with the log configuration file.<\/li>\n<li>Validate logs in the CloudWatch console.<\/li>\n<\/ol>\n<p><\/p>\n<p><\/p>\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/V4MJThfvmHNlBCe_LT6_RT1hQ06IWRBAYA0_SK1UntY0xxDl1mB2KqZPfrqvreSmxcU_K4X9o1JpPl2iu-zHP3F1NkV2VX1tD6IH28kFATbBhj6GFOiiS7fSjGfzrnFxZKLudRXRQ24hNqu_siRIKIEJRUTzL2vbYNaQWhRA6fNhz0-wEugqnCNo-Q\" alt=\"\"><\/figure>\n<p><\/p>\n<p><\/p>\n<h6 class=\"wp-block-heading\"><strong>Create a custom EC2 IAM role with CloudWatch log write access<\/strong><\/h6>\n<p><\/p>\n<p><\/p>\n<p class=\"wp-block-paragraph\">Create and add a custom EC2 IAM role to the instance. This IAM role will have policies with write access to the CloudWatch service so that all the logs from EC2 instances can be shipped to CloudWatch.<\/p>\n<p><\/p>\n<p><\/p>\n<p class=\"wp-block-paragraph\">Attach the following policy to the Role.<\/p>\n<p><\/p>\n<p><\/p>\n<figure class=\"wp-block-table\">\n<table>\n<tbody>\n<tr>\n<td>{<br>&nbsp; &#8220;Version&#8221;: &#8220;2012-10-17&#8221;,<br>&nbsp; &#8220;Statement&#8221;: [<br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &#8220;Effect&#8221;: &#8220;Allow&#8221;,<br>&nbsp; &nbsp; &nbsp; &#8220;Action&#8221;: [<br>&nbsp; &nbsp; &nbsp; &nbsp; &#8220;logs:CreateLogGroup&#8221;,<br>&nbsp; &nbsp; &nbsp; &nbsp; &#8220;logs:CreateLogStream&#8221;,<br>&nbsp; &nbsp; &nbsp; &nbsp; &#8220;logs:PutLogEvents&#8221;,<br>&nbsp; &nbsp; &nbsp; &nbsp; &#8220;logs:DescribeLogStreams&#8221;<br>&nbsp; &nbsp; ],<br>&nbsp; &nbsp; &nbsp; &#8220;Resource&#8221;: [<br>&nbsp; &nbsp; &nbsp; &nbsp; &#8220;arn:aws:logs:*:*:*&#8221;<br>&nbsp; &nbsp; ]<br>&nbsp; }<br>]<br>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p><\/p>\n<p><\/p>\n<h6 class=\"wp-block-heading\"><strong>Install CloudWatch agent &amp; Pushing custom metrics to Cloudwatch<\/strong><\/h6>\n<p><\/p>\n<p><\/p>\n<p class=\"wp-block-paragraph\">Use the following commands &#8211; <strong>Amazon Linux 2<\/strong><\/p>\n<p><\/p>\n<p><\/p>\n<figure class=\"wp-block-table\">\n<table>\n<tbody>\n<tr>\n<td>#!\/bin\/bash<br>sudo yum install amazon-cloudwatch-agent -y<br>cd \/opt\/aws\/amazon-cloudwatch-agent\/etc<br>sudo wget https:\/\/metrics.s3.ap-southeast-2.amazonaws.com\/instancemetrics.json<br>sudo chmod 777 instancemetrics.json<br>sudo \/opt\/aws\/amazon-cloudwatch-agent\/bin\/amazon-cloudwatch-agent-ctl -a append-config -m ec2 -s -c file:\/opt\/aws\/amazon-cloudwatch-agent\/etc\/instancemetrics.json<br>systemctl restart amazon-cloudwatch-agent.service<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p><\/p>\n<p><\/p>\n<p class=\"wp-block-paragraph\">Use the following commands &#8211;<strong> Ubuntu<\/strong><\/p>\n<p><\/p>\n<p><\/p>\n<figure class=\"wp-block-table\">\n<table>\n<tbody>\n<tr>\n<td>#!\/bin\/bash<br>sudo wget https:\/\/s3.amazonaws.com\/amazoncloudwatch-agent\/debian\/amd64\/latest\/amazon-cloudwatch-agent.deb<br>sudo dpkg -i -E .\/amazon-cloudwatch-agent.deb<br>sudo apt-get update &amp;&amp; sudo apt-get install collectd -y<br>cd \/opt\/aws\/amazon-cloudwatch-agent\/etc<br>sudo wget https:\/\/metrics.s3.ap-southeast-2.amazonaws.com\/instancemetrics.json<br>sudo chmod 777 instancemetrics.json<br>sudo \/opt\/aws\/amazon-cloudwatch-agent\/bin\/amazon-cloudwatch-agent-ctl -a append-config -m ec2 -s -c file:\/opt\/aws\/amazon-cloudwatch-agent\/etc\/instancemetrics.json<br>sudo service amazon-cloudwatch-agent start<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p><\/p>\n<p><\/p>\n<p class=\"wp-block-paragraph\">You can view the custom metrics in the CloudWatch Console (Metrics \u2192 All Metrics \u2192 Custom namespaces \u2192 Custom Metrics).<\/p>\n<p><\/p>\n<p><\/p>\n<h6 class=\"wp-block-heading\"><strong>Configure the log path in the CloudWatch agent log configuration file<\/strong><\/h6>\n<p><\/p>\n<p><\/p>\n<ul class=\"wp-block-list\">\n<li>Go to \/opt\/aws\/amazon-cloudwatch-agent\/etc<\/li>\n<li>You can find log-config.json<\/li>\n<li>Modify the log file path in the following and Paste it in the log-config.json<\/li>\n<\/ul>\n<p><\/p>\n<p><\/p>\n<figure class=\"wp-block-table\">\n<table>\n<tbody>\n<tr>\n<td>{<br>&nbsp; &nbsp; &#8220;agent&#8221;: {<br>&nbsp; &nbsp; &nbsp; &#8220;metrics_collection_interval&#8221;: 10,<br>&nbsp; &nbsp; &nbsp; &#8220;run_as_user&#8221;: &#8220;root&#8221;<br>&nbsp; &nbsp; },<br>&nbsp; &nbsp; &#8220;logs&#8221;: {<br>&nbsp; &nbsp; &nbsp; &#8220;logs_collected&#8221;: {<br>&nbsp; &nbsp; &nbsp; &nbsp; &#8220;files&#8221;: {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8220;collect_list&#8221;: [<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8220;file_path&#8221;: &#8220;\/var\/log\/httpd\/access*&#8221;,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8220;log_group_name&#8221;:&nbsp; &#8220;apache-access-logs&#8221;,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8220;log_stream_name&#8221;: &#8220;{instance_id}&#8221;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8220;file_path&#8221;: &#8220;\/var\/log\/httpd\/error*&#8221;,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8220;log_group_name&#8221;: &#8220;apache-error-logs&#8221;,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8220;log_stream_name&#8221;: &#8220;{instance_id}&#8221;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]<br>&nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; }<br>&nbsp; }<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p><\/p>\n<p><\/p>\n<h6 class=\"wp-block-heading\"><strong>Start the CloudWatch agent with the log configuration file<\/strong><\/h6>\n<p><\/p>\n<p><\/p>\n<p class=\"wp-block-paragraph\">Use the following command to start the CloudWatch agent with the modified log-config.json file.<\/p>\n<p><\/p>\n<p><\/p>\n<figure class=\"wp-block-table\">\n<table>\n<tbody>\n<tr>\n<td>sudo \/opt\/aws\/amazon-cloudwatch-agent\/bin\/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:\/opt\/aws\/amazon-cloudwatch-agent\/etc\/log-config.json -s<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p><\/p>\n<p><\/p>\n<h6 class=\"wp-block-heading\"><strong>Validating Logs in the Cloudwatch Console<\/strong><\/h6>\n<p><\/p>\n<p><\/p>\n<p class=\"wp-block-paragraph\">Once the setup is done, you can view all the configured logs under the CloudWatch Console.<\/p>\n<p><\/p>\n<p><\/p>\n<ul class=\"wp-block-list\">\n<li>Go to Logs \u2192 Log groups and you will see the log group mentioned in the log configuration file.<\/li>\n<li>Select the log group and you can see the instance ID mentioned in the config.<\/li>\n<\/ul>\n<p><\/p>\n<p><\/p>\n<p class=\"wp-block-paragraph\"><\/p>\n<p><\/p>\n<p><\/p>\n<p class=\"wp-block-paragraph\"><\/p>\n<p><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>Earlier, Custom metrics and application logs are pushed to CloudWatch using the CloudWatch monitoring scripts. They are now deprecated. AWS recommends using the CloudWatch agent to collect metrics and logs. AWS CloudWatch service has the capability to store custom logs and process metrics generated from application instances. Use cases for custom metrics and logs: EC2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1679,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[12],"tags":[19],"class_list":["post-1674","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aws","tag-aws"],"acf":[],"_links":{"self":[{"href":"https:\/\/cloudaliv.com\/stage\/wp-json\/wp\/v2\/posts\/1674","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cloudaliv.com\/stage\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudaliv.com\/stage\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudaliv.com\/stage\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudaliv.com\/stage\/wp-json\/wp\/v2\/comments?post=1674"}],"version-history":[{"count":16,"href":"https:\/\/cloudaliv.com\/stage\/wp-json\/wp\/v2\/posts\/1674\/revisions"}],"predecessor-version":[{"id":4255,"href":"https:\/\/cloudaliv.com\/stage\/wp-json\/wp\/v2\/posts\/1674\/revisions\/4255"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cloudaliv.com\/stage\/wp-json\/wp\/v2\/media\/1679"}],"wp:attachment":[{"href":"https:\/\/cloudaliv.com\/stage\/wp-json\/wp\/v2\/media?parent=1674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudaliv.com\/stage\/wp-json\/wp\/v2\/categories?post=1674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudaliv.com\/stage\/wp-json\/wp\/v2\/tags?post=1674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}