> For the complete documentation index, see [llms.txt](https://www.ankitavirani.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.ankitavirani.com/experience/deployment-platforms/aws.md).

# AWS

## 🌐 AWS Server Setup & Website Deployment

### 1. AWS Login 🚪

#### Account Types

* **Root User**: Main account holder, manages everything (email & password).
* **IAM User**: Limited permissions for security (can only perform specific tasks).

### 2. Log in to AWS Console 💻

* Log in to [AWS Management Console](https://aws.amazon.com/console/).
* **Region Selection**: Set to **London** (or choose your preferred region).

### 3. Launch an EC2 Instance 🚀

#### Steps:

1. **Navigate to EC2 Service**:
   * Console > **EC2** > **Instances** > **Launch Instance**.
2. **Instance Name and Tags**:
   * Provide a name for the server, e.g., "Frontend Server", "Backend Server".
3. **Application and OS Images** (Choose the OS for your server):
   * Options:
     * **Ubuntu** (Recommended) 🐧.
     * Amazon Linux.
     * macOS.

#### Example:

* Select **Ubuntu Server 22.04 LTS (HVM)**, SSD Volume Type.

### 4. Instance Details ⚙️

**AWS Architecture Overview:**

<figure><img src="/files/2qYCKit3fWCECqShBveM" alt=""><figcaption></figcaption></figure>

The architecture comprises:

* **Virtual Private Cloud (VPC)** to isolate resources.
* **EC2 Instances** to host the server.
* **S3** for object storage.
* **Security Groups** for managing inbound/outbound traffic.
* Select **64-bit (x86)** for compatibility.

#### Instance Type

* **t2.micro**: Suitable for small web apps, **Free Tier** (React Apps).
* **t2.small**: Suitable for backend services (**Paid**).

#### Key Pair (Login) 🔑

1. Click on **Create new key pair**.
2. Provide a name for the project.
3. Select **File Type: .pem** and **Download**.
   * Save it securely to access the server later.

#### Network Settings 🌐

* Allow SSH Traffic from **anywhere** (tick).
* Allow HTTPS and HTTP Traffic from the **internet** (tick).

#### Configure Storage 💾

* Allocate **30 GB** (default, gp2 SSD).

#### Advanced Details (Optional) ⚙️

* No changes required here.

#### Summary 📝

* Review all settings:
  * **Number of instances**: 1 or more based on requirements.
  * **AMI**: Ubuntu 22.04 LTS.
  * **Instance Type**: t2.micro or t2.small.

### 5. Create and Connect to EC2 Instance 🔌

#### Create the EC2 Instance 🖥️

* Click **Launch**.

#### Connect to the Instance 📡

1. **Create IAM Role**: Attach `AmazonSSMFullAccess` for secure access.
2. Enable **Fleet Manager** host.

#### Connect Using SSH 🔐

```sh
chmod 400 project-key.pem
ssh -i "project-key.pem" ubuntu@<public-ip-address>
```

### 6. Install Dependencies for Web Application ⚙️

#### Become Super User

```sh
sudo su
```

#### Install Node.js & NPM 🌐

1. Follow the guide [here](https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-20-04).
2. Use the first option command.
3. After installing Node.js:

```sh
npm install -g n
n lts
```

* If you encounter errors, refer to the [Node.js Package Manager Guide](https://nodejs.org/en/download/package-manager#n).

#### Install MongoDB 💾

Follow [this tutorial](https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-ubuntu-18-04-source).

If there are issues:

1. Download `libssl1.1`:

```sh
wget http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2_amd64.deb
sudo dpkg -i libssl1.1_1.1.1f-1ubuntu2_amd64.deb
sudo apt-get install -y mongodb-org
```

#### Install Nginx 🌐

1. Follow [this tutorial](https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-20-04).
   * Use `"Nginx Full"` for firewall settings:

```sh
shCopy codesudo ufw allow 'Nginx FULL'
sudo ufw enable
sudo ufw status
sudo ufw reload
```

#### Install PM2 🔄

```sh
sudo npm install pm2 -g
```

### 7. Upload Application Code 📁

#### Upload Frontend & Backend Code

```sh
cd /var/www/html/
```

#### Install NVM (Node Version Manager) 🌐

```sh
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
source ~/.bashrc
nvm install --lts
npm install -g npm
npm install
```

#### Site Configuration for Nginx ⚙️

**Unlink Default Config File**

```sh
cd /etc/nginx/sites-available/
sudo unlink /etc/nginx/sites-available/default
```

**Create New Config File for Frontend & Backend 🖊️**

1. **Frontend Configuration Example**:

```nginx
server {
    root /var/www/html/frontend-build/;
    index index.html;
    server_name yourdomain.com;
    location / {
        try_files $uri /index.html;
    }
}
```

2. **Backend Configuration Example**:

```nginx
server {
    listen 80;
    server_name backend.yourdomain.com;
    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}
```

**Enable Configuration**

```sh
sudo ln -s /etc/nginx/sites-available/example.conf /etc/nginx/sites-enabled
```

**Test and Restart Nginx ⚙️**

```sh
sudo nginx -t
sudo systemctl restart nginx
```

### 8. Set Up SSL 🔒

1. **Install Certbot**:

```sh
shCopy codesudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt install certbot python3-certbot-nginx
```

2. **Generate SSL Certificate**:

```sh
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
```

3. **Renew Certificate**:

```sh
sudo certbot renew --dry-run
```

### 9. Additional Configurations 🛠️

#### Start Services

```sh
sudo systemctl start nginx
sudo systemctl start mongod
```

#### Check Service Status

```sh
sudo systemctl status nginx
sudo systemctl status mongod
```

#### Add IAM Role for SSM 📊

* Assign **SSM Administrator** access to connect easily via AWS Systems Manager.

### 10. DNS Setup 🌍

#### A Records and CNAME

* **Main Website**: Use **A Record** pointing to instance IP.
* **Subdomain** (same instance IP): Use **CNAME Record**.
* **Different IP**: Use **A Record**.

### 11. Extra Commands & Tips 💡

* List PM2 processes:

```sh
pm2 list
```

* Check Certbot Certificates:

```sh
certbot certificates
```

* If stopping and restarting the instance, update **DNS** with the new IP address.

#### **Conclusion** 🎉

AWS provides powerful and flexible tools to build scalable and secure infrastructure for your website. With these steps, you're on your way to mastering AWS deployments!
