How to deploy NodeJS with PostgreSQL on AWS in 60 seconds
Tags: node, tutorial, aws, beginners
Published: 2020-01-15 13:51:47

This article was originally published at: [Qovery Blog](https://www.qovery.com/blog/the-simplest-way-to-deploy-nodejs-with-postgresql-on-aws)

--------

[AWS](https://aws.amazon.com/) (Amazon Web Services) is an amazing and reliable cloud service provider. AWS, like Google Cloud Platform and Microsoft Azure, provides everything you need to host an application without having to worry about running the underlying servers and network configuration. Everything you need to quickly begin hosting is provided as a packaged services.

However, deploying an application on AWS presents some challenges. The typical deployment workflow looks like this: write code, push it to Git, compile code, deploy code, validate your changes and repeat. Developers thus not only have to do all of this manually, but they also have to configure tons of services (VPC, database, cache, DNS, CDN, etc.) to make their application live on the web.

[Qovery](https://www.qovery.com) was created to solve this problem. In this blog post, I will show you how Qovery improves the developer experience to deploy staging and production [NodeJS](https://nodejs.org) application with [PostgreSQL](https://www.postgresql.org/) database on AWS. You will be able to focus on writing the best code instead of managing complex services.

# Prerequisites
* Operating system: MacOS / Windows / Linux
* A Github account

# Optional: Get the NodeJS sample application

Get a local copy of the NodeJS sample project by forking the project using your Github account and by executing the following command:

⚠️ **Do not forget to fork** the project and change the URL below with yours

```bash
git clone -b tutorial git@github.com:YOUR_GITHUB_USERNAME/simple-example-node-with-postgresql.git
```

# Step 1: Install the Qovery CLI

The [Qovery CLI](https://github.com/Qovery/qovery-cli) is an open-source project that lets you deploy your applications directly on AWS, while taking advantage of well-known local tooling. We will use this tool to speed up our development cycle instead of using the typical development workflow (based on a CI/CD, Docker and other DevOps tools).

Install the Qovery CLI by running the following command:

MacOS

```bash
brew tap Qovery/qovery-cli
brew install qovery-cli
```

Linux

```bash
curl -s https://get.qovery.com | bash
```

Windows

```bash
scoop bucket add qovery https://github.com/Qovery/scoop-qovery-cli
scoop install qovery-cli
```

Authenticate yourself before using it‍‍

```bash
qovery auth
```

# Step 2: Deploy the NodeJS application on AWS

To deploy the NodeJS application connected to a PostgreSQL database, you need to have a .qovery.yml file and a [Dockerfile](https://docs.docker.com/engine/reference/builder/) (already provided) at the root of your project. This file indicate the external resources (eg. PostgreSQL) that your application need to work properly.

**Optional (but recommended)**: Qovery provides a [Javascript client](https://docs.qovery.com/extending-qovery/sdks#nodejs-coming-soon) to simplify the way to retrieve the PostgreSQL instance host, port, username and password.

To create the .qovery.yml file, run the following command:‍‍
```bash
qovery init
```

The .qovery.yml file is created at the root of the project directory
```
cat .qovery.yml
```

Output

```yml
application:
  name: simple-example-node-with-postgresql
  project: simple-example-node-with-postgresql
  cloud_region: aws/eu-west-3
  publicly_accessible: true
databases:
- type: postgresql
  version: "11.5"
  name: my-postgresql-3498225
routers:
- name: main
  routes:
  - application_name: simple-example-node-with-postgresql
    paths:
    - /*
```

Note: Qovery supports multiple databases (eg. PostgreSQL, MySQL, MongoDB, Redis, Memcached, Cassandra), brokers (eg. RabbitMQ, Kafka) and storage services(eg. S3).

Authorize the Qovery Github application to get access to your Github account through [this link](https://github.com/apps/qovery/installations/new). After that, you just need to commit and push the .qovery.yml file in order to deploy your app.

```bash
git add .qovery.yml .gitignore
git commit -m “add .qovery.yml and update .gitignore”
git push -u origin tutorial
```

Voila! Qovery is now deploying your app!

‍
⚠️ BETA: During the Beta phase, the first deployment requires 26 minutes (the database requires a longer processing time while being deployed for the first time due to AWS). After that, each update will take only 2 minutes. Wait a couple of minutes and type the following command to get your application endpoint:

```bash
qovery status
```

Output

```bash
Environment
branch  status  endpoints                                    applications  databases  brokers  storage
master  LIVE    https://x52d28s8iu22mwmi-main-gtw.qovery.io  1             1          0        0

Applications
name                                 status  databases  brokers  storage
simple-example-node-with-postgresql  LIVE    1          0        0

Databases
name                   status  type        version  endpoint                                                                                     port  username   password          application
my-postgresql-3498225  LIVE    POSTGRESQL  11.5     x52d28s8iu22mwmi-rq1xywcyi8wqqcmt-b5pcw97rq8456u5z.cnuxtlki1yn9.eu-west-3.rds.amazonaws.com  5432  superuser  ****************  simple-example-node-with-postgresql

Brokers
name  status  type  version  endpoint  port  username  password  application

Storage
name  status  type  version  endpoint  port  username  password  application
```

# Bonus 1: Test the NodeJS application locally

The Qovery motto is: if your application runs locally, then your application will run on Qovery. To test if your application is running locally, execute the following command:

Warning: Docker runtime required.

```bash
qovery run
```

Output

```bash
...
Step 6/7 : EXPOSE 3000
 ---> Running in a154409a60a7
 ---> 572b7f72c640
Step 7/7 : CMD node ./bin/www
 ---> Running in e315a1fe7c35
 ---> b8c8f6b25307
Successfully built b8c8f6b25307
```

Navigate to http://localhost:3000 through your web browser and you should see a Welcome message.

Note: **qovery run** connects your application to the PostgreSQL database on AWS.
# Bonus 2: Deploy the application on a staging environment

Qovery has a very powerful feature known as “environment”. Qovery supports the deployment of isolated development environments from your branches, complete with exact copies of all of your data. This is useful for testing changes in isolation before merging them.

So, do you want to create a new feature, fix a bug or make a modification without impacting the production or any other important environment? Type the following commands:

```bash
git checkout -b feat_foo
```

Do your changes if required, and commit + push them

```bash
git push -u origin feat_foo
qovery status
```

Output

```bash
Environment
branch    status  endpoints                                    applications  databases  brokers  storage
feat_foo  LIVE    https://vvzwrr81194ksqaw-main-gtw.qovery.io  1             1          0        0

Applications
name                                 status  databases  brokers  storage
simple-example-node-with-postgresql  LIVE    1          0        0

Databases
name                   status  type        version  endpoint                                                                                     port  username   password          application
my-postgresql-7972832  LIVE    POSTGRESQL  11.5     vvzwrr81194ksqaw-fdhuc83ov12fayoz-kpxoosudsewqxs9b.cnuxtlki1yn9.eu-west-3.rds.amazonaws.com  5432  superuser  ****************  simple-example-node-with-postgresql

Brokers
name  status  type  version  endpoint  port  username  password  application

Storage
name  status  type  version  endpoint  port  username  password  application
```

# Conclusion

Qovery and AWS together bring to developers the full power of simplicity and flexibility while deploying applications. Any developer can now take advantage of AWS in seconds instead of days.

Accelerate your development and start using [Qovery](https://www.qovery.com/) today. Let us know what you think about it on [Twitter](https://twitter.com/Qovery_), or by e-mail.