What’s fastest way to start up EC2 instance and run Node.js on it which will serve simple http requests?
So, you created Amazon AWS account and wanted to give it a try. As Amazon promised you will have micro instance for one year free of charge, let’s use it 🙂
I found various posts how to run Node.js with already prepared image or some third party EBS ( Elastic Block Store ) images but I wanted to do it from scratch. Here are steps:
0. Log in to AWS Management Console: Here
1. Fire up micro instance, I’m using “Lunch Classic Wizard” with Basic 64-bit Amazon Linux AMI.
2. Setup key pair if you don’t have it already. Download to computer. You will need them to access instance after start up is finished
3. Under “Configure firewall”, be sure to add ssh and http ports exceptions. For ssh, tcp port 22 with source: 0.0.0.0/0 and for http, tcp port 80 with source 0.0.0.0/0. I added range 8000 – 9000 ( just in case I need it later ). Note: for https different port is used than for http.
4. Lunch instance
After instance is lunched you will see instance details under “Instances”. Copy public DNS we will use it to attach to instance ( something like ec2-XX-XX-XX-XX.x.compute.amazonaws.com )
Note: There is possibility to assign static IP address to this instance. This feature is free as I understood as long it’s linked to active instance. If instance is down and given IP is kept you need to pay. Smart way to convince you to run your instance 24×7 :). For more details see Amazon AWS pricing and “Elastic IPs” under AWS Console.
So to continue our journey, you have active instance, so let’s use it:
5. ssh to your instance ssh -i //ec2.pem ec2-user@ec2-XX-XX-XX-XX.x.compute.amazonaws.com ( you will probably need to change certificate permissions to 700 )
6. do sudo su to get the feeling 🙂
You have root access to you machine, let’s install Node.js and run it.
7. Install required packages
yum install gcc gcc-c++ openssl-devel make
8. install Node.js
wget http://nodejs.org/dist/v0.6.5/node-v0.6.5.tar.gz tar -zxf node-v0.6.5.tar.gz cd node-v0.6.5 ./configure make sudo make install sudo chown -R ec2-user /usr/local curl http://npmjs.org/install.sh | sh
After this steps you should have node installed. To check it out you can download some simple node server ( I will use one from one of earlier posts Simple static file HTTP server with Node.js )
9. install git
yum install git
10. become ec2-user ( use exit command if you are root )
11. checkout code to home dir
12. run Node.js
You can test everything by going with you browser to http://ec2-XX-XX-XX-XX.x.compute.amazonaws.com:8000/test.html.
Note: if you want to run Node.js on default HTTP port ( 80 ) you need to do so as root and of course to change server.js code to start server on port 80
13. change server.js
... }).listen(80); ...
14. run Node.js as root
sudo /usr/local/bin/node server.js