DEV Community

gortron
gortron

Posted on

AWS EC2 Deployment Guide

Introduction

I thought I would share my personal deployment checklist for AWS EC2. I remember the first time I deployed, I felt like there were a lot of gotchas. I've put together this checklist, which helps me keep track of the different steps it takes to create a new EC2 instance you can ssh into, configuring its environment, and deploying on it.

For first-timers deploying to AWS, I hope you find this helpful.

Steps

  1. Create a new EC2 instance.

    Defaults / free tier are fine, except for Security Groups and Key Pair.

    1. The included port 22 is for SSH.
    2. Open Port 80. Can use your laptop IP address or leave it as 0.0.0.0 which leaves it exposed to any IP.
    3. Add other ports as necessary. If your server "listens" on 3000, then add 3000. But you may want to consider using nginx to only listen on port 80 but "route" relevant requests to 3000, 8000, etc.
    4. Make a new Key Pair (.pem) and download it. Move it to ~/.ssh . You can do that with mv <drag file to terminal to get current path> ~/.ssh
  2. SSH into your instance.

    You need to configure the read/write authorizations on your ssh key. Then you need to add that key to the destination site and finally add the key to your keychain. Keychain is going to run through all possible ssh keys whenever you try to connect to a site.

    1. chmod 400 ~/.ssh/<your key>. If you need to debug this step, use ls -lh <filepath> , which will tell you permissions. should just be r at the front.
    2. ssh -i identityFile ec2-user@<your host ip address>
    3. ssh-add ~/.ssh/<your key>.pem
    4. eval "$(ssh-agent -s)" - starts the ssh-agent
    5. Finally, ssh ec2-user@<your host ip address>
  3. Install necessary software.

    You'll need git at a minimum. Rest is app-specific. This example is for nvm/node/yarn.

    1. sudo yum install git. which git to make sure it's working.
    2. Install nvm: curl -o- [https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh](https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh) | bash
    3. Install node: nvm install node
    4. Install your version: nvm install <your local version #>
    5. Install yarn: npm install -g yarn@<your version #>
  4. Configure git.

    Your local is already set up to ssh nicely with git. New EC2 instance isn't (yet).

    1. ssh-keygen -t rsa -b 4096 -C "your_email@example.com" Use an empty passphrase when prompted.
    2. Add the key to your ssh-agent with eval "$(ssh-agent -s)" and ssh-add ~/.ssh/id_rsa
    3. If you have problems with this, see step 2 above.
    4. You then need to copy the content of this newly generate id_rsa file into your github security. use pbcopy < ~/.ssh/id_rsa.pub or vim ~/.ssh/id_rsa.pub to do this.
    5. If you need to debug, git has a guide here.
  5. Set up the directory.

    Finally, clone the repo.

    1. git clone <repo .git>
    2. yarn
    3. Any files not pushed to github, like .env, need to be copied over. use scp <drag .env file to terminal> ec2-user@<your host ip address>
    4. if you need this to run while you're not in ec2 terminal, use pm2 start <app.js or runfilename.js>

Conclusion

There are many ways to configure an EC2 instance and set it up, but I hope this barebones guide is useful to beginners debugging their deployment. I'll be writing a future post that demonstrates ways to set up testing, logging, and CICD on EC2.

Top comments (0)