SwapStation_WebApp/DEPLOYMENT.md

4.3 KiB

Swap Station Web Application - AWS Deployment Guide

This document provides detailed instructions for deploying the Swap Station web application to a Linux server on AWS.

The application is containerized using Docker and managed with Docker Compose for ease of deployment and configuration. 0. Prerequisite: Setting Up the Production Database (AWS RDS)

Before deploying the application, a PostgreSQL database must be created. The recommended service for this is Amazon RDS. The person deploying the application will need to perform these steps in the AWS Management Console. Steps to Create the Database:

Launch an RDS Instance:

    Navigate to the RDS service in the AWS Console.

    Choose to "Create database" with the "Standard create" option.

    Select PostgreSQL as the engine type. Choose a recent version (e.g., PostgreSQL 15 or higher).

    Under "Templates", select the "Free tier" option for testing or a suitable production instance size.

    Under "Settings", create a DB instance identifier (e.g., swapstation-db), a Master username, and a Master password. Securely store these credentials.

Configure Networking & Security:

    Ensure the RDS instance is launched in the same VPC as your EC2 instance (the server that will run the application).

    In the "Connectivity" section, find the Security Group settings. Create a new security group for the database (e.g., rds-sg).

    After the database is created, you must configure this security group to allow inbound traffic from your application server. Go to the security group's "Inbound rules" and add a rule that allows traffic on the PostgreSQL port (5432) from the security group of your EC2 instance. This is a critical step to allow the application to connect to the database.

Get the Connection Details:

    Once the database is created and available, select it from the RDS dashboard.

    In the "Connectivity & security" tab, you will find the Endpoint (this is the database host) and the Port.

Constructing the DATABASE_URL:

Use the details from the steps above to build the full connection string. The format is: postgresql://:@:/

<user>: The Master username you created.

<password>: The Master password you created.

<host>: The Endpoint from the RDS console.

<port>: The Port (usually 5432).

<dbname>: The initial database name you provided during setup (often postgres if not specified).

This full string is the value you will use for DATABASE_URL in the .env file.

  1. Developer Preparation (Completed)

    Backend (main.py): The application is configured to read the DATABASE_URL from an environment variable.

    Frontend (JavaScript files): All API endpoints are relative, making them independent of the server's IP or domain.

    Dockerization: The project includes a Dockerfile and docker-compose.yml for easy deployment.

  2. Server Prerequisites

The deployer must have an AWS EC2 instance (Ubuntu 22.04 recommended) with the following installed:

Git

Docker

Docker Compose

Installation on Ubuntu:

sudo apt-get update sudo apt-get install -y git docker.io docker-compose sudo systemctl start docker sudo systemctl enable docker

  1. Deployment Steps Step 3.1: Clone the Project

git clone cd SWAPSTATION_WEBAPP

Step 3.2: Create the Production Configuration File

Copy the example file to a new .env file in the project's root directory:

cp .env.example .env

Open .env with a text editor (e.g., nano .env) and fill in the values:

    DATABASE_URL: The full connection string you constructed in Section 0.

    SECRET_KEY: A strong, randomly generated secret key.

    CORS_ALLOWED_ORIGIN: The public http://<YOUR_SERVER_IP_OR_DNS> of the EC2 instance.

Step 3.3: Build and Run the Application

Build the Docker image

docker-compose build

Run the application in the background

docker-compose up -d

Step 3.4: Verify and Access the Application

Check that the container is running: docker ps

Open a browser and navigate to the public IP address of your server: http://<YOUR_SERVER_PUBLIC_IP_OR_DNS>
  1. Managing the Application

    Check Logs: docker-compose logs -f

    Update Application: git pull, then docker-compose build, then docker-compose up -d

    Stop Application: docker-compose down