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: under predeploy folder

# this file should be located at: .platform/hooks/predeploy/
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"

Create a file named: under postdeploy folder

# this file should be located at: .platform/hooks/postdeploy/
# mod:
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)
if [ -n "$SIDEKIQ_PID" ]; then
  echo "terminating existing sidekiq"
  # wait process to get killed
  while kill -0 "$SIDEKIQ_PID" 2>/dev/null; do
    sleep 1
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


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.
    command: find .platform/hooks/ -type f -iname "*.sh" -exec chmod +x {} \;