Cloud computing has revolutionized the IT World. It is an innovative platform and It has established itself as the backbone of the technology for the future. It allows organizations to implement and launch applications in such a way, which is next to impossible with that traditional infrastructure.
Cloud Computing has reshaped the entire Application development process. With the emergence of several cloud service providers like Microsoft Azure, Google Cloud or Amazon Web Services, which offer a variety of development tools and services, which make the application development process more robust and secure. But choosing Which one is better AWS or Azure or Google cloud? is the real question! AWS Elastic Beanstalk is a development tool launched by Amazon, and it has been implemented based on the PaaS model.
Cloud Computing has been able to deliver such a success an exceptional success with its PaaS service. Amazon has its PaaS service, which is famously known as AWS Elastic Beanstalk. AWS Elastic Beanstalk is a service that helps us in the deployment and scaling of web applications and services developed with .NET, Java, Node.js, PHP, Go, Python development, Docker, and Ruby on renowned servers like IIS, Nginx, Apache, and Passenger.
AWS Elastic Beanstalk facilitates rapid deployment and management of applications in the AWS cloud without worrying about the underlying infrastructure which supports those applications and their related services. Elastic Beanstalk takes care of all the details of the Application’s versions, load balancing, application scaling, capacity provisioning, and its health monitoring. It offers flexibility to the developer to deploy an application without provisioning the infrastructure while maintaining the high availability of application services.
Benefits of AWS Elastic Beanstalk
Here are some important benefits that AWS Elastic Beanstalk offers over other PaaS services in the market.
It allows users to share their applications across different sets of Devices and Architecture with high security and scalability. It also offers a thorough report of User profiles and application usage.
It offers an ability to the developers to deploy their applications in the most rapid and rapid way possible. It is possible for developers to deploy and launch their applications within a few minutes straight. This gives them a big upper hand as they do not have to worry about the resource configuration and underlying infrastructure.
Provides absolute Resource Control :
Beanstalk provides developers the freedom to choose the AWS resources, that are optimal for their application. It gives developers an absolute control over all the AWS resources and access them whenever it is required.
Transparent and Simplified Operations:
It operates and provisions the computing infrastructure and at the same time, it manages the application stack. It makes the process too transparent and simplified, those application developers have to just focus on writing the code for their application, and they don’t have to spend time configuring and managing the servers, networks, databases, and firewalls.
In this article, we would be explaining the process of setting up continuous deployment for a Node.js application on Elastic Beanstalk. We will be initializing an environment on the Elastic Beanstalk console and make some configuration changes on our Node.js application. We will cover the deployment process as well.
What is Node.js?
Node.js- It is getting more popularity in recent times due to the availability of a huge number of plugins it supports and its unbelievably better spin-up times than Java or C#. Which makes it the best bet for developing client-facing applications. It enjoys enormous support from its dedicated community of developers and contributors, which makes it easy to discuss the problems and finding their solutions.
What is Elastic Beanstalk CLI
It is a command-line tool, which helps us managing AWS Elastic Beanstalk environments. We can also use EB CLI to monitor our environment’s status and health parameters in real-time. Once we install and configure the EB CLI, we can launch a new environment and proceed with the deployment of our code via the eb create command. If we already created an environment in place in EB console, then we can attach the EB CLI to it, by running eb init in a project folder.
Deploying Node.js Applications to Elastic Beanstalk
It is extremely easy to deploy, manage and scale a Node.js application on AWS Elastic Beanstalk using the Amazon web services. Elastic Beanstalk for Node.js is used for hosting or developing a web application using Node.js. Past deployment of our EB app, we can use EB Command Line Interface to manage our app and environment, or you can use the Elastic Beanstalk console, APIs, or AWS Command-line interfaces.
Roadmap of our Deployment
Our deployment roadmap will have the following basic steps.
- We will be creating an IAM user to handle deployments.
- We will create an EB app and set up an environment.
- We will deploy our Node.js app to the EB application using a command-line interface.
Pre-requisites for a successful deployment
- Basic knowledge of command-line interface.
- An active AWS account
- The Elastic Beanstalk Command Line Interface (EB CLI)
- Basic knowledge of Git
Step by the Step Deployment Process
Step -1 – Setting up an AWS Account
The first and foremost thing is to set up an AWS account. If you do not have one, then please register yourself at the AWS portal. If you already have an AWS account, then please make sure you have an IAM user that has API keys and an adequate level of access.
It is a pretty straightforward process to create an account. The AWS account signup process should walk you through everything smoothly. When you first set up an AWS account, you will get root access. However, it is considered as a security best practice to create a different user that you will use to login on regular basis.
It is always a good practice to have control over the roles and responsibilities within the AWS account. Whenever you start a new project, please create a new IAM user with specific permissions, which is aligned to the specific project needs.
Let us get this started. Please head over to your AWS console and open the IAM console. Select the Users option on the left side menu. Here, hit that blue “Add user” button to create a new user account. We should create a new eb-deploy-user user, that will be having programmatic access through the Command-line interface (CLI). So please tick that checkbox.
Step -2 Creation of Group
The next step about defining the permissions for the newly added User. Here the thumb rule is, you should always follow a restrictive approach while delegating the permissions. In this case we are supposed to use only Elastic Bean deployment capabilities, hence we will just assign the Elastic Beanstalk Full Access permission to the new User. We will be creating a new Group called eb-full-access and assign that group to our new user.
Continue hitting next, you should get the review screen that looks like this.
Here it is important to note that, we will have to use these access keys and secrets soon. You can either leave this window open or just download the CSV file using the “Download .csv” button.
Now we must open the console on our system and have to configure a new profile for the AWS cli. We can call this new profile eb-deploy-user. We will use the AWS to configure the tool to fulfill this function.
Once the new profile is created, we can confirm it by opening the ~/.aws/credentials file and see that if the new profile is available there. Till now we have set up the Authentication and now let us move ahead and create our first EB application.
Step -3 – Creating the Elastic Beanstalk App
We will be using the EB CLI tool to perform all the further actions. To get started, we will have to initialize our Node.js project as an Elastic Beanstalk project. To proceed further, we can use the eb init command, it will help us passing an argument for a new user profile, which we have already created using the –profile option.
For the sake of demonstration, we have chosen all the default options here. However, you are free to make the required configurations based on your project and regional requirements. Now, If you check the Elastic Beanstalk console in your browser, you will find the new application names elastic beanstalk-HelloWorld listed there. Here it is important to note, that you must choose the correct Zone and Region to get the appropriate level of services.
Now we will proceed further with the creation of our first Elastic Beanstalk environment. In EB, the top-level entity it an Application, then within the application we can have several environments, like staging, development, or production environment. Here, we will be creating a single environment to show how this entire process functions.
Step -4 – Creating the EB Environment
We will proceed with the creation of our first environment, by using the eb create command, that is offered by the EB Command Line Interface. Here, we are again using all the default options for now to avoid any complexities.
Now, as our EB application has been created and Environment has been set up and in place, we will notice a new directory in the root of our Node.js project, the directory called .elasticbeanstalk. You will also find a YML file in this directory, along with the deployment configuration.
This file contains all the necessary information, used by the EB Command Line interface to deploy our application. It contains a branch-defaults property, which helps EB to decide to which environment it should start the deployment, depending on the local GIT branch we are standing at.
In the current scenario, we are standing at the master branch, hence it will start application deployment to the elastic beanstalk-HelloWorld-dev environment we have just created. If required, we can anytime create separate environments and assign specific GIT branches to them and deployment could be performed there as well.
Here, we have to be a little bit patient as the Environment creation may take a few minutes as Elastic Beanstalk will create the EC2 instance and make the necessary configuration changes. As a default option, it will also attempt to proceed with the deployment of our application, but this deployment may fail, we need to make further configuration changes to ensure this works fine.
Step -5 – Configuring the Node Command
In the previous step, we did talk about the failure of initial deployment, the reason behind that failure is because Elastic Beanstalk looks at the application code and it found an app.js file in the directory. By default, when EB sees an app.js file, it considers it as an entry file and attempts to run node app.js.
In our case, we will be running the application using the npm start command. In a way or another, we will tell EB to use that command, instead of trying to run our app.js file on its own.
Now, access the left side menu and select the configuration option, within your application in the EB console and click the “Modify” button within the “Software” box:
Here we will see an empty field called “Node command”. We need to enter “npm start” as the start command and click the “Apply configuration” button on the top right corner to proceed further.
We can also change the Node version in our instance, and we can make other configuration changes as well. We would recommend you go over all the configuration settings, as it will help you to understand the EB platform in a better way.
Once we are finished with all the configuration changes, the EB will once again try to run the application automatically using the new node command. Once it runs all the initialization steps, you will get a green status in your dashboard, which means the deployment has been successful.
Now you can click the environment URL and you will see the ‘Express hello world’ page in your browser.
Step -6 – Deploying your app from the console
Now, as we are done with all the configuration steps, it is time to deploy the application directly from our console, without accessing the AWS console in the browser. All we need to do is to run the eb deploy command and that is where we will get the successful application deployment.
Here, we want to share a quick tip for you, please make sure that you always commit the changes you want to deploy to the branch configured our config.yml file. Once we hit the deploy command, it always searches for our local branch and initiates a build using that. God forbids, if you have uncommitted changes, then those will not be available in the deployed application.
Step -7 Get a custom domain name
As we are done with the Application deployment and it can be seen in the browser as well. But if you run eb open command, we will observe that application URL is very long. If required, then we can make the necessary changes using Route 53.
Step -8 Get an SSL Certification
An SSL certification is a de facto requirement for ensuring security for our web application. It is very easy to get an SSL certificate for our application instance. We can visit the certificate manager and start the process of creating a new certificate for our domain, it is quite an easy and effective process.
We can sign for an SSL cert for CloudFront, all we need is to start the process from the target zone and Region. We can always make the changes in our Zone at this screen.
Once we verify it and it is ready to use, we can add this to our EB configuration, we can use the following steps to add the SSL certification in the configuration.
- Go to your EB dashboard
- Choose your application
- Choose your environment
- Click on “configuration” and choose the SSL certificate.
Step – 9 – ssh into the EC2 instance
Here we have few commands to do the ssh into the EC2 instance.
To ssh into the EB EC2 instance, we can run the following command
$ eb ssh <environment_name>
To navigate to the location of the application files, we can run the following command
$ cd /var/app/current
Step -10 – Debugging
Debugging is an important aspect of mobile application development. Sometimes, the application status may not be ‘green’, it may be degraded, that means there might be some problem, which should be assessed and fixed. We can run the following command to view the logs and take the appropriate action.
$ eb logs
Step – 11 – Deploying an update
If we want to deploy an update to the application, we can run the following command
$ eb deploy
Step – 12 – Setting environment variables
If you want to set an environment variable using the CLI, run the following command
$ eb setenv [VAR_NAME=VALUE]
Step – 13 – For more commands, run
$ eb –help
We hope these steps will help you with the deployment of Node.js application to AWS Elastic Beanstalk. Now we can go ahead and deploy to multiple environments in EB directly from your local console using a single command. We can deploy any valid Node.js application in this way, including much more complex and practical applications.
The biggest advantage is that AWS Elastic Beanstalk is itself free. All we need to pay is for the underlying resources which we are using, like EC2 instances. It also offers 12 months of free 750 hours of EC2 per month under the free tier payment model.
Rahul Mathur is the founder and managing director of ARKA Softwares, a company renowned for its outstanding mobile app development and web development solutions as well as specialized in Android and iOS app development. Delivering high-end modern solutions to all over the globe, Rahul takes pleasure in sharing his experiences and views on latest technological trends.