Skip to content

Latest commit

 

History

History
212 lines (159 loc) · 6.9 KB

DEPLOY.md

File metadata and controls

212 lines (159 loc) · 6.9 KB

Deploy production

Discord Bot creation

  1. Open "Applications" at Discord Developer Portal
  2. Click "New Application" button
  3. Type application name and "Create" button
  4. Click "Bot" on the left panel of the created application
  5. Click "Reset Token" button and then confirm by clicking "Yes, do it!" button
  6. Re-enter your Discord password and click "Submit" button
  7. Copy and save the generated bot token
  8. Check "MESSAGE CONTENT INTENT" and click "Save Changes" button
  9. Click "OAuth2" on the left panel
  10. In "OAuth2 URL Generator" -> "SCOPES" section check "bot"
  11. In "OAuth2 URL Generator" -> "BOT PERMISSIONS" section check "Send Messages"
  12. Set "OAuth2 URL Generator" -> "INTEGRATION TYPE" = Guild Install
  13. Copy "OAuth2 URL Generator" -> "GENERATED URL", open it in your browser and authorize the bot to a server

Initial deployment

Initial deployment is done once. Update section is executed for redeployment

  1. Create EC2 instance:

    1.1 Open EC2 console: https://us-west-2.console.aws.amazon.com/ec2/home?region=us-west-2

    1.2 Click "Launch instance"

    1.3 Fill in the following:

    • Name and tags -> Name: thenewboston.network
    • Application and OS Images: Ubuntu Server 22.04 LTS (HVM), SSD Volume Type, Architecture = 64-bit (x86)
    • Instance type: t2.micro
    • Key pair (login): choose a key pair from the list
    • Network settings: Create security group
    • Network settings: Allow SSH traffic from (Anywhere), Allow HTTPS traffic from the internet, Allow HTTP traffic from the internet
    • Configure storage: 20 Gb (gp2)

    1.4 Click "Launch instance"

  2. Create elastic IPv4 address and assign to the EC2 instance

  3. Create DNS A record thenewboston.network pointing to the elastic IPv4 address

  4. Add 4G swapfile (this is necessary to avoid out of memory errors which lead to EC2 instance reboot):

# Login to the EC2 instance
ssh [email protected]
sudo  -i

# Allocate swap space
fallocate -l 4G /swapfile && \
chmod 600 /swapfile && \
mkswap /swapfile

# Turn on swap file
swapon /swapfile
vim /etc/fstab
# Add the following line
# /swapfile swap swap defaults 0 0

# Correcting swappiness
sysctl vm.swappiness=10
vim /etc/sysctl.conf
# Add the following line
# vm.swappiness=10

# Reboot
reboot

# Login to the EC2 instance again
ssh [email protected]

# Validate new settings
free -h
swapon --show
cat /proc/sys/vm/swappiness
  1. Install the latest software and prerequisites:
ssh [email protected]  # if necessary

sudo apt update && sudo apt upgrade
sudo apt install make nginx
sudo reboot
  1. Configure nginx:
# Run on local machine:
scp thenewboston/project/settings/templates/nginx.conf [email protected]:/tmp/thenewboston.conf && \
ssh [email protected] 'sudo cp /tmp/thenewboston.conf /etc/nginx/sites-available/ && sudo ln -s /etc/nginx/sites-available/thenewboston.conf /etc/nginx/sites-enabled/thenewboston.conf && sudo rm /etc/nginx/sites-available/default'
  1. Install certbot as described at https://certbot.eff.org/instructions?ws=nginx&os=ubuntufocal (use [email protected] for urgent renewal and security notices)

  2. Create environment specific configuration file:

# Run on local machine:
ssh [email protected] 'sudo mkdir -p /etc/thenewboston' && \
scp thenewboston/project/settings/templates/template.env [email protected]:/tmp/template.env && \
ssh [email protected] 'sudo cp /tmp/template.env /etc/thenewboston/.env'
  1. Update /etc/thenewboston/.env with actual values at thenewboston.network:
ssh [email protected]

sudo vim /etc/thenewboston/.env
  1. Install docker as described at https://docs.docker.com/engine/install/ on thenewboston.network
  2. Setup and check docker installation:
ssh [email protected]
sudo usermod -aG docker $USER
exit

ssh [email protected]

# Known working versions described in the comments below 
docker --version # Docker version 26.0.1, build d260a54

# (!!!) At least Docker Compose version v2.24.0 is required
docker compose version # Docker Compose version v2.26.1
  1. Create database RDS instance:

3.1 Open AWS RDS -> Databases: https://us-west-2.console.aws.amazon.com/rds/home?region=us-west-2#databases

3.2 Click "Create database"

3.3 Fill in the following:

  • Choose a database creation method: Standard create
  • Engine options -> Engine type: PostgreSQL
  • Engine options -> Engine version: 16.2
  • Templates: Free tier
  • Settings -> DB instance identifier: thenewboston
  • Settings -> Credentials Settings -> Master username: thenewboston
  • Settings -> Credentials Settings -> Credentials management: Self managed
  • Settings -> Credentials Settings -> Master password:
  • Settings -> Credentials Settings -> Confirm master password:
  • Instance configuration -> DB instance class -> Burstable classes (includes t classes): db.t3.micro
  • Storage -> Storage type -> gp2
  • Storage -> Allocated storage: 20
  • Storage -> Storage autoscaling -> Storage autoscaling -> Enable storage autoscaling: checked
  • Storage -> Storage autoscaling -> Storage autoscaling -> Maximum storage threshold: 40
  • Connectivity -> Compute resource: Don’t connect to an EC2 compute resource
  • Connectivity -> Network type: IPv4
  • Connectivity -> Virtual private cloud (VPC): Default VPC
  • Connectivity -> DB subnet group: default
  • Connectivity -> Public access: Yes
  • Connectivity -> VPC security group (firewall) -> Create new: Automatic setup
  • Connectivity -> New VPC security group name: thenewboston-database-sg
  • Database authentication -> Database authentication options: Password authentication
  • Monitoring -> Turn on Performance Insights: unchecked
  • Monitoring -> Additional configuration -> Enable Enhanced Monitoring: unchecked
  • Additional configuration -> Database options -> Initial database name: thenewboston
  • Additional configuration -> Log exports -> PostgreSQL log: checked
  • Additional configuration -> Log exports -> Upgrade log: checked
  • Additional configuration -> Deletion protection -> Enable deletion protection: checked
  • Additional configuration: leave defaults for the rest of the options
  1. Clone git repo:
ssh [email protected]  # if necessary

git clone https://github.com/thenewboston-developers/thenewboston-Backend.git
  1. Update docker-compose.yaml:
ssh [email protected]  # if necessary

bash -c 'cd ~/thenewboston-Backend && make update-docker-compose-yaml'
  1. Run Update section

Update

  1. Login:
  1. Fetch the latest code:
cd ~/thenewboston-Backend && git fetch origin && git checkout origin/master
  1. Redeploy:
make deploy
  1. Clean up:
make deploy-cleanup