Cloning an Instance in AWS
- Make an AMI (Actions > Image > Create Image)
- AMI > Launch
- Protect termination
- Enable T2/T3 unlimited
- Storage: more for DB instance, less for BG instance.
- Security groups
- Allow SSH from all
- Allow all traffic from VPC (default VPC group)
- Don't set up elastic IP unless it's a web instance (AWS limits number of elastic IPs)
- Give instance a name like
elmo-staging-db. - Copy public IP and add to your
~/.ssh/configwith helpful alias. - If you are moving your background job processing to a different server, do sudo -u deploy crontab -r to delete the whenever-created crontab, which is no longer needed on this server.
Setting up DB Instance
Configure access to DB Instance
sudo -u deploy crontab -rto delete the whenever-created crontab, no longer needed.sudo systemctl stop nginx && sudo systemctl disable nginxsudo vi /etc/postgresql/9.4/main/postgresql.confand setlisten_addresses = '*'sudo vi /etc/postgresql/9.4/main/pg_hba.confand add these lines:# IPv4 remote connections: host all all 0.0.0.0/0 md5 # IPv6 remote connections: host all all ::/0 md5sudo systemctl restart postgresql- Set password for
deployuser inelmo_productiondatabase (on database instance, privileged user): sudo su - deploy psql elmo_production \password # and enter new password \quit exit
Setup DB connection on web instance
- Verify connection is possible:
psql -h
-U deploy elmo_production Enter the password. Console should open successfully. - Edit
config/database.ymland make it look something like this: default: &default adapter: postgresql encoding: utf8 pool: 5production: <<: *default database: elmo_production host: <DB_INST_PRIV_DNS> user: deploy password: "<DB_PASSWORD>"(Note quotes around password, just to be safe). 3. Restart nginx and ensure site still works. 4. Disable local postgres: sudo systemctl stop postgresql && sudo systemctl disable postgresql
Setting up background worker instance
- Clone app instance per instructions above.
- Do steps under "Setup DB connection on web instance" above.
- Disable local nginx: sudo systemctl stop nginx && sudo systemctl disable nginx
- Open rails console and ensure a basic query (e.g.
User.count) works.
Sample Capistrano config
If using capistrano for deployment, configure something like this:
set :deploy_to, "/u/apps/elmo"
set :rbenv_custom_path, "/opt/rbenv"
set :whenever_roles, %i[bg] # Only deploy schedule.rb jobs to crontab on bg server(s).
set :delayed_job_roles, %i[bg] # Only deploy run delayed_job on bg server(s).
set :delayed_job_workers, 2
server "1.2.3.4", user: "deploy", roles: %i[app web]
server "1.2.3.5", user: "deploy", roles: %i[db]
server "1.2.3.6", user: "deploy", roles: %i[bg]