/rails-server-template

Ansible playbook template to deploy Ubuntu 18.04 server on AWS EC2 and locally with Vagrant with Ruby dependencies

Primary LanguageDockerfile

Overview

Ansible playbook template to deploy Ubuntu 18.04 server on AWS EC2 and locally with Vagrant with Ruby dependencies

Setup Local

  1. Enter relevant variables in the local/vars/vars.yml file
  2. Create ~/.ssh/authorized_keys on your local machine and with your public key. This is used as a shared folder with vagrant in order to connect: cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys (ssh-keygen to setup)
  3. Edit the config files in local/vars/configs/
  4. Add any tasks you need to local/tasks/ and local/main.yml
  5. Add any roles you need to local/roles/ and local/main.yml
  6. Create the VM: vagrant up
  7. Connect to the VM: vagrant ssh
  8. Create ruby app: cd /var/www/vhosts then rails new <app name>
  9. Test with default puma server: cd <app name> then rails server -b 0.0.0.0
  10. View in your local browser at localhost:3000

Web Setup

  1. Enter relevant variables in the web/vars/vars.yml file
  2. Add your ssh public key(s) to web/vars/ssh_keys/authorized_keys.yml
  3. Edit the config files in web/vars/configs/
  4. Add any tasks you need to web/tasks/ and web/main.yml
  5. Add any roles you need to web/roles/ and web/main.yml

After the site is provisioned:

  • Create certs with certbot (--certonly)
  • Create an associated domain name, DNS record, add it below [web] in the hosts file.

Local Usage

Vagrant Commands:

  • Build: vagrant up
  • Connect: vagrant ssh
  • Reload: vagrant reload
  • Re-provision: vagrant provision
  • Stop: vagrant halt
  • Delete: vagrant destroy -f

VM Tips & Tricks:

  • View login message: motd
  • File browser: ranger
  • Reload dotfiles: sauce
  • Netdata real-time performance monitor in the browser at 192.168.1.43:19999
    • follow troubleshooting steps here if Netdata is not loading

Resource Allocation:

You can easily change resource allocation for the VM in the lab/local/Vagrantfile.

  • v.memory = <memory> = Memory / RAM allocated to the VM
  • v.cpus = <cpu_cores> = CPU Cores allocated to the VM

Web Usage

lab/web/main.yml is used to build and manage servers and dependencies.

ansible-playbook --ask-vault-pass -i hosts --key-file "~/devops/local/key_pairs/<keypair>.pem" main.yml

Dependencies

Ansible

sudo easy_install pip
sudo pip install ansible
sudo mkdir /etc/ansible
sudo touch /etc/ansible/hosts
sudo touch /etc/ansible/ansible.cfg

Vagrant

Download and install from website.

Virtualbox

Download and install from website.

If experiencing issues installing on mac, follow this guide.

AWS CLI

First, create an AWS account if you haven't already.

Install pip3:

brew install python3
brew postinstall python3

If you encounter permissions issues do this:

sudo mkdir /usr/local/Frameworks
sudo chown $(whoami):admin /usr/local/Frameworks

Install aws-cli:

pip3 install awscli --upgrade --user
echo "export PATH=~/Library/Python/3.7/bin:$PATH" >> ~/.profile
sudo pip install boto

Go to AWS dashboard and create keys for your user.

Create ~/.aws directory and files:

mkdir ~/.aws
touch ~/.aws/config
touch ~/.aws/credentials

Add the text below to ~/.aws/config:

[default]
region = us-east-2

Add the text below to ~/.aws/credentials with relevant credentials:

[default]
aws_access_key_id = <aws access key>
aws_secret_access_key = <aws secret key>

Using Ansible Vault

Make sure to encrypt any passwords/secrets with ansible vault:

  • Creating encrypted files: ansible-vault create file.yml
  • Editing encrypted files: ansible-vault edit file.yml
  • Encrypting files: ansible-vault encrypt file1.yml file2.yml file3.yml
  • Decrypting files: ansible-vault decrypt file.yml