# AWS CLI, Session Manager & Database Access Guide

## 1. Setup
- Download **AWS CLI**
- Download **AWS Session Manager Plugin**
- Generate **Key for KCCB**

____________________________________________________________
### Production: (Run in systemctl)
 - cd /etc/systemd/system
  <!-- IB is the service name -->
 - vi IB.service
  ```
    [Unit]
    Description= Internet Banking Frontened Application in Node
    After=network.target

    [Service]
    # Use absolute path for node or npm
    User=ib
    Group=ib
    ExecStart=/usr/bin/npm start
    WorkingDirectory=/home/ib/IB
    Restart=on-failure
    RestartSec=5
    Environment=NODE_ENV=production
    Environment=PORT=3000
    SuccessExitStatus=143

    [Install]
    WantedBy=multi-user.target

    <All value are changed as per domain>
  ```
 - sudo systemctl status IB
 - sudo systemctl start IB  
 - sudo systemctl stop IB 
 - sudo systemctl restart IB
 
---

## Machine 
```bash

UAT (IB- frontend Test) : i-0b55435e15425f1c3
Linux : i-0c850dcf8b85b1447 (Test)
Prod : i-088e64c3435cb5078 (For IB & MB)
```

## 2. list of Port :
  - 8686 (fetch the customer details)
  - 5432 (postgres)
  - 8080 (IB- backend port)
  - 8688 (fetch the account statement)
  - 8687 (for customer name fetch in CBS -- when add beneficiary)
  - 8689 (payment port)
  - 6379( redis port)


## 3. Port Forwarding

### API (localhost → EC2:8080)
```bash
aws ssm start-session --target i-0c850dcf8b85b1447 --document-name --profile kccb AWS-StartPortForwardingSession --parameters "portNumber"=["8080"],"localPortNumber"=["8080"]
```
### PostgreSQL (localhost:5431 → EC2:5432)
```bash
aws ssm start-session --target i-0c850dcf8b85b1447 --document-name --profile kccb AWS-StartPortForwardingSession --parameters "portNumber"=["5432"],"localPortNumber"=["5431"]
```
### CBS (localhost → EC2:8686)

```bash
aws ssm start-session --target i-0c850dcf8b85b1447 --document-name --profile kccb AWS-StartPortForwardingSession --parameters "portNumber"=["8686"],"localPortNumber"=["8686"]
```

## Database Access

```bash
aws ssm start-session --target i-0c850dcf8b85b1447 --profile kccb
```
## 4.PostgreSQL Commands

```bash
# Connect as postgres
psql -U postgres

# List all databases
\l

# Connect to kmobile
psql -U admin -d kmobile

# Password: kmobile

# Query data
SELECT * FROM users;

# Expanded display
\x

# See all tables
\d

# Table details
\d users;

# Switch database with specific user
\c kmobile_banking kmobile_app_rw

# Grant permissions
grant select, insert, update, delete on table admin to kmobile_app_rw;
```
## 5. Linux Machine Access

```bash
# Run the .bat file first
ssh username@localhost
# Enter your password
```
## 6.Copy File from Local → Linux

```bash
scp -P 9022 Smsservice/smsserviceapplication.jar <username>@localhost:/home/<username>

# ssh nabanita@localhost -p 9022
```
## 7.About Backend

- If user "is_first_login" = true means Users did not login in IB.
- **ib_access_level** or **mb_access_level**
  - **0** → Disabled  
  - **1** → Transaction  
  - **2** → Read Only
  -**null** → not configured consider as disabled


