Deploying a Node.js App with AWS Elastic Beanstalk
Tags: javascript, node, aws
Published: 2022-11-02 21:53:50

*This article was originally written by [Samson Omojola](https://www.honeybadger.io/blog/authors/omojolasamsonade/) on the [Honeybadger Developer Blog](https://www.honeybadger.io/blog/node-elastic-beanstalk/).*

[Amazon’s Elastic Beanstalk](https://aws.amazon.com/elasticbeanstalk/) makes it easy to deploy and scale your applications. You can deploy applications built with various languages using this tool. It abstracts away all the complexities involved in deployment. All you have to do is upload your code, and Elastic Beanstalk takes care of the rest. It also provides you with additional services, such as [load balancing](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.managing.elb.html), [health monitoring](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.healthstatus.html), and [auto scaling](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.managing.as.html). In this tutorial, I’ll walk you through how to deploy a Node JS application with AWS Elastic Beanstalk.

## Prerequisites

To follow this tutorial, you should have [Node](https://nodejs.org/en/download/) installed.

## Step 1 - Creating a Simple Node JS Application

You can use the [express-generator](https://expressjs.com/en/starter/generator.html) tool to whip up a quick Node application by creating a new directory and running the following command inside it:

```bash
npx express-generator
```

Now you should have a new Express application. You can run `npm install` to install all the application’s dependencies. To see the application, run `npm start`  and navigate to `http://localhost:3000` in your browser.

![New Express application](https://www.honeybadger.io/images/blog/posts/node-elastic-beanstalk/Screenshot284.png)
_New Express application_

## Step 2 - Creating an EB Application

To deploy the newly created application, navigate to your [AWS Elastic Beanstalk environment](https://console.aws.amazon.com/elasticbeanstalk/home?p=elbk&cp=bn&ad=c) and click on **Create Application**.

On the Create a web app page, do the following:

- Give your application a name.
- Select **Node.js** under **Platform**.
- Leave **Sample application** as the selected option under **Application code**.
- Click on **Create Application**.

![Create an Elastic Beanstalk application](https://www.honeybadger.io/images/blog/posts/node-elastic-beanstalk/Screenshot285.png)
_Create an Elastic Beanstalk application_

After some minutes, your new EB application should be ready and live. You can view it by clicking on the link auto-generated for you on the top-left side of the page.

![Elastic Beanstalk application is ready and live](https://www.honeybadger.io/images/blog/posts/node-elastic-beanstalk/Screenshot286.png)
_Elastic Beanstalk application is ready and live_

![Elastic Beanstalk application is ready and live](https://www.honeybadger.io/images/blog/posts/node-elastic-beanstalk/Screenshot287.png)
_Elastic Beanstalk application is ready and live_

## Step 3 - Manual Deployment

First, we need to zip our project files so that they can be uploaded all at once. Navigate to your project directory, select all the files and folders excluding **node_modules**, and compress them into a zip file.

Navigate back to your Elastic Beanstalk console, and on the left-hand pane, select the environment you created earlier. As you can see below, mine is 'Honeynode-env'.

![Elastic Beanstalk environment is on the left](https://www.honeybadger.io/images/blog/posts/node-elastic-beanstalk/Screenshot293.png)
_Elastic Beanstalk environment is on the left_

Next, select **Upload and deploy**.

Click on **Choose file**, select the zip file you just created, and click on **Deploy**.

![Upload and deploy](https://www.honeybadger.io/images/blog/posts/node-elastic-beanstalk/Screenshot295.png)
_Upload and deploy_

After a few minutes, the deploy should be complete. Now, if you click on the URL that was generated by AWS EB, you should see your deployed app.

## Continuous Integration and Deployment(CI/CD)

CI/CD involves automating the building, testing, and deployment of an application. This process eliminates the errors often made when manually performing these tasks. It also saves your team a lot of effort and time.

With CI/CD, code changes made to an application can be tested and deployed automatically, enabling customers get improvements to your application and new features quickly.

We can automate the deployment of a Node JS application to AWS EB using AWS CodePipeline.
We’ll upload our application code to GitHub and use CodePipeline to connect the GitHub repo to Elastic Beanstalk.

## Step 1 - Pushing Local Repo to GitHub

Open your GitHub account, create a new repo, and through your CLI, push your local repo to the remote repo.

![create a new repository](https://www.honeybadger.io/images/blog/posts/node-elastic-beanstalk/Screenshot296.png)
_create a new repository_

```bash
git init
git add .
git commit -m "first commit"
git remote add origin https://github.com/username/projectname.git
git push origin master
```

## Step 2 - Creating a Pipeline

Next, go to the search bar in your AWS console, search for **CodePipeline**, and click on it.

![Search for CodePipeline](https://www.honeybadger.io/images/blog/posts/node-elastic-beanstalk/Screenshot297.png)
_Search for CodePipeline_

On the CodePipeline console, click on **Create pipeline**.

![Create CodePipeline](https://www.honeybadger.io/images/blog/posts/node-elastic-beanstalk/Screenshot298.png)
_Create CodePipeline_

Give your pipeline a name (which can be anything).

To have CodePipeline create a new service role for you, select **New service role**. A new **role name** will be auto-generated for you.

![Pipeline settings](https://www.honeybadger.io/images/blog/posts/node-elastic-beanstalk/Screenshot299.png)
_Pipeline settings_

Click on **Next**.

Now we need to point AWS CodePipeline to the remote repo whose deployment we want to automate. In our case, it’s a GitHub repo. After adding the repo to CodePipeline, every subsequent commit made to the repo will be automatically deployed.

## Step 3 - Connecting the Pipeline to GitHub

On the **Add Source** page, select GitHub (Version 2) as your code source provider.

If you don’t have an existing GitHub connection, click on **Connect to GitHub** to give AWS CodePipeline access to your GitHub account and repositories.

![Add source](https://www.honeybadger.io/images/blog/posts/node-elastic-beanstalk/Screenshot300.png)
_Add source_

In the new window or tab that pops up, give your connection a name (which can be anything).

Click on the **Connect to GitHub** button.

![Create a connection](https://www.honeybadger.io/images/blog/posts/node-elastic-beanstalk/Screenshot302.png)
_Create a connection_

On the next page, AWS Connector for GitHub requests permission to verify your GitHub identity and control access to your resources. To grant permission, click on **Authorize AWS Connector for GitHub**.

![AWS Connector for GitHub](https://www.honeybadger.io/images/blog/posts/node-elastic-beanstalk/Screenshot303.png)
_AWS Connector for GitHub_

Upon authorization, you’ll be redirected back to the **Create connection** page.

![You’ll get redirected after authorization](https://www.honeybadger.io/images/blog/posts/node-elastic-beanstalk/Screenshot304.png)
_You get redirected after authorization_

To have GitHub Apps generate a link to your GitHub to be used by CodePipeline, click on **Install a new app**.

![Install a new app](https://www.honeybadger.io/images/blog/posts/node-elastic-beanstalk/Screenshot305.png)
_Install a new app_

This time, you’ll be redirected to a page to select the GitHub account or organization to which you want to connect. Select the appropriate option.

![Select the appropriate account](/images/blog/posts/node-elastic-beanstalk/Screenshot306.png)
_Select the appropriate account_

Next, you’ll be prompted to decide whether you want to give AWS access to all the repositories in your account or only specific ones. Here, you can select the option you prefer. I’ll choose **All repositories**.

![All repositories](https://www.honeybadger.io/images/blog/posts/node-elastic-beanstalk/Screenshot307.png)
_All repositories_

Click on **Install**.

Upon installation, you’ll be redirected to the **Create connection** page.

![You get redirected](https://www.honeybadger.io/images/blog/posts/node-elastic-beanstalk/Screenshot308.png)
_You get redirected_

Click on **Connect** to complete the process.

![Connect to GitHub](/images/blog/posts/node-elastic-beanstalk/Screenshot309.png)
_Connect to GitHub_

Now, back on the Add source page, you should see a **Ready to connect** message.

![Ready to connect](https://www.honeybadger.io/images/blog/posts/node-elastic-beanstalk/Screenshot310.png)
_Ready to connect_

If you click on the **Repository name** search box, you should see a list of your repositories. Select the one you want to deploy.

Next, select the Branch name (**master** for me).

Click on **Next**.

Build stage is optional, so we can skip it. Click **Skip build stage**.

## Step 4 - Connecting Our Pipeline to a Deployment Tool

Under **Deploy provider**, select **AWS Elastic Beanstalk** as the tool through which your app will be deployed.

Under **Region**, leave the default region in which your pipeline was created.

For **Application name**, select the application that you created in AWS Elastic Beanstalk.

Under **Environment name**, select the appropriate environment for the application you chose above.

Click on **Next**.

![Deploy stage](https://www.honeybadger.io/images/blog/posts/node-elastic-beanstalk/Screenshot311.png)
_Deploy stage_

On the **Review** page, go through all the options you’ve selected and ensure that you’ve made no errors. If everything is as it’s supposed to be, click on **Create pipeline** to complete the process.

![Review page](https://www.honeybadger.io/images/blog/posts/node-elastic-beanstalk/Screenshot312.png)
_Review page_

Now your code will be deployed from the source to Elastic Beanstalk.

If you load your Elastic Beanstalk URL, you should see your newly deployed app. If you commit and push any changes to your GitHub repo, it should be reflected in your Elastic Beanstalk application.

![Newly deployed app](https://www.honeybadger.io/images/blog/posts/node-elastic-beanstalk/Screenshot313.png)
_newly deployed app_

## Conclusion

You’ve made it to the end of the tutorial! We’ve created an Elastic Beanstalk application and pushed our local Node JS code to Elastic Beanstalk. We also implemented CI/CD by hosting our Node JS code on GitHub, creating a pipeline with AWS CodePipeline, and using the pipeline to automate deployment of our code to Elastic Beanstalk. To avoid incurring costs and having your credit card charged by AWS, make sure to delete all the applications and environments you set up on the platform.