Reputation: 115
I have a CloudWatch set up on my EC2 instance to transfer logs to specific log groups.
In time, those logs can grow quite big in size so I wanted to delete them for example, on weekly basis.
I was wondering if there is any option of setting up auto-cleanup from EC2 instance, of transferred logs using Cloudwatch?
What would be the best way to achieve that?
Upvotes: 2
Views: 2472
Reputation: 4486
To remove the logfiles from EC2 running Linux, you have two choices:
If you're using logfiles that already rotate based on time or other value, you can use the auto_removal
option to delete them after the log agent is finished. See docs.
If you're using a file that's constantly updated, you'll need to use logrotate
, which is a program invoked by CRON that will rename, compress, and delete old files. There's a good intro doc here.
If you use logrotate
, here's an example config that I've found useful for high-volume log sources. It performs a rotate if the file reaches 100 megabytes, rather than just doing it every day (you'll need to run it from cron.hourly
to make that useful). Most important, it enables copytruncate
, which will truncate the file in-place, allowing the program to continue writing to it.
/var/log/filename.log {
rotate 7
daily
maxsize 100M
nodateext
missingok
notifempty
copytruncate
compress
delaycompress
}
Upvotes: 4