# Auto Scaling

## Load Balance

Load Balancer helps handle the request from 1 endpoint to 1 or more instance of the server. Normally, if we have 2 servers, the first request, will go to server A. Then, the second request will be send to server B instead. And the next one will be server A.

This means that, to make the server performs better. We can just add more server instance instead to share the work loaded. However, this also mean that, there are more servers we have to tend to. This also including how the server handle the local file if any.&#x20;

There are 2 ways of scaling the server

* Vertical Scaling - Adding more resource to the server such as increase more RAM
* Horizontal Scaling - Adding more server into load balancer

Both ways have the same cost for the same amount of resources but, with Horizontal Scaling, we do not need to keep config the server every time there is a change to the server resource.

As we have prepared code to be able to support the Horizontal Scaling and still functioning the same way. Including the way we have to upload the code to the server now. So, the developer no longer needs to update the code on each server.

While upgrading the server size, the server has to go down first until it finish the upgrade. Adding more instance, however, does not required any down time. As, it is just create another server, and until the server is ready, the load balancer will not give the request to that server.

## Create AMI and setup for Auto Scaling Groups

### Create AMI from Existing Instance

* Go to AWS - EC2 page > Instances
* Select a single instance
* Click on **Actions** > **Image and templates > Create image**
* Enter Image name
  * Should try checking on existing IAM to understand how to name the image to make it easier to mange
* Click **Create image**
* This process mays take a while, and will down a server a few minutes

### Update Launch Template

* Go to AWS - EC2 page > Launch templates
* Select a single template
* Click on **Actions** > **Modify template (Create new version)**&#x20;
* Assign Template version description
* on **Application and OS Images (Amazon Machine Image)**
* Select **My AMIs > Owned by me**
* Select the AMI you want to replace the current one
* Click **Create template version**
* Back to Launch templates page
* Select above instance
* Click **Actions >** **Set default version**
* Choose the template version you want as default
* Click **Set as default version**

## Increase/Decrease EC2 Instance

As we have a main server for each server, the minimum amount of the server we required on normal day should be 0. As this mean, it will has 1 server up and running. So, increase the server in Auto Scaling Group by 1 will mean, we have 2 servers.

1. Go to AWS console and select service **EC2**.
2. On the left panel, select **Auto Scaling Groups** from **Auto Scaling** section.
3. Click on the name of the Auto Scaling Groups you want to update.
4. You will be in the **Details** tab. Click **Edit** on **Group Details**.
5. Change the **Desired Capacity** to the amount of the number of instance you want. If it is more than **Maximum capacity**. Just increase this number as well. Then click **Update**.
6. After a few minutes, it will create or delete the instance(s) in **Instances** page.

## Add New Reader Instance for RDS

With multiple reader instances, when connect to Cluster endpoint, it will share the request between all the instance. This will help ease the load on a single instance.

* Go AWS - RDS page > Databases
* Select a single **Regional cluster**
* Click **Actions** > **Add reader**
* **Instance configuration**
  * select which size of the new instance should be
* **Connectivity > Public access**
  * If it is for **Writer** Instance, should always be **Publicly accessible**
    * This is to allow our Digital Ocean be able to access the RDS
* Click **Add reader**

## (RDS) Change Another Instance to Writer

* Go AWS - RDS page > Databases
* Select a single **Writer instance**
* Click **Actions** > **Failover**

## Add RDS Replica

**Need to contact AWS support, as the happenn-api can no longer create replica**&#x20;

For RDS, as we can only has 1 Database but we have to add **Reader Instance** instead to make it act the same way as the Load Balancer for EC2.

1. Go to AWS console and select service **RDS**.
2. From left navigation, click on **Databases**.&#x20;
3. Click on **happenn-api.** (Should be the first one with Role: **Regional Cluster**)
4. Click **Actions** at the top right and click **Add replica auto scaling.**
5. In the page
   1. **Policy Name** - Can be anything.
   2. **Target Value** - The default is 50% of the CPU has been reached.
   3. **Cluster Capacity Details**
      1. Minimum Capacity - Specify the initial number of the Reader instance.
6. Click **Add policy**.

This process will take sometime before it kick in as it need to create a new **Reader Instance**.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.happenn.com/for-dev/aws/auto-scaling.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
