Recently I wrote a couple of Node.js apps that use Aerospike and I’ve deployed them on Amazon EC2. In the end, everything seems to have worked out fine since there are a ton of resources out there to help you through the deploy process, but I found those resources to be scattered all over the place. So I thought of taking a stab at putting together a concise set of steps that can help you setup a Node.js app on Amazon EC2 such that it can scale up and down, is fault-tolerant and accessible via a custom domain.
First and foremost, you must have a Node.js app you’ve built and are ready to deploy. It is also presumed that you have an AWS Account and working knowledge of Amazon EBS and Elastic Load Balancing. If not, please visit the links below under Useful Resources section.
Here We Go
Create EBS application with configuration — 64bit Amazon Linux 2014.09 v1.0.9 running Node.js
Deploy your Node.js app via EBS — if it fails (which it does from time to time!), scp your Node.js app files over to /var/app/current on the EBS instance
- If scp’ing your files over:
- /var/app/current folder on the EBS instance should be owned by user ec2-user — if it is not, ssh into the instance, change directory to /var/app/ and run chown ec2-user current
- Change directory to /var/app/current and run sudo npm update to make sure your application dependencies are resolved — in case of errors, try npm install
for the ones that fail and retry - From /var/app/current, run your Node.js app. For example, node server or node app
- If scp’ing your files over:
Browse over to EC2 — https://INSERT-REGION-HERE.console.aws.amazon.com/ec2
- Note: Replace INSERT-REGION-HERE with region where the instance has been launched. For example, us-west2
- Here you should see the EBS instance — make sure it is in running state
On the left, under NETWORK & SECURITY, click on Load Balancers. Then
- Click on Create Load Balancer and
- Make sure it is associated with the EBS instance created for your Node.js application
- Make sure it is “internet-facing”
- Make sure it is configured to forward HTTP requests coming in on port 80 to whichever port your Node.js app is running
- Click on Create Load Balancer and
Make note of Load Balancer DNS Name. Copy-and-paste it in your browser and you should see your app in action
Setup custom domain for your app instead of Load Balancer DNS Name url
- Head on over to your domain provider and create a CNAME record giving it the value of Load Balancer DNS Name
- If all goes well, browsing to your custom domain should now bring up your app
Useful Resources
AWS Amazon Amazon EBS Elastic Load Balancing Deploying AWS Elastic Beanstalk Applications in Node.js Using Eb and Git