Get node.js up and running in AWS EC2 by building from source

First we need to get an EC2 instance up and running using 64-bit Amazon Linux. I like to do this from their command line tools commonly known as their CLI. You can also do this from their console. Here we indicate that we want to use a small instance in the US East 1a data center.

ec2-run-instances ami-1624987f -k KO_Keypair --instance-type m1.small -z us-east-1a

This will take a few minutes to get this guy up and running. I look for running instances by issuing this command.

ec2-describe-instances | grep INSTANCE | grep running

Once the instance is up and running, I ssh into the machine.

ssh -i AWS_KEY_FILE ec2-user@MACHINE_NAME.compute-1.amazonaws.com

Let’s udpate all of the software on the machine and make sure that we have modern versions of everything.

sudo yum update

Let’s grab all the dependencies we need to build and operate node.

sudo yum install gcc-c++ make openssl-devel git

Now we are going to make a directory so we can download and build node.

mkdir ~/node
cd ~/node

Let’s download the most recent version of node and extract it into the ~/node directory

curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1

Configure the build for this operating system and environment.

./configure

Now we build node. grab some coffee. this will take a while (20 min or so). When it’s done we will install it.

make
sudo make install

Let’s kick the tires to make sure everything worked. You can CTRL-C twice within the node console to exit.

[ec2-user@ip-10-170-61-122 ~]$ node
> console.log('it worked');
it worked
undefined
>

Let’s clean up the node build directory now that everything has been installed.

sudo rm -rf ~/node

Ok, we’re done but that was kind of a pain. Let’s save this machine so we can resurrect it later and use node without going through all of that hassle. Amazon has a doc on this here.

ec2-create-image -n "AWS Linux 64-bit EBS with Node.js v0.10.13" i-YOUR_MACHINE_INSTANCE_HERE

AWS will return the name of the AMI so you can use that the next time you want to spin up node. If you want a new version of node you will have to start from scratch. Rats!!!!

Now the next time you want a machine like this with node installed you can spin it up with ease. And all of the software will be pretty much up to date so you won’t have to download as many security patches.

Leave a Reply

Your email address will not be published. Required fields are marked *