Deploy Node.
js Application on AWS using
Elastic Beanstalk
In this document, we will deploy a Node.js application using Elastic Beanstalk through
the command line.
Elastic Beanstalk?
AWS Elastic Beanstalk is a Platform as a Service (PaaS) offered by Amazon Web Services
(AWS). It simplifies the process of deploying, managing, and scaling web applications
and services developed with various programming languages and frameworks, including
Java, .NET, PHP, Node.js, Python, Ruby, Go, and Docker.
Key Features of Elastic Beanstalk
1. Easy Deployment and Management: Elastic Beanstalk automatically handles the
deployment, from capacity provisioning, load balancing, and auto-scaling to
application health monitoring.
2. Supports Multiple Languages and Frameworks: It supports a variety of platforms,
making it versatile for different development stacks.
3. Environment Management: You can easily manage different environments
(development, testing, production) with isolated resources.
4. Full Control: While it automates the management of your application, you still
retain full control over the underlying AWS resources.
5. Integrated Monitoring: It integrates with AWS CloudWatch to provide metrics and
logs for your application.
6. Customization: You can customize the environment using configuration files and
hooks to modify the underlying resources.
How Elastic Beanstalk Works
1. Application Upload: You start by uploading your application code (e.g., a ZIP file,
WAR file) to Elastic Beanstalk.
2. Environment Creation: Elastic Beanstalk creates an environment where your
application will run. This environment includes all the necessary resources, such
as EC2 instances, load balancers, auto-scaling groups, and RDS databases, based
on your configurations.
3. Configuration: You can configure your environment using the Elastic Beanstalk
management console, CLI, or configuration files in your application source code.
4. Deployment: Elastic Beanstalk handles the deployment of your application to the
environment. It provisions and configures the necessary resources.
5. Monitoring and Management: Once deployed, Elastic Beanstalk monitors the
health of the application and the resources. You can scale the application, update
the environment, and manage the resources using the Elastic Beanstalk console
or CLI.
6. Updates and Versioning: You can deploy new versions of your application,
rollback to previous versions, and manage application versions easily.
Prerequisites:
● A host machine with aws cli installed.with this instance type t2.medium
● The host is configured with IAM user profile with AWS
AdministratorAccess-AWSElasticBeanstalk policy attached to it.
Step 1: Clone the Node.js Project
Firstly, clone the Node.js project from GitHub. You can use the following URL:
https://github.com/xoor-io/elasticbeanstalk-helloworld/tree/master
Step 2: Install EB CLI on EC2
1. Update Packages
Before installing new software, update the package repositories and installed
packages:
sudo yum update -y
2. Install Python and Pip
Python and pip are usually pre-installed on Amazon Linux. If they are not installed, use
the following commands:
sudo yum install python3 -y
sudo yum install python3-pip -y
3. Install EB CLI
Once Python and pip are installed, use pip to install the EB CLI:
pip3 install --upgrade --user awsebcli
4. Verify Installation
Verify the installation of the EB CLI:
eb --version
You can see its install successfully
Step 3: Create Beanstalk Environment
1. Initialize Git
● Navigate to your project directory and initialize git:
git init
● 2. Create a .gitignore File
● Create a .gitignore file and add the following lines to exclude unnecessary
files:
node_modules/
.gitignore
.elasticbeanstalk/
● 3. Initialize Elastic Beanstalk
● Run the following command to initialize Elastic Beanstalk in the us-east-1
region:
eb init --platform node.js --region us-east-2
This will create an application, which you can view on the AWS
console.
We can see it on aws console.
Step 4: Create a Procfile
A Procfile is used to specify the commands that are executed when your application
starts.
1. Create a Procfile
Create a file named Procfile in the root directory of your project and add the following
content:
vi Procfile
web: npm start
Procfile
A Procfile is a file used to explicitly declare the commands that are run by your
application's containers on platforms like Heroku and AWS Elastic Beanstalk. The
Procfile is used to specify the processes that are executed when your application
starts. It is especially useful for defining web processes and worker processes.
Purpose of Procfile
Defining the Command to Run Your App: The main purpose of the Procfile is to specify
the command that should be run to start your application. This is critical for web
applications to ensure the correct server process is initiated.
● Save and Commit the Changes
● Save the Procfile and commit the changes using git:
git add .
git commit -m "Initial commit with Procfile"
● Create the environment using the following command.
eb create
You will be prompted to enter the environment name, DNS CNAME prefix, and load
balancer type. You can use the default settings.
2. Handle Platform Version Error
If you encounter the following error:
Run this command to select the correct platform:
eb platform select
When prompted, confirm the platform:
It appears you are using Node.js. Is this correct?
(Y/n): Y
Select the appropriate platform version.
3. Create the Environment Again
Run the following command to create the environment without errors:
Step 6: Verify Deployment
1. Check Environment Status
Check the status of your environment:
eb status
Ensure the environment health is green, the application is deployed successfully. we
can see on aws console.
● 2. Access the Application
● Copy the CNAME from the Elastic Beanstalk console and use your browser to
access the application.
This document should now be in a clear and organized format, making it easy to follow
the steps for deploying a Node.js application on AWS Elastic Beanstalk.
Thanks and regards
Aamir Qureshi