How to deploy Nodejs web app on AWS elastic beanstalk

Abinash Phulkonwar
3 min readDec 2, 2021
AWS elastic beanstalk node js deployment
Photo by Christopher Gower on Unsplash

To deploy a Node.js app on AWS elastic beanstalk you need to have an AWS account. And then you need AWS CLI to interact with AWS elastic beanstalk. To get the AWS CLI you need to install Python and make sure the Python version is higher than 2.7 and try to install the latest version of Python.

Python comes with a pip installer. To get the AWS CLI you need to run this command on your terminal.

pip install awsebcli --upgrade --user

Add the executable path, %USERPROFILE%\AppData\roaming\Python\Python39\scriptsto your PATH environment variable. The location might be different, depending on whether you install the Python version. Python39 replace with your python version if the python version is 3.8 then it Python38and if it is 4.0 then it Python40 .

with the AWS CLI eb cli comes with it. eb cli use to interact with AWS elastic beanstalk.

To Verify that the EB CLI is installed correctly. Run this command.

eb --version

if the output is something like that EB CLI 3.14.8 (Python 3.9).

To see more information on the installation process you can visit here docs.aws.amazon.com.

after the installation, you need to configure the EB CLI

To initialize an EB CLI project run this command. (make sure you run this command in your project directory).

eb init

the EB CLI prompts you to select a region. Type the number that corresponds to the region that you want to use, and then press Enter.

Next, provide your access key and secret key so that the EB CLI can manage resources for you. To create an access key and secret key you need to create an IAM user with programmatic access and select an elastic beanstalk-related policy for the user. And download the CSV file that AWS gives you. And open the file and you get information about the user that you create. and copy and paste the access key and secret key that you found here.

aws-access-id: your access key

aws-secret-key: your secret key

then eb cli asks you to Select an application to use or create an application. Select your option.

then you need to Select a platform Node.js, Python, Ruby, Docker, Go, Java.

then just select what you want for your project.

For learning more about eb cli visit here docs.aws.amazon.com.

You can see the root of your project directory .elasticbeanstalk file.

make sure you put this file name in .gitignore . also, eb cli use git for deployment.

Create an Elastic Beanstalk environment

Create an environment running a sample application with the eb create command.

eb create --sample your-environment-name

This command creates an environment with the default settings for the Node.js platform and resources.

eb open you can use this command to open the sample app on your browser.

Procfile setup

include a file called Procfile at the root of your source directory to specify the command that starts your application. and put this command.

web: node app.js

Configuring your application’s dependencies

In your package.json file you need to provide a starts command like this “scripts”: { “start”: “node app.js” } and also put the dependencies in there. elastic beanstalk run this command npm install --production to install the dependencies. and then run npm start to start the app. also you need to set your Node.js and NPM version in there like this “engines”: { “node”: “14.16.0”, “npm”: “6.14.12” }.

And you need to set NODE_ENV to production eb setenv NODE_ENV= production

and set the port to app.listen(process.env.PORT || 3000)

And then commit your code

git add .

git commit -m "commit your code"

eb cli use the commited code to deploy.

Then you run eb deploy to deploy your code to AWS elastic beanstalk.

If everything is right then your app deploys successfully.

You can visit here to learn more about deployment docs.aws.amazon.com.

If it not working then download the full logs of your environment and open it and see what wrong is happening.

And every time you need to deploy an app or API. please visit the official website of the cloud provider and see how to deploy. And go throw at least enough to you need the deployment of the app or API.

Thank you!

--

--