There are limitless options for building a web application server on AWS, but lets define some basic requirements :
- Eligible for/compatible with the “AWS Free Usage Tier”
- Runs on Amazon Linux (the Amazon Linux images seem to be better optimized for EC2)
- Install Node.JS support with npm and the Express, Socket.io, and Mongoose packages
- Install MongoDB
- Use Git-based deployment of our code to AWS
- Implement a webserver application using the Node & Express framework we install
Apps I use on my Mac for local development and managing AWS
Your AWS account and server instances are of course managed entirely online, but there are a few applications you will need on your Mac to develop and deploy applications to AWS.
I like to use the same IDE for coding and managing projects, whatever programming language I’m using. Aptana is a pretty universal open IDE that integrates well with Git for source control and Firefox for debugging & testing.
An editor i sometimes use for coding when an IDE is overkill.
vSSH is a customizable SSH and Telnet client with macros and keys sharing. I use this app on my Mac and iPad to remotely log into my AWS instances. Get it from the Mac App Store here
Using Git to deploy my application to the server
I develop my applications locally using Aptana Studio, and use Git for version control.
I also deploy the application to the production server (my EC2 instance) using git, giving me the ability to roll back to previous versions when I break something.
The workflow is simply:
On my Mac:
cd myapp # make changes ... git commit git push
then when I’m ready to deploy to the server, I log into the AWS instance using vSSH and clone the git repository there
mkdir myapp cd myapp git clone https://github.com:/my_name/path_to_myapp_repository
to checkout the latest version from the repository on github, then
to start the Node server and run my application
Getting started with EC2
Let’s start by creating an account at Amazon Web Services.
Visit https://aws-portal.amazon.com/gp/aws/developer/registration/index.html and log in or create an account.
When you have created your account, open the AWS Management Console — a simple-to-use web interface for starting and managing your virtual servers.
In order to install and run Node, we first need to set up our first virtual server to install it onto.
Amazon calls its virtual servers instances. For now, you only need one instance. An EC2 instance is similar to a regular unmanaged web-server.
Here’s how we create our first instance on AWS:
1. Select EC2 (Amazon’s Elastic Compute Cloud) from the list of services AWS provides
2. In the menu bar you find an item labelled “Region”. Click it to switch to your nearest or chosen region where you want to locate your server. The cost varies between locations (although it’s free for the first year).
Now give your instance a name.
Type a name for your key pair file, e.g. “myhosts” and download it to your Mac.
Your key file must not be publicly viewable for SSH to work. So open a terminal window on your Mac and use this command to restrict access rights to the file :
chmod 400 myhosts.pem
Then choose an AMI (Amazon Machine Image) for your server. I use Amazon Linux 64 bit, which is tuned for security and performance in the cloud. I chose 64bit due to reported compatibility issues between the 32bit version and some versions of Node.
There are other Linux AMIs that you can use, which have Node and other software pre-installed, which you may use as a short-cut later. But for our purposes, if you start with a clean installation and configure everything yourself, you’ll be able to better understand how to administer and maintain your instances in future.
Your instance is now launching and that may take a few minutes. Click the View your instances on the Instances page link and you will be redirected to your “My Instances” page, where you can monitor and configure all your EC2 instances:
You can now use your instance’s public dns to connect to your server via ssh. Amazon does not provide a custom username and password for ssh connections. Instead, they use the key pair file you created a couple of steps ago (a more secure method of authentication).
Connect to Amazon EC2 with vSSH
Configure your security private key
OpenPreferences, navigate to the Security tab. click + and open the .pem file you saved earlier.
Once this is added, close the preferences window.
Connect with vSSH
Enter your instance’s public DNS address shown in the EC2 console in the Host Name field. in the Private Key field, select the myhosts.pem key file in the drop down box
the terminal window will appear. if asked to log in, enter ‘ec2-user’ You are now logged-in into your instance!
Update your EC2 Amazon Linux
Security updates are automatically applied on the initial boot of the AMI. Upon login, the Message of the Day (/etc/motd) indicates whether or not any additional updates are available.
The Amazon Linux distribution uses the yum configuration manager.
To install updates, enter :
sudo yum update
Type y when prompted if this is ok and wait a few minutes for the updates to install.
Install Node.js and NPM on your server
At last, it’s time to install Node.js on your Amazon Linux server! We are going to install some required packages to compile Node and get Node from its Github repo. Type the following:
sudo yum install gcc-c++ make sudo yum install openssl-devel sudo yum install git git clone git://github.com/joyent/node.git cd node
Now you need to decide which version of Node you want to install. You can view all available Node tags (versions) by typing git tag -l. The make command will take a while to complete (30+ minutes), so go make a coffee and come back later.
git checkout v0.8.1 ./configure make sudo make install
OK, Node is now installed!
Let’s add it to sudo’s path so that we can install more packages. You will need to edit the /etc/sudoers file.
sudo vi /etc/sudoers
find this line:
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
move the cursor to the end of the line and hit the insert button to append the text
exit insert mode, save your changes and exit VI, by hitting ESC and typing :w!
The next step is to install NPM (the Node package manager).
Type the following commands:
git clone https://github.com/isaacs/npm.git cd npm sudo make install
You now have a working Amazon EC2 instance with Node.js and NPM, and you’re ready to install any additional Node library packages you need !
Installing Node library packages
You can install additional Node packages using NPM like this :
sudo npm install express -g sudo npm install socket.io -g
go to security Groups on your AWS EC2 dashboard.
Add new inbound rules for the security route :
- All ICMP (to allow you to ping your server), and
- custom 8080 (if thats the port your web server will be using)
- for IP address, just use 0.0.0.0/0 to allow access from all IP-addresses
End of Part 1
I hope this guide was useful. If you have any comments, suggestions or additional tips that could be added to the guide, please let me know in the comment section below.
Go on to Part 2 – Adding a Database
Next, in Part 2 of the series, we’ll add a database