AWS EC2 Deployment Guide
gortron
Posted on May 3, 2020
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
-
Create a new EC2 instance.
Defaults / free tier are fine, except for Security Groups and Key Pair.
- The included port 22 is for SSH.
- Open Port 80. Can use your laptop IP address or leave it as 0.0.0.0 which leaves it exposed to any IP.
- 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. - Make a new Key Pair (.pem) and download it. Move it to
~/.ssh
. You can do that withmv <drag file to terminal to get current path> ~/.ssh
-
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.
-
chmod 400 ~/.ssh/<your key>
. If you need to debug this step, usels -lh <filepath>
, which will tell you permissions. should just ber
at the front. ssh -i identityFile ec2-user@<your host ip address>
ssh-add ~/.ssh/<your key>.pem
-
eval "$(ssh-agent -s)"
- starts the ssh-agent - Finally,
ssh ec2-user@<your host ip address>
-
-
Install necessary software.
You'll need git at a minimum. Rest is app-specific. This example is for nvm/node/yarn.
-
sudo yum install git
.which git
to make sure it's working. - 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
- Install node:
nvm install node
- Install your version:
nvm install <your local version #>
- Install yarn:
npm install -g yarn@<your version #>
-
-
Configure git.
Your local is already set up to ssh nicely with git. New EC2 instance isn't (yet).
-
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Use an empty passphrase when prompted. - Add the key to your ssh-agent with
eval "$(ssh-agent -s)"
andssh-add ~/.ssh/id_rsa
- If you have problems with this, see step 2 above.
- You then need to copy the content of this newly generate
id_rsa
file into your github security. usepbcopy < ~/.ssh/id_rsa.pub
orvim ~/.ssh/id_rsa.pub
to do this. - If you need to debug, git has a guide here.
-
-
Set up the directory.
Finally, clone the repo.
git clone <repo .git>
yarn
- Any files not pushed to github, like
.env
, need to be copied over. usescp <drag .env file to terminal> ec2-user@<your host ip address>
- 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.
Posted on May 3, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.