How to deploy Express App to AWS EBS with Postgres DB & Automate Deployment with CodePipeline

Petar Georgiev
8 min readDec 9, 2022

--

This tutorial assumes you already have some basic AWS knowledge and an AWS account.

A common problem is the initial setup of the automated deployment and in general, deploying our Express app to AWS. Every one of us wants to have automation when committing to GitHub/GitLab etc. I have also had this issue. It took me some time to figure out the whole process, so I will try to explain it in detail hopefully, it will save everyone reading this a lot of time. Let’s start!

Tools we are going to use from AWS are (some of them automatically from EBS):

  • Elastic Beanstalk (EBS)
  • CodePipeline
  • RDS (Relational Database Service)
  • EC2

Login into your AWS account and enter “elastic beanstalk” in the search bar.

You will see a list of your environments, probably none for you at the moment, and you must click “Create a new environment”

Selection of environment tier is the next thing, we want to click on “Web server environment”, as we are going to deploy an Express app (it needs to be Dockerized) and then Select.

Now a list of options will appear, and we need to fill all of them correctly. Let’s start with the name. We could use whatever we like. About application tags, for now, we are not gonna use them.

Next up “Environment information”, it’s important to check carefully here everything as it’s not gonna be possible to change it later! I personally try to make everything with the same or similar names as it’s easier to read and understand later on.

The next two sections are more or less used for our deployment. In the platform section, we should use Docker and versions whatever we want, but we recommend these settings, so you can be sure that everything will work as in this tutorial.

For the “Application code” we don’t care for now, as we are going to change it later on. We could just choose “Sample application”, just to get us started, as we will connect our Express app with CodePipeline and GitHub for automated deploys.

As we are to the bottom of the page, there is a button “Configure more options” and this is exactly what we are going to do.

Now as we are going to need to be production ready, we are going to use High availability as a Preset. This allows us to, later on, add Load balancers which are going to help us make our app stable and with close to zero downtime.

Time to find the Load balancer section and click on “Edit”.

The best option, in my opinion, is an application load balancer, so we are going to choose it.

In the next section for Listeners, we not going to add anything (if you like here is the place where you can add SSL — 443 Port and your custom SSL certificate). Jumping up to Processes. Here to make it easier for your future, let’s add a process SSL, and change the health check path for both processes to “/health” (we need to create this route in our Express app).

After adding this SSL process, and editing our existing HTTP one, it must look like shown below.

We are done with this section of load balancing, so we click on Save at the bottom, to get us to the previous screen. Next up Database section. Let’s click on Edit so we can add our Postgres database to our EBS environment.

Time to add our database information. Change the engine to Postgres and the version to your preference (mine is 14.4). The instance class db.t3.micro is a free tier one. Storage can be changed later on, but let’s select something which we will not change soon like 20GB. Username and password are a must to remember, as we are going to need them to connect to our DB (they should not have special symbols). Availability as per my preference must be High as we want users to have close to zero downtime. Now time to save Database settings.

Now it’s time to create our Elastic beanstalk environment. Let’s click on Create environment and wait for a couple of minutes (possibly around 10–15, for some users maybe an hour) as AWS creates everything needed for us. In the beginning, you should see a screen with something similar to what is shown below.

We can go from the sidebar to our EBS application, and we can see that the status is still pending. This means we need to wait a little longer until health becomes Ok.

After Health has transitioned to Ok, we can move on to adding CodePipeline to our environment. Search for CodePipeline and click on it. You should see “Create pipeline” and click it.

Fill in the name of your pipeline and click “Next”. I like to name it as my EBS and append “-pipe” to the end.

We need to connect our GitHub account to AWS if we haven’t already. It’s important to use GitHub (Version 2) as a source provider so you can create a connection to it. As I already have a connected account I just select my current connection. Otherwise, you need to click on “Connect to GitHub” give a name to your connection and add it.

After a successful connection to GitHub, we choose a repository that contains our Express App. In the below selection, we want to redeploy our app on any change in “master” branch. Time to go to the next step.

Next up we see Build Provider selection, but as we are going to use Docker for our builds, we don’t need this at the moment. Click on “Skip build stage”.

As for the next step, we cannot skip it. We need to set a deploy provider step. Our choice should be AWS Elastic Beanstalk, as this is where our environment resides. Carefully choose a region of your choice and select below the application name and environment name for your EBS and click Next.

This should let you review everything we wrote for your CodePipeline. Validate that all is correct and click “Create pipeline” which will trigger an immediate deployment using this pipeline and the selected repository branch.

Don’t forget that without a “Dockerfile” you won’t be able to deploy!!!

Something else that is important is that you may need some environment variables, as you probably have those in your application. One way to add them is to go to Elastic Beanstalk and find your environment in the left panel. Click on “Configuration”.

This will redirect you to a screen with all possible configurations you can change for your EBS. We are looking for the category “Software” and we are going to “Edit” it.

We go to the bottom to find “Environment variables” and add whatever we want for our application. As I am using TypeORM with Postgres I need a lot of variables, but for the sake of the tutorial, I won’t be adding them all here. So add yours below and click on “Apply”, which should trigger an automatic redeploy of your instance.

I will just give you an example Dockerfile for an Express App with TypeScript. Also don’t forget that this file should be at the main directory of your App and should be called exactly Dockerfile

#DOCKER FILE for Express App with TypeScript

FROM node:16 as development

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm ci --development

COPY tsconfig*.json ./
COPY src/ src/

RUN npm run build # Build command in package.json -> "build": "tsc",

FROM node:16 as production

WORKDIR /app

COPY package*.json ./

RUN npm ci --production

COPY --from=development /usr/src/app/dist ./dist

EXPOSE 8443 # Our server listens on this PORT.

CMD node dist/index.js

I hope you really liked this tutorial. I tried to be as thorough as possible. The plan for the next article is to write “How to add migrations to your Dockerfile and deployment with TypeORM”, as I think this is a very common problem, which needs to be resolved for every production app.

If you enjoyed reading and want to get notified for my next article, don’t forget to Follow me here in Medium and in Twitter — https://twitter.com/peturgeorgievv Thank you.

--

--

Petar Georgiev

Writer and Developer with React/Express/GraphQL/TypeScript