Jonas Bylov
Jonas Bylov

Reputation: 1524

Why does Elastic Beanstalk delete app log on deployment

We're running Elastic Beanstalk (64bit Amazon Linux 2016.09 v2.3.1 running Ruby 2.3 (Puma)) with a Rails app.

The app log is writing to /var/apps/current/log/production.rb like standard. As standard configure with EB, that file is symlinked to /var/apps/containerfiles/logs/ and used for rotation and upload to S3.

For some reason, production.log appear to be overriden or truncated every time we eb deploy, which seems unintended.

Have we misconfigured something and how would you suggest we debug?

Upvotes: 0

Views: 2160

Answers (2)

daveharris
daveharris

Reputation: 395

I was also really surprised when I found this, it seems to be the reverse of what we want. The owner of the log files should be /var/app/containerfiles.

On Amazon Linux 2, I just added a post-deploy hook to switch them back around and seems to work great... I had to do this before with Amazon Linux 1 (AMI) also.

This is the contents of .platform/hooks/postdeploy/logs.sh:

#!/bin/bash

# Switch over the master location of the log files to be /var/app/containerfiles/logs/, with a symlink into /var/app/current/log/ so logs are kept between deploys
# Effectively reversing this line:
  # [INFO] adding builtin Rails logging support
  # [INFO] log publish feature is enabled, setup configurations for rails
  # [INFO] create soft link from /var/app/current/log/production.log to /var/app/containerfiles/logs/production.log

if [ -L /var/app/containerfiles/logs/production.log ]; then
  unlink /var/app/containerfiles/logs/production.log
  mv /var/app/current/log/production.log /var/app/containerfiles/logs/production.log
fi

touch /var/app/containerfiles/logs/production.log
ln -sf /var/app/containerfiles/logs/production.log /var/app/current/log/production.log

Upvotes: 0

Jonas Bylov
Jonas Bylov

Reputation: 1524

We came to the (perhaps obvious) conclusion, that there is no log magic to EB deploys. It just replace the /var/apps/current/ directory, including /var/apps/current/log. Thereby deleting all existing logs.

Our solution therefore was to place logs in a separate folder and patch EB to know where the log is placed. By overriding the production.log symlink in app_log_dir (/var/app/containerfiles/logs/) we still rely on EB's normal procedure for rotation and publishing to S3.

.ebextensions/log-rotation.config

files:
  "/opt/elasticbeanstalk/hooks/appdeploy/pre/01a_override_log_symlinks.sh":
    mode: "000777"
    content: |
      #!/bin/bash
      EB_APP_LOG_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_log_dir)
      CUSTOM_APPLOG_DIR=/var/log/applog
      mkdir -p $CUSTOM_APPLOG_DIR
      chown webapp $CUSTOM_APPLOG_DIR
      chmod 777 $CUSTOM_APPLOG_DIR

      cd $EB_APP_LOG_DIR
      ln -sf $CUSTOM_APPLOG_DIR/production.log production.log
      ln -sf $CUSTOM_APPLOG_DIR/development.log development.log

/config/environments/production.rb

...
# Specific for Rails 5!
config.paths['log'] = "/var/log/applog/#{Rails.env}.log"
...

Upvotes: 2

Related Questions