Learn about the elastic Beanstalk and how to deploy node application using this service

AWS Elastic Beanstalk is a simple-to-use software built with Java to deploy and scale Web apps and services. You can easily upload the code and Elastic Beanstalk manages the implementation completely, from power optimization, load balancing, auto-scaling, and safety tracking for apps.

Why choose Elastic Beanstalk?

Beanstalk has the same elements as we use for the network. A front-load balancer in our system that can be the key to your private network or website. Based on the configurations the number of associated instances is fluid or static. When you want to have a massive number of instances, you can scale over any parameter you can think of, CPU, ram, IO, requests for a network, time, and much more. Last but not least, a relational database can be used as a datastore. The datastore is supported by RDS.

In this post, we’ll lead you through the process of running a Node App with Docker on AWS Elastic Beanstalk.

Appropriations

  • An AWS account.
  • Node installed.
  • A Docker Hub account.
  • Docker installed and some experience using it.
  • A Buddy account

Step 1: Create a simple Node app.

Create and navigate to the directory:

$ mkdir node-docker-eb
$ cd node-docker-eb

Create a package.json, replying yes to all questions:

$ yarn init --yes

Install Express:

$ yarn add express

Create index.js file:

$ touch index.js

Create an index.js file with the following:

The app can then run with this command:

$ node index.js

You should see your app at http://localhost:3000/:

Step 2: Dockerize your Node application.

Before Moving forward let us give you a brief introduction of What is Docker?

Docker is a platform designed to make software easier to build, install, and run using containers. Containers allow the programmer to assemble and distribute an application as one assemble, with all the parts it needs, such as libraries and other dependencies. Through this, the developer can be guaranteed, that the program can run on every other Linux machine independently of any modified settings that the computer may have that may vary from the system used to write and check the code.

Let's Move Forward and follow the below steps to dockerize your application:

Create a Dockerfile in your project directory and populate it with the following code:

Now build and tag your image with the following command (don’t forget the . !):

$ docker build --tag=node-docker-eb .

Then, run the ls the command to find your built image:

$ docker image ls
REPOSITORY TAG IMAGE ID
node-docker-eb latest 57b318b00e74

Run your image with the following command: (Note how to port 80 is mapped to port 3000 with -p)

$ docker run -p 80:3000 node-docker-eb

If you open your browser at http://localhost:80 (which is the same as http://localhost), you should see your app again:

Step 3: Deploy your Node Application ON Elastic Beanstalk.

Head over to AWS and open Elastic Beanstalk. If you have never used this service before, your screen should look like this:

Click Get Started, name your application (we named mine node-docker-eb), set your platform as Docker and select Sample application before clicking Create application:

It will then take a few minutes for your application to spin up but you should eventually see that it has been launched successfully:

Click the provided URL (purple arrow above) to view the sample application:

Step 4: Replace the Sample App With Your Dockerized Node App

To do so, we’ll use the following process:

  1. Develop code locally (Done).
  2. Build a Docker image locally.
  3. Push the built Docker image up to Docker Hub.
  4. Upload a Dockerrun.aws.json file to Elastic Beanstalk. At this point, Elastic Beanstalk will fetch your image off Docker Hub and deploy your application.

Navigate to https://hub.docker.com/ and sign in. If you have no repositories you should see this:

Click Create a Repository, enter a repository name (we chose node-docker-eb), and click Create:

Now, go back to your terminal and build your image. Note how we tagged our image with <username>/<repository_name> — replace these with your own details.

$ docker build --tag andrewbest/node-docker-eb .

Next, push the image you just tagged up to Docker Hub (you will need to be logged in). Again, replace the <username>/<repository_name> with your own details:

$ docker push andrewbest/node-docker-eb

If all goes well, if you navigate back to Docker Hub you should see that your repository has recently been pushed to:

The next step is to get Elastic Beanstalk to fetch your newly pushed image and replace the sample application.

To do this, Elastic Beanstalk uses a file named Dockerrun.aws.json. This file contains a specification detailing how AWS should build and run your app.

To get started, create a Dockerrun.aws.json file.

This file needs a few details:

  • AWSEBDockerrunVersion: Set to 1 as you are using a single Docker container.
  • Image: Specify the Name field to be your Docker Hub repository.
  • Ports: Specify the list of ports you wish to expose on your container. Elastic Beanstalk uses the passed value to connect the container to the reverse proxy running on the host.

My Dockerrun.aws.json looks like this:

The final step is to upload your Dockerrun.aws.json file to Elastic Beanstalk. Navigate to your application and click Upload and Deploy:

Select just the Dockerrun.aws.json file and click Deploy.

Elastic Beanstalk will fetch your image and run it for you. Once your app has been deployed successfully, navigate to your app URL:

Here we go, congratulation you successfully abled to deploy your Node Application using Docker ON AWS Elastic Beanstalk.

Happy Coding!!!!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Wakeupcoders

Wakeupcoders

We make your business smarter and broader through the power of the internet. Researcher | Web developer | Internet of things | AI | www.wakeupcoders.com