Deploying Node.js Apps to AWS Elastic Beanstalk
A quick guide to deploying Node.js on Elastic Beanstalk
A quick guide to deploying Node.js on Elastic Beanstalk
Let’s take a look at how we can deploy a Node.js application to AWS Elastic Beanstalk (EB). EB is not exactly the most straightforward platform and it may take you a couple of hours to figure out how things work. This short and handy guide aims to simplify that for you. Let’s deploy our app in 10 minutes!
What is Elastic Beanstalk?
Elastic Beanstalk is an excellent platform provided by Amazon Web Services (AWS) to help deploy our applications and scale them. You can deploy apps built on various languages or frameworks such as Java, .NET, PHP, etc. We have chosen Node.js here as it is one of the most popular frameworks currently and is beginner-friendly.
The process of deploying is pretty straightforward. We just need to upload the code and EB automatically handles the deployment in the background, abstracting away all the complexities. In fact, you get a whole suite of benefits when deploying through EB such as load balancing, auto-scaling, and even health monitoring. This does not mean you won’t be able to configure these. You still gain granular control over these aspects, but EB just helps to provide these services out-of-the-box.
Note: Elastic Beanstalk itself is free to use. However, you will still be charged for the underlying services it uses. This might include services like Cloudwatch or Amazon S3.
Kickstarting a Simple Node.js App
Let’s create a simple boilerplate app on Node.js. To do this, let’s use Express.js and the express-generator. To learn more about express-generator, visit this link.
Open up the terminal, create a directory folder (e.g. my-app) and use the below command to kickstart our app. Note that Node.js 8.2.0 or above is required for this to work.
npx express-generator
That’s about it, we will have a functional Node.js, Express-based app ready to run. To test it out, visit localhost:3000 and it should give a “Welcome to Express” message.
Creating IAM User and Group on AWS
Next, let’s create an IAM user and group for Elastic Beanstalk on AWS. To do this, let’s go to the AWS Console and sign in.
Select “IAM” from the list of services.
Under the IAM service, let’s click on the “Users” section on the left menu and click “Add user”.
In this section, let’s name our user as “deploy-eb” and select “Programmatic access”. Click on Next to continue.
In the next section, click on “Create group”.
Let’s name our group as “eb-deploy-group” and from the search box, let’s look for “AdministratorAccess-AWSElasticBeanstalk” and select it. Click on “Create group”.
After this step, we can speed through the other settings by just clicking Next all the way. Finally, we get the Access key ID and the Secret access key. We will need this, so do download the CSV file and keep it handy.
Configure Profile on AWS CLI
Next, we will jump back to our terminal and configure the above profile in our CLI. At this point, you need to have AWS CLI installed on your computer. Do check out this link if you need to install the CLI.
To configure the profile, use the command:
aws configure --profile <profile_name>
I have chosen to use the same name for the profile as we did for our user, deploy-eb
. The AWS CLI will now ask for the access ID, secret key, and region. Enter the information using the CSV we downloaded in the previous step and for the region, check it out on your AWS console. Usually, you can see the region in the console URL itself. My region is ap-southeast-1
which I have used as shown below. We can just press enter for the default output format.
Initialising an EB App
In this step, we will initialise an EB app using the CLI. In this step, we will need some CLI tools specific to EB installed. Do check this link if you need to setup EB.
Enter eb init --profile deploy-eb
to initialise an EB project based on the profile we created earlier.
There will be a list of prompts as shown below. You can choose the region, for most parts just use the default options. I have chosen to name the app as “eb-node-app”, but feel free to name it as you like. For the prompt on setting up SSH, we can use “No” as it is not required at this point.
Initialising an Environment
Just like how apps maintain various versions of environments, such as release, QA, development etc., EB uses the same concept. Let’s create an environment for our app by entering eb create
. It should look something like the following:
Once you see a stream of event logs, you can follow along to see the various operations EB is doing. This will include activities like uploading our code to S3, creating load balancing configurations, auto-scaling groups, creating security groups to govern the inbound/outbound rules, Cloudwatch monitoring etc.
At this point, if you do a ls -al
on the application directory, you will notice a new folder called .elasticbeanstalk
. Inside this, there will be a single file called config.yml
which should look like this:
Back to AWS Console
Now, let’s go back toour AWS Console. Let’s visit the “Elastic Beanstalk” service and you should be able to see your app running there.
Click and go into the app and we will see that our EB app is successfully deployed and the Node.js versions, the health check status everything will be automatically shown here in the dashboard.
The URL highlighted in the blue link is the domain of our Node.js app and … Voila! Clicking on it will bring us to our hosted website!
Deploying Changes
To demonstrate how easy it is to push changes to our newly hosted EB app, I am going to make a small change to our code. If you have used the Express generator toolchain to create a sample app, you can go into routes/index.js
and modify the title key to be something like “My Elastic Beanstalk App”. The code will look something like this.
var express = require("express");
var router = express.Router();
/* GET home page. */
router.get("/", function (req, res, next) {
res.render("index", { title: "My Elastic Beanstalk App" });
});
module.exports = router;
Now, to push this changes, I just need a single command: eb deploy
. Enter this command on the terminal and you should see this:
Now let’s refresh our website and the changes are already up! That’s how fast we can deploy changes.
Conclusion
Elastic Beanstalk is an excellent way to deploy many applications. This guide is a quick start to deploying Node.js apps to EB in a jiffy! EB is powerful when it comes to abstracting the complexities of scaling, monitoring, and DevOps from you so that you can remain focused on the development of your application.
Hope this guide was useful! Be sure to leave your comments below and do explore the various capabilities of EB.
Happy coding! 💻