Skip to content

Deployment

Sam Ping edited this page Apr 29, 2021 · 3 revisions

Deployment

Sledge is deployed on AWS EC2. As of now, the dev server is hosted on a t2.micro instance with 8GB of storage space.

NOTE: The amount of resources Sledge uses has not been properly measured yet. It may be advisable to open an instance with higher specs if needed.

  1. Create EC2 instance and connect to it. Keep the .pem key somewhere safe!
    1. Create instance:
      • OS: Ubuntu 20.04 LTS x64
      • Instance Type: t2.micro (vCPUs: 1, Memory: 1GiB)
      • Storage Space: 8GB SSD
      • Tags:
        • Name: sledge-dev/prod
        • env: dev/prod
    2. Inbound rules:
      • Allow HTTP and HTTPS. Expose port 5000 (which the server uses).
  2. Update and install dependencies:
    1. sudo apt update # updates repositories
    2. sudo apt install git # installs Git
    3. curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
    4. sudo apt-get install -y nodejs # installs NodeJS and NPM
    5. By now, you should have Git, NodeJS, and NPM installed.
    6. Now you can install PM2 with npm i pm2 -g. This will be used later in deploying the Express server. If you get an error, try running the same command using sudo.
    7. Install MongoDB (instructions taken from here):
      1. wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add - # should return OK
      2. echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
      3. sudo apt-get update
      4. sudo apt-get install -y mongodb-org # installs MongoDB
    8. Now MongoDB should be installed. To start the service, run sudo systemctl start mongod.
    9. sudo apt-get install nginx # installs nginx
  3. Now start the server:
    1. Clone the Sledge repository with git clone https://github.com/HackRU/sledge.git # clones over HTTPS, may have to be updated
    2. cd sledge/server # go into the server directory
    3. npm i # installs Sledge server dependencies
    4. Now if you run node server.js, you'll see that the server is up and running. However, if you close the terminal, the server will also stop running. Thus, we will be using PM2 (which we installed in the dependencies section) to keep the server running in the background.
    5. If you haven't already, close the current Node.js process with CTRL-C. Now run the Express server using PM2 by running pm2 start server.js, and you're good to go!
  4. Now set up the Nginx configs:
    1. rm /etc/nginx/sites-available/default # delete default nginx config file
    2. To be continued...

Setting Up CodeDeploy (based on this):

Make sure the repository you're deploying (probably Sledge since you're on the Sledge Wiki, after all) has an appspec.yaml file in its root. This file contains the instructions for how CodeDeploy should set up the Sledge files on EC2. You can look here for reference.

  1. If it doesn't already exist, go to the "IAM Management" section in the AWS console and create a new role with CodeDeploy permissions. Also create an EC2 role with CodeDeploy permissions and attach it to the EC2 instance, and restart CodeDeploy-agent on that instance.
  2. Install CodeDeploy onto the EC2 instance (based on this).
    1. Go to your home directory by running: cd.
    2. sudo apt-get install ruby-full # installs ruby, which the CodeDeploy Agent uses
    3. sudo apt-get install wget # installs wget, which is a useful command. It might already be installed.
    4. wget https://<bucket-name>.s3.<region-identifier>.amazonaws.com/latest/install # downloads a script that installs CodeDeploy
      • For and , check here.
      • Ex. wget https://aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com/latest/install
    5. chmod +x ./install # gives executable permission to the script
    6. sudo ./install auto > /tmp/logfile # as of 4/22/2021, the docs say to run this to avoid a bug with the script in Ubuntu 20.04.
  3. Go to CodeDeploy in AWS console.
  4. Create a new application for Sledge. Since we deployed Sledge on EC2, for "Compute Platform", choose EC2.
  5. After you create the application, click on it, and create a new deployment group.
    • "Deployment Group Name": Name this deployment group whatever you want.
    • "Service Role": Add the role you created in step 0.
    • "Deployment Type": In-place
    • "Environment Configuration": Amazon EC2 Instances
      • Use the tags you created for the EC2 instance you launched for Sledge.
    • "Agent configuration with AWS Systems Manager": Up to you. I decided on "Now and schedule updates" every 30 days using the basic scheduler. Because of this step, we don't have to manually install CodeDeploy on the EC2 instance.
    • "Deployment Settings": CodeDeployDefault.AllAtOnce
    • "Load balancer": Disabled
    • "Advanced":
      • "Rollbacks": I enabled rollbacks for when a deployment fails.
  6. (Optional) To manually deploy Sledge, click on "Create Deployment":
    1. Make sure the deployment group is correctly selected as the one you created earlier
    2. For "Revision Type", be sure to select "My application is stored in GitHub". To connect a new account (preferably official HackRU GH account?), simply type the username of that GitHub account in "GitHub Token Name", then click on Connect.
    3. "Repository Name": HackRU/sledge
    4. "Commit ID": probably the latest commit.
Clone this wiki locally