Logrotate

To ensure efficient use of disk space, it is recommended to configure log rotation on an Amazon Web Services (AWS) Elastic Compute Cloud (EC2) instance. Additionally, to ensure that logs are stored safely and can be accessed later if needed, it is suggested to save them on Simple Storage Service (S3) provided by AWS.

Logrotate
  1. Create a new file named: /etc/logrotate.d/myapp
/home/ubuntu/myapp/current/log/*.log {
  daily
  dateext
  missingok
  rotate 3
  compress
  copytruncate
  sharedscripts
  create 0644 root adm
  su root adm
  postrotate
    sudo su ubuntu -c "/bin/sh /home/ubuntu/scripts/upload_logs.sh"
  endscript
}
  1. Create a new file named: /home/ubuntu/scripts/upload_logs.sh
#!/bin/bash
instance_id=$(wget -q -O - http://169.254.169.254/latest/meta-data/instance-id)
/usr/local/bin/aws s3 mv /home/ubuntu/myapp/current/log s3://myapp/logs/$instance_id --recursive --exclude "*" --include "*.gz"
  1. Assign role (has S3 permission) to EC2 instance

That's it.