Solution for running Sidekiq on Amazon Linux 2

Upgrading your ruby stack to sidekiq > 6 and moving to Amazon Linux 2 was a nightmare of a challenge for me.

I spent my entire holiday season and first week of 2022 working over 80 hours a week trying to make this run. I hope this helps you save some time.

Instead of creating a config file in .ebextensions, on amazon linux 2 we can utilize platform hooks. (this is where i screwed up and so did 3 supposedly aws experts)

Without further a do here are the steps:

  • In your app's root folder (next to .ebextensions) create a new folder called: .platform with a subfolder called hooks.
  • Within hooks create two folders: postdeploy & predeploy. Amazon Linux will automatically run scripts placed within these folders.

Create a file named:  03_mute_sidekiq.sh under predeploy folder

#!/bin/sh
# this file should be located at: .platform/hooks/predeploy/03_mute_sidekiq.sh
#mod: a@barot.us
 
EB_APP_USER=$(/opt/elasticbeanstalk/bin/get-config platformconfig -k AppUser)
SIDEKIQ_PID=$(/usr/bin/pgrep -u "$EB_APP_USER" -f sidekiq)
 
if [ -n "$SIDEKIQ_PID" ]; then
  echo "TERM/quieting sidekiq"
  kill -TERM "$SIDEKIQ_PID"
fi

Create a file named:  50_restart_sidekiq.sh under postdeploy folder

#!/bin/sh
# this file should be located at: .platform/hooks/postdeploy/50_restart_sidekiq.sh
# mod: a@barot.us
 
EB_APP_DEPLOY_DIR=$(/opt/elasticbeanstalk/bin/get-config platformconfig -k AppDeployDir)
EB_APP_USER=$(/opt/elasticbeanstalk/bin/get-config platformconfig -k AppUser)
RACK_ENV=$(/opt/elasticbeanstalk/bin/get-config environment -k RACK_ENV)
SIDEKIQ_PID=$(pgrep -u "$EB_APP_USER" -f sidekiq)
SIDEKIQ_LOG="$EB_APP_DEPLOY_DIR/log/sidekiq.log"
 
cd "$EB_APP_DEPLOY_DIR"
 
if [ -n "$SIDEKIQ_PID" ]; then
  echo "terminating existing sidekiq"
  kill -TERM "$SIDEKIQ_PID"
  # wait process to get killed
  while kill -0 "$SIDEKIQ_PID" 2>/dev/null; do
    sleep 1
  done
fi
 
set -xe
export $(cat /opt/elasticbeanstalk/deployment/env | xargs)
 
echo "starting sidekiq"
su "$EB_APP_USER" -c "bundle exec sidekiq -e $RACK_ENV > $SIDEKIQ_LOG 2>&1 & disown"

Errors you most likely will run into:

ERROR .platform/hooks/predeploy/ permission denied

Solution:

Create a file under .eb extensions folder with the right order and name it something like: 001_chmod.config

# Finds the file within hooks folder with extension .sh and makes them executable.
container_commands:
  01_chmod1:
    command: find .platform/hooks/ -type f -iname "*.sh" -exec chmod +x {} \;