How to host Generated on the Flatlogic Platform Applications on AWS

1. Create EC2 instance

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/

  2. Select the desired region in the top right corner

  3. Click “Launch instance”

  4. Select “Debian” in “Application and OS Images”

  5. Select “64-bit” under “Architecture”

  6. Select “t3.small” under “Instance type” (you can select a larger instance

depending on workload)

  1. Click “Create new key pair” (RSA, .pem) under “Key pair (login)”

  2. Enter any key pair name

  3. Click “Create key pair”

! Important ! Download this Key Pair and make sure you remember where you saved it because you will need to use it to connect to the EC2 instance

  1. Click “Allow HTTP traffic from Internet” and “Allow HTTPS traffic from

Internet”

  1. Click “Launch instance”

  2. Wait for the environment to enter “Running” instance state;

2. Connect to EC2 instance

Scroll guide to step 2. There is a description of how to connect to an AWS instance.
1. You should open the terminal.

  1. Navigate to the place where the downloaded key is.

  2. Type the command: ssh -i “{{your key name}}” “{{Public IPv4 DNS}}”. You can copy this command from AWS (Instances → Instance Name → Connect → SSH client) like is mentioned in the guide step 2.

3. Install required packages for instance

When you connect to the instance, you should install the required packages:

sudo apt update

sudo apt -y install git

sudo apt -y install nginx

sudo apt -y install postgresql postgresql-client

You should install the node. Here it uses v18:

curl  -s  https://deb.nodesource.com/setup_18.x  |  sudo  bash  
sudo  apt-get  install  nodejs

It installs node and npm. Then install pm2 and yarn:

sudo npm install -g pm2

sudo npm install -g yarn

4. Generate SSH keys for the git connection

Generate SSH key pair:

ssh-keygen -t ed25519

This command will show the SSH key:

cat .ssh/id_ed25519.pub

Then you should copy the FULL string and paste it to your git account.

Paste to: Git → Settings → SSH and GPG keys → New SSH key. You should enter any Title; Key type: Authentication Key; Key is generated SSH key.

Then run the following:

ssh-keyscan  -t  ed25519  github.com  >>  ~/.ssh/known_hosts

This command appends GitHub's SSH key to your known_hosts file. You can check if everything is ok by command:

ssh -T [email protected]

5. Configure database

Change database user and password:

sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'NEW_PASSWORD_HERE'";

NEWPASSWORDHERE is the value (DB_PASS field) from ecosystem.config.js file from your project. It is ‘postgres’.

All database seeds and migrations are run automatically during the deployment process. You can use pm2 lib for deploying. During the process, a database is created according to config. All migrations are run (post-deploy instructions in ecosystem.config.js file).

So the developer does not need to create or update any database in ec2, it is created/updated automatically. The developer only needs to configure the correct password for the DB, as it was mentioned at the beginning of this section.

If you need to update the database manually, you can use postgresql service. You should start postgresql service:

sudo  service  postgresql  start 

6. Configure nginx

Open nginx config file:

sudo nano /etc/nginx/sites-enabled/default

Copy nginx configuration, paste it to the file, and save it:

map  $sent_http_content_type  $expires  {

"text/html"  epoch;

"text/html; charset=utf-8"  epoch;

default  off;

}

server  {

listen  80;

server_name  your-domain;  #  setup  your  domain  here if you have

gzip  on;

gzip_types  text/plain  application/xml  text/css

application/javascript;

gzip_min_length  1000;

location  /  {

expires  $expires;

proxy_redirect  off;

proxy_set_header  Host  $host;

proxy_set_header  X-Real-IP  $remote_addr;

proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;

proxy_set_header  X-Forwarded-Proto  $scheme;

proxy_read_timeout  1m;

proxy_connect_timeout  1m;

proxy_pass  http://127.0.0.1:3000;  #set  the  address  of  the instance

}

}

7. Restart database and nginx services

sudo systemctl restart nginx

sudo systemctl restart postgresql

8. Deploy the project via pm2

First you should download pm2 for your local machine: npm install pm2 -g.

Clone the repo on your local machine: git clone {{you project git url};

Check ecosystem.config.js file. In the deploy section, you will see configurations of how the project can be deployed. In your production section:

  • key - is the path to the .pem key that you downloaded when creating an instance. You need this to connect to the instance. Check if the path is correct;

    • host - is the Public IPv4 address from AWS instance info. You can find it if select AWS instance;
    • ref - is the name of the git branch that you want to deploy. Now it is origin/main, but it can be different (f.ex. origin/master).

Open the terminal and navigate to the project folder. Type:

pm2 deploy production setup

It will pull the git project to your AWS instance and configure everything to run it.

  1. Then deploy:

    pm2 deploy production

It will run the project. You will see it on your Public IPv4 address. (it should be http, not https)

Also, you can check what is running via pm2 list command. And server logs via

pm2 monit