{"id":72649,"date":"2025-06-13T11:35:40","date_gmt":"2025-06-13T06:05:40","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=72649"},"modified":"2025-09-03T19:52:02","modified_gmt":"2025-09-03T14:22:02","slug":"automating-server-log-mangement-moving-logs-to-s3-bucket","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/automating-server-log-mangement-moving-logs-to-s3-bucket\/","title":{"rendered":"Automating Server log mangement: Offloading logs to S3 bucket."},"content":{"rendered":"<h1><span style=\"color: #000000;\"><strong>Introduction<\/strong><\/span><\/h1>\n<p><span style=\"color: #000000;\">Server monitoring is one of the crucial task of the overall Infrastructure monitoring and observability whether it is a production, staging or development environment and on the key factor of server monitoring is the disk that has been attached to that server as it contains most of the crucial logs that may play a key role while debugging or while facing any server related issue but these logs may cause the performance issue and the storage issue if these logs are not managed properly and without some automated way for log rotation these logs can consume high disk usage and my cause in loosing of some critical logs.<\/span><\/p>\n<p>So, In this blog we will be looking how we can rotate these logs and moving these logs over the S3 bucket that can further be rotated using lifecycle rules on S3 bucket.<\/p>\n<p><strong>Problem Statement<\/strong><\/p>\n<p>Most of us have gone through the problem where servers crash unexpectedly or they become unresponsive because of the high disk utilization which is caused by unmanaged logs. And these logs increase with the time<\/p>\n<p><span style=\"color: #000000;\">So we need a automated way to rotate these logs so that we can prevent high disk utilization which result in<\/span><\/p>\n<ul>\n<li><span style=\"color: #000000;\">High Latency<\/span><\/li>\n<li><span style=\"color: #000000;\">Failed Deployments<\/span><\/li>\n<li><span style=\"color: #000000;\">Server Unresponsiveness<\/span><\/li>\n<\/ul>\n<p><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-74565\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/08\/Screenshot-from-2025-08-30-03-00-57-1024x678.png\" alt=\"Image\" width=\"679\" height=\"450\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/08\/Screenshot-from-2025-08-30-03-00-57-1024x678.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/08\/Screenshot-from-2025-08-30-03-00-57-300x199.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/08\/Screenshot-from-2025-08-30-03-00-57-768x508.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/08\/Screenshot-from-2025-08-30-03-00-57-624x413.png 624w, \/blog\/wp-ttn-blog\/uploads\/2025\/08\/Screenshot-from-2025-08-30-03-00-57.png 1242w\" sizes=\"(max-width: 679px) 100vw, 679px\" \/><\/p>\n<p><span style=\"color: #000000;\"><strong>Prerequisites<\/strong><\/span><\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li><span style=\"color: #000000;\">AWS Linux server<\/span><\/li>\n<li><span style=\"color: #000000;\">S3 bucket where the logs will be moved<\/span><\/li>\n<li><span style=\"color: #000000;\">Basic knowledge of AWS CLI, Linux command line and Bash Scripting.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<div id=\"attachment_74567\" style=\"width: 1146px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-74567\" decoding=\"async\" loading=\"lazy\" class=\" wp-image-74567\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/08\/Screenshot-from-2025-08-30-03-23-49-1024x375.png\" alt=\"Log Management\" width=\"1136\" height=\"416\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/08\/Screenshot-from-2025-08-30-03-23-49-1024x375.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/08\/Screenshot-from-2025-08-30-03-23-49-300x110.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/08\/Screenshot-from-2025-08-30-03-23-49-768x281.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/08\/Screenshot-from-2025-08-30-03-23-49-624x229.png 624w, \/blog\/wp-ttn-blog\/uploads\/2025\/08\/Screenshot-from-2025-08-30-03-23-49.png 1362w\" sizes=\"(max-width: 1136px) 100vw, 1136px\" \/><p id=\"caption-attachment-74567\" class=\"wp-caption-text\">Log Management<\/p><\/div>\n<h1><span style=\"color: #000000;\"><strong>Step 1 &#8211; Create a S3 bucket.<\/strong><\/span><\/h1>\n<p><span style=\"color: #000000;\">First, we need to create a S3 bucket, where we will be moving the logs from our EC2 server. Also, we will be applying the lifecycle policy on it for log rotation.<\/span><\/p>\n<div id=\"attachment_72644\" style=\"width: 1117px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-72644\" decoding=\"async\" loading=\"lazy\" class=\" wp-image-72644\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-01-23-26.png\" alt=\"Create Bucket\" width=\"1107\" height=\"222\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-01-23-26.png 1661w, \/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-01-23-26-300x60.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-01-23-26-1024x205.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-01-23-26-768x154.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-01-23-26-1536x308.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-01-23-26-624x125.png 624w\" sizes=\"(max-width: 1107px) 100vw, 1107px\" \/><p id=\"caption-attachment-72644\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Create Bucket<\/span><\/p><\/div>\n<p><span style=\"color: #000000;\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-72645 aligncenter\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-01-25-44.png\" alt=\"Bucket\" width=\"1148\" height=\"719\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-01-25-44.png 1690w, \/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-01-25-44-300x188.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-01-25-44-1024x641.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-01-25-44-768x481.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-01-25-44-1536x962.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-01-25-44-624x391.png 624w\" sizes=\"(max-width: 1148px) 100vw, 1148px\" \/><\/span><\/p>\n<p><span style=\"color: #000000;\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-72646 aligncenter\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-01-26-31.png\" alt=\"Bucket2\" width=\"1150\" height=\"720\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-01-26-31.png 1690w, \/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-01-26-31-300x188.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-01-26-31-1024x641.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-01-26-31-768x481.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-01-26-31-1536x962.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-01-26-31-624x391.png 624w\" sizes=\"(max-width: 1150px) 100vw, 1150px\" \/><\/span><\/p>\n<p><span style=\"color: #000000;\"><strong><br \/>\n<\/strong>Create a lifecycle policy for rotating the logs, like below &#8211;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\"><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-72647 aligncenter\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-02-24-36.png\" alt=\"Bucket3\" width=\"1150\" height=\"709\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-02-24-36.png 1301w, \/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-02-24-36-300x185.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-02-24-36-1024x631.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-02-24-36-768x473.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-02-24-36-624x385.png 624w\" sizes=\"(max-width: 1150px) 100vw, 1150px\" \/><\/span><\/p>\n<p><span style=\"color: #000000;\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-72648 aligncenter\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-02-30-38.png\" alt=\"lifecycle \" width=\"934\" height=\"665\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-02-30-38.png 1364w, \/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-02-30-38-300x214.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-02-30-38-1024x729.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-02-30-38-768x547.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/06\/Screenshot-from-2025-06-09-02-30-38-624x444.png 624w\" sizes=\"(max-width: 934px) 100vw, 934px\" \/><\/span><\/p>\n<h1><span style=\"color: #000000;\"><strong>Step 2 &#8211; IAM Role and Policy creation<\/strong><\/span><\/h1>\n<p><span style=\"color: #000000;\">So, For moving the logs from the server to S3 bucket we will be needing a role that will be used by server to move the logs-<\/span><\/p>\n<h2><span style=\"color: #000000;\"><strong>Example of AWS IAM Policy<\/strong><\/span><\/h2>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-73117 \" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/07\/Screenshot-from-2025-07-08-03-32-03.png\" alt=\"Example\" width=\"743\" height=\"583\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/07\/Screenshot-from-2025-07-08-03-32-03.png 681w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/Screenshot-from-2025-07-08-03-32-03-300x235.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/Screenshot-from-2025-07-08-03-32-03-624x489.png 624w\" sizes=\"(max-width: 743px) 100vw, 743px\" \/><\/p>\n<p><span style=\"color: #000000;\">So, after creating a role. Add the above policy to it and add the role over the server.<\/span><\/p>\n<h1><span style=\"color: #000000;\">Step 4 &#8211; Install AWS CLI on server.<br \/>\n<\/span><\/h1>\n<p><span style=\"color: #000000;\">Use below command to install AWS CLI over the server-<\/span><\/p>\n<pre><span style=\"color: #000000;\">curl \"https:\/\/awscli.amazonaws.com\/awscli-exe-linux-x86_64.zip\" -o \"awscliv2.zip\"\r\nunzip awscliv2.zip\r\nsudo .\/aws\/install<\/span><\/pre>\n<p><span style=\"color: #000000;\">For other OS, you can\u00a0refer\u00a0this official\u00a0guide\u00a0of AWS\u00a0 &#8211; https:\/\/docs.aws.amazon.com\/cli\/latest\/userguide\/getting-started-install.html<\/span><\/p>\n<h1><span style=\"color: #000000;\">Step 5 &#8211; Log transfer script<\/span><\/h1>\n<p><span style=\"color: #000000;\">Below is sample script that we would be using to move logs from server to S3-<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-73118\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/07\/Screenshot-from-2025-07-08-03-35-27.png\" alt=\"ag\" width=\"1077\" height=\"322\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/07\/Screenshot-from-2025-07-08-03-35-27.png 1027w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/Screenshot-from-2025-07-08-03-35-27-300x90.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/Screenshot-from-2025-07-08-03-35-27-1024x306.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/Screenshot-from-2025-07-08-03-35-27-768x230.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/07\/Screenshot-from-2025-07-08-03-35-27-624x187.png 624w\" sizes=\"(max-width: 1077px) 100vw, 1077px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>We need to give executable permission to the above sample script, use below command for it-<\/p>\n<pre><span style=\"color: #000000;\">sudo chmod +x logs-to-s3.sh<\/span><\/pre>\n<h1><span style=\"color: #000000;\">Step 6 &#8211; Setup Cron Job<\/span><\/h1>\n<p><span style=\"color: #000000;\">use below commands to setup cron job-<\/span><\/p>\n<pre><span style=\"color: #000000;\">sudo crontab -e\r\n\r\n0 1 * * * \/opt\/logs-to-s3.sh<\/span><\/pre>\n<h1><span style=\"color: #000000;\">Conclusion<\/span><\/h1>\n<p><span style=\"color: #000000;\">Finally, we have setup an automated way to rotate the logs and move the logs to s3 bucket with the lifecycle rule implemented. You can also create or modify the sample script give above in the blog as per the requirements. The cronjob will make sure that the script runs after a fixed interval of time and the lifecycle rules that has been implemented over the bucket will make sure that the objects(Logs) retire after a fixed interval of time.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Server monitoring is one of the crucial task of the overall Infrastructure monitoring and observability whether it is a production, staging or development environment and on the key factor of server monitoring is the disk that has been attached to that server as it contains most of the crucial logs that may play a [&hellip;]<\/p>\n","protected":false},"author":1873,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":31},"categories":[5877],"tags":[248,1782,670,7554],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/72649"}],"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\/1873"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=72649"}],"version-history":[{"count":18,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/72649\/revisions"}],"predecessor-version":[{"id":75244,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/72649\/revisions\/75244"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=72649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=72649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=72649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}