Episodes

Resources

Supported Platforms - https://docs.aws.amazon.com
Beanstalk Episodes - https://www.driftingruby.com
Download Source Code

Summary

# Use the appropriate environment variable
RAILS_MASTER_KEY
RAILS_PRODUCTION_KEY

# Terminal

rails credentials:edit
rails credentials:edit --environment production

aws configure --profile NAME
eb init --profile NAME

aws elasticbeanstalk update-environment \
                  --solution-stack-name "64bit Amazon Linux 2 v3.2.2 running Ruby 2.7" \
                  --environment-name "ENVIRONMENT_NAME" \
		  --region "us-east-1" \
                  --profile=NAME
				  
bundle lock --add-platform x86_64-linux
				  
eb ssh --setup
eb ssh
eb deploy

# Terminal in EC2 Instance

sudo service web restart
sudo service worker restart

tail -f /var/log/eb-engine.log
tail -f /var/log/puma/puma.log

sudo su - webapp
cd /var/app/current
bundle
RAILS_MASTER_KEY=your_key RAILS_ENV=production bin/rails db:create
RAILS_MASTER_KEY=your_key RAILS_ENV=production bin/rails db:migrate

Leave out the port number on the db_host

# config/credentials.yml.enc

secret_key_base: ea2ec3933e576bb9484bf6e664a9efe986f87de46163604e20d556f04b98c4e03a65f0aa4b967bf95f74a7ed367d1b2074a330d2b464b52972a8be15e0ceebe1
db_username: dbuser
db_password: password
db_host: aatz0s1fq3ap9m.cir3bne2xs7f.us-east-1.rds.amazonaws.com

# Procfile

web: bundle exec puma -C /opt/elasticbeanstalk/config/private/pumaconf.rb
worker: bundle exec sidekiq -C config/sidekiq.yml

# .ebextensions/yarn.config

commands:
  01_install_node:
    command: |
      sudo curl --silent --location https://rpm.nodesource.com/setup_14.x | sudo bash -
      sudo yum -y install nodejs

  02_install_yarn:
    test: '[ ! -f /usr/bin/yarn ]'
    command: |
      sudo wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo
      sudo yum -y install yarn

# .platform/nginx/conf.d/elasticbeanstalk/webapp.conf

server_name _ localhost; # need to listen to localhost for worker tier

location / {
    proxy_pass http://my_app; # match the name of upstream directive which is defined above
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    location /cable {
      proxy_pass http://my_app;
      proxy_http_version 1.1;
      proxy_set_header Upgrade "websocket";
      proxy_set_header Connection "Upgrade";
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

location /assets {
    alias /var/app/current/public/assets;
    gzip_static on;
    gzip on;
    expires max;
    add_header Cache-Control public;
}

location /public {
    alias /var/app/current/public;
    gzip_static on;
    gzip on;
    expires max;
    add_header Cache-Control public;
}

location /packs {
    alias /var/app/current/public/packs;
    gzip_static on;
    gzip on;
    expires max;
    add_header Cache-Control public;
}

# .platform/nginx/conf.d/client.conf

client_max_body_size 500M;

# .bundle/config

---

# config/database.yml

production:
  <<: *default
  database: template_production
  username: <%= Rails.application.credentials.db_username %>
  password: <%= Rails.application.credentials.db_password %>
  host: <%= Rails.application.credentials.db_host %>