Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add my own log to log rotation/S3 backup on Amazon Elastic Beanstalk?

We have a PHP application that lives on Amazon Elastic Beanstalk. The application has log rotation and backup to S3 enabled. Apache access and error logs do get properly rotated and backed up every hour.

However the application also creates its own log file. I want to do the same thing with it - every hour it should be rotated and backed up to S3. Following the instructions here I created the following file:

.ebextensions/publish-logs.config

files: 
  "/opt/elasticbeanstalk/tasks/publishlogs.d/cloud-init.conf" :
    mode: "000755"
    owner: root
    group: root
    content: |
      /var/app/current/log/*.log

Then I uploaded the new version to Amazon.

The results - I see that the log file was backed up to S3 ONCE at the very first rotation. And it was not gzipped, just copied. After that, nothing. No new backups to S3. No rotation. When downloading bundle logs, the file is there, in its full glory of about 80MB now (accumulated over several days).

Amazon's documentation is pretty sparse. But it does say that:

When you configure your application's log files for log rotation, the application doesn't need to create copies of log files. Elastic Beanstalk configures logrotate to make a copy of your application's log files for each rotation.

What have I done wrong?

like image 777
Vilx- Avatar asked Nov 23 '17 11:11

Vilx-


People also ask

Where does Elastic Beanstalk store logs files?

Elastic Beanstalk creates a bucket named elasticbeanstalk- region - account-id for each AWS Region in which you create environments. Within this bucket, logs are stored under the path resources/environments/logs/ logtype / environment-id / instance-id .

Can we use S3 with Elastic Beanstalk?

To access an S3 bucket from Elastic Beanstalk, verify that your AWS Identity and Access Management (IAM) instance profile is attached to an Amazon Elastic Compute Cloud (Amazon EC2) instance. The instance must have the correct permissions for Amazon S3.

How do you get logs from Elastic Beanstalk?

To view instance logs, you can enable instance log rotation and log streaming in the Elastic Beanstalk console. Open the Elastic Beanstalk console , and in the Regions list, select your AWS Region. In the navigation pane, choose Environments, and then choose the name of your environment from the list.

How do you rotate logs with logrotate?

Similarly, we can use a postrotate directive to run scripts after each rotation. When logrotate runs the script, it will pass as the first argument the file path for each log file to the scripts. The configuration above will produce 2 log lines in the /tmp/rotation. log files for each log file rotated.


1 Answers

To do that you need to configure logrotate, and that is a bit tricky. Since highly depends on the instance you are using. But let me try. Add this two files to your configuration. First creates configuration for logrotate, and second configures cron to run logrotate with that configuration.

files:
    "/etc/logrotate.d/logrotate.elasticbeanstalk.php.conf":
        mode: "000655"
        owner: root
        group: root
        content: |
            /var/app/current/log/*.log {
                rotate 14
                size 100M
                daily
                compress
                delaycompress
            }
    "/etc/cron.daily/cron.logrotate.elasticbeanstalk.php.conf":
        mode: "000655"
        owner: root
        group: root
        content: |
            #!/bin/sh
            test -x /usr/sbin/logrotate || exit 0
            /usr/sbin/logrotate /etc/logrotate.d/logrotate.elasticbeanstalk.php.conf
            /sbin/service awslogs restart

Give it a try. If fail - please provide AMI ID you are using

like image 121
Michał Zaborowski Avatar answered Sep 30 '22 17:09

Michał Zaborowski