Intro to AWS EC2 instances
Launch an Amazon Web Service EC2 instance and install Node.js and NPM to host your JavaScript applications.
i. Intro
ii. Setup
iii. Implementation
a. Launch instance
b. Set Security Group
c. Connect to Instance
d. Install Node and NPM
e. Easy Login Set Up
iv. Conclusion
v. Credits
Intro
Why make a guide on setting up an EC2 instance? There are many guides out there to help set up an EC2 instance. Well, when I was learning how to do it myself I found that I had to reference many different guides and websites at once in order to get everything working.
My goal is to create a simple one stop guide that has the tricks I learned from many different guides all in one place. I hope this guide will be easy to follow but also be thorough enough so that there is little room to get lost in the labyrinth that is Amazon Web Services.
I will also cover some neat tricks that will help make logging into your EC2 instance easier and this will require that you have generated an SSH key beforehand.
What this guide will cover:
- Setting up an EC2 instance
- Logging into the EC2 instance
- Installing Node.js and NPM
- (Extra) Configuring your EC2 instance for easy login in the future
Setup
- Create an Amazon Web Service Account
- Generate (or have handy) an SSH key
Implementation
a. Launch Instance
- Log into Amazon Web Services
2. On the dashboard click EC2
3. In the upper right hand corner of the screen choose where you want your instance(s) to be located
4. Click Key Pairs (under Network & Security)
5. Click Create Key Pair. Name the pair and save these somewhere safe! You can only download them once.
6. On the EC2 Dashboard, click on Instances (under Instances)
7. Click Launch Instance
8. Select Amazon Linux AMI
9. Click Review and Launch
10. Click Launch
11. Select “Choose an existing key pair”
12. Choose the key pair that you want to use when launching this instance
13. Click Launch Instances
b. Set Security Group
1. Go back to your EC2 Dashboard
2. Click Security Group
3. Select the security group associated with the instance you just launched. You can check this under Instances in the EC2 Dashboard.
4. Click Actions
5. Click Edit inbound rules
6. Click Add Rule to allow connections from IP addresses to a port of your choosing when testing your project
7. Click Save
c. Connect to Instance
1. Go back to your EC2 Dashboard
2. Click Instances
3. Click instance you want to connect to
4. Click Connect
5. In your local terminal navigate to where your keys from part a step 5 were saved (in my case it is on the Desktop)
6. Copy and paste the link provided on the AWS pop-up page into your terminal
7. You are now logged in!
d. Install Node and NPM
1. Update to have the latest packages
sudo yum update -y
2. Install required packages
sudo yum install -y gcc gcc-c++ make openssl-devel
3. Go into the tmp directory
cd tmp
4. Download the Node.js source code by going to the Node.js download page and copy the URL of the “Source Code” package
5. Download the package to your instance. The following link is an example and your results may vary depending on when Node.js updates again
NOTE: That is a “O” in the following command and not a “0” (zero).
curl -O https://node.js.org/dist/v6.11.4/node-v6.11.4.tar.gz
6. Unpack and cleanup
tar -xvf node-v6.11.4.tar.gz
rm node-v6.11.4.tar.gz
7. Configure, Make, and then Make Install. This step will take a while
cd node-v6.11.4
./configure
make
sudo make install
8. Verify that Node and NPM are installed by typing:
node -v
npm -v
9. If “sudo npm” returns a “sudo: npm: command not found” then try creating the following links:
sudo ln -s /usr/local/bin/node /usr/bin/node
sudo ln -s /usr/local/lib/node /usr/lib/node
sudo ln -s /usr/local/bin/npm /usr/bin/npm
10. Your EC2 instance is ready to hold JavaScript Node.js applications!
e. Easy Login Setup
1. Have your SSH keys handy
2. Go into your local .ssh directory or where your ssh keys are saved (not your EC instance)
cd .ssh
3. Copy the contents of the keys. Can use the “cat” command to display it. For mac users you can use ‘pbcopy < ~/.ssh/SSH_KEY.pub’
cat test.pub
4. Log into your EC2 instance.
5. Don’t navigate anywhere else in your EC2 instance and type in:
echo 'PASTE-KEYS-FROM-PART-E-STEP-3-HERE' >> .ssh/authorized_keys
chmod 640 .ssh/authorized_keys
chmod 750 .ssh
6. Navigate to your local .ssh folder (not EC2 instance) on your terminal
7. Edit the config file located in the .ssh folder
vi config
8. Now type in:
Host CHOOSE-ANY-NAME-HERE
Hostname TYPE-EC2-ADDRESS(IP or address)-HERE
User TYPE-EC2-USER-NAME-HERE (usually this is 'ec2-user' for an AMI EC2 instance)
IdentityFile TYPE-FILE-PATH-TO-SSH-KEYS-YOU-GENERATED-IN-SETUP-HERE
Here’s an example to refer to in case you are confused by the above notation:
Host testserver
Hostname ec2-127.0.0.1.us-west-1.compute.amazonaws.com
User ec2-user
IdentityFile ~/.ssh/test
9) Save and quit
:wq
10) Now you can log into your EC2 instance from any directory in your local terminal
ssh testserver
Congratulations on making it this far! Your EC2 instance is ready to go to serve your basic deployment needs and you won’t need your PEM key in order to login anymore.
Conclusion
There are many steps and configuration settings that I did not go over, but this is the very bare bones of setting up your very own AWS EC2 server installed with Node and NPM. I hope that you find my “short” tutorial useful. Tune in for another update next week!
Credits
More indepth AWS guide: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EC2_GetStarted.html
Installing node/npm guide: https://gist.github.com/nrollr/325e9bc4c35a0523d290b38cfa3c5142
Configuring EC2 for easy login guide: http://devoncmather.com/setting-aws-ec2-instance-lamp-git/