> ## Documentation Index
> Fetch the complete documentation index at: https://cloudanix.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Rds underutilized instance remediation

### Triage and Remediation

<Tabs>
  <Tab title="Remediation">
    ### Remediation

    <AccordionGroup>
      <Accordion title="Using Console" defaultOpen="true">
        To remediate the issue of underutilized RDS instances in AWS, follow these steps using the AWS Management Console:

        1. **Identify Underutilized RDS Instances**:
           * Navigate to the AWS Management Console and open the Amazon RDS console.
           * Click on "Databases" from the left-hand menu to view all your RDS instances.
           * Look for instances with low CPU utilization, low memory usage, or low storage usage compared to their allocated resources.

        2. **Modify Instance Type**:
           * Select the underutilized RDS instance that you want to remediate.
           * Click on the "Modify" button at the top to change the instance type.
           * Choose a more appropriate instance type based on the workload requirements and expected usage patterns.
           * Click on "Apply immediately" to apply the changes.

        3. **Enable Auto Scaling**:
           * In the RDS console, select the RDS instance that you want to enable Auto Scaling for.
           * Click on the "Modify" button to make changes to the instance.
           * Scroll down to the "Auto Scaling" section and click on "Enable Auto Scaling".
           * Configure the minimum and maximum values for CPU utilization that should trigger scaling actions.
           * Click on "Apply immediately" to save the changes.

        4. **Implement Performance Insights**:
           * Performance Insights helps you identify performance issues and underutilized resources in your RDS instance.
           * In the RDS console, select the instance you want to monitor.
           * Click on the "Modify" button and scroll down to the "Enable Performance Insights" section.
           * Enable Performance Insights and set the data retention period.
           * Click on "Apply immediately" to save the changes.

        5. **Monitor and Optimize**:
           * Regularly monitor the performance metrics of your RDS instances using CloudWatch or Performance Insights.
           * Adjust the instance type, storage, and other configurations based on the actual workload requirements.
           * Consider implementing RDS Proxy for connection pooling and better resource utilization.

        By following these steps, you can remediate the issue of underutilized RDS instances in AWS and optimize your resources for better performance and cost-efficiency.

        #
      </Accordion>

      <Accordion title="Using CLI">
        To remediate the issue of underutilized RDS instances in AWS using AWS CLI, you can follow these steps:

        1. **Identify Underutilized RDS Instances**:
           * Use the AWS CLI command `describe-db-instances` to get information about all your RDS instances.
           * Look for instances with low CPU utilization, low memory utilization, or high idle time.

        2. **Modify RDS Instance**:
           * Use the AWS CLI command `modify-db-instance` to resize the instance to a more appropriate size based on your workload requirements.
           * Increase the instance size to utilize more CPU and memory resources if the current instance is underutilized.

        3. **Enable Auto Scaling**:
           * Configure Auto Scaling for your RDS instances to automatically adjust the instance size based on the workload.
           * Use the AWS CLI command `modify-db-instance` with the `--enable-auto-scaling` parameter to enable Auto Scaling for the RDS instance.

        4. **Monitor and Optimize**:
           * Regularly monitor the performance metrics of your RDS instances using CloudWatch.
           * Optimize the database configuration, queries, and indexes to improve performance and resource utilization.

        5. **Implement Cost Optimization**:
           * Consider Reserved Instances or Savings Plans to reduce costs for your RDS instances.
           * Use the AWS CLI command `purchase-reserved-db-instances-offering` to purchase Reserved Instances for your RDS instances.

        By following these steps, you can remediate the issue of underutilized RDS instances in AWS and ensure efficient resource utilization and cost optimization.
      </Accordion>

      <Accordion title="Using Python">
        To remediate underutilized RDS instances in AWS using Python, you can automate the process by creating a Lambda function that checks the CPU utilization of the RDS instances and modifies the instance class if the utilization is consistently low. Here's a step-by-step guide on how to achieve this:

        1. **Setting Up AWS Lambda Function:**
           * Create a new Lambda function in the AWS Management Console.
           * Choose Python as the runtime for your function.

        2. **IAM Role for Lambda Function:**
           * Create a new IAM role with the necessary permissions to describe RDS instances and modify them.
           * Attach the IAM role to the Lambda function.

        3. **Code for Lambda Function:**

           * Write Python code in the Lambda function to describe RDS instances and check their CPU utilization.
           * If the CPU utilization is consistently low, modify the instance class using the modify\_db\_instance API.

           ```python theme={null}
           import boto3

           def lambda_handler(event, context):
               rds = boto3.client('rds')
               db_instances = rds.describe_db_instances()

               for instance in db_instances['DBInstances']:
                   if instance['DBInstanceClass'] == 'db.t2.micro' and instance['DBInstanceStatus'] == 'available':
                       # Check CPU utilization using CloudWatch metrics
                       # Modify the instance class if CPU utilization is consistently low
                       rds.modify_db_instance(DBInstanceIdentifier=instance['DBInstanceIdentifier'], DBInstanceClass='db.t2.small')
           ```

        4. **CloudWatch Alarms:**
           * Set up CloudWatch alarms to monitor the CPU utilization of RDS instances.
           * Trigger the Lambda function when the utilization is consistently low.

        5. **Testing and Monitoring:**
           * Test the Lambda function to ensure it correctly identifies and modifies underutilized RDS instances.
           * Monitor the performance of RDS instances after modification to verify the effectiveness of the remediation.

        By following these steps, you can automate the remediation of underutilized RDS instances in AWS using Python and Lambda functions.
      </Accordion>

      <Accordion title="Using Terraform">
        ```hcl theme={null}
        resource "aws_db_instance" "THIS_DB" {
          identifier = "DB_INSTANCE_IDENTIFIER" # replace with the existing DB instance identifier

          # Existing required attributes for your instance (engine, storage, etc.) go here…

          # Downsized instance class to right-size the underutilized DB
          instance_class = "NEW_DB_INSTANCE_CLASS" # e.g., "db.t3.small"

          # Match the CLI fix: apply the resize immediately (causes a brief outage)
          apply_immediately = true
        }
        ```

        Replace `DB_INSTANCE_IDENTIFIER` with the current RDS instance identifier and `NEW_DB_INSTANCE_CLASS` with the smaller class you’ve selected after reviewing CloudWatch metrics (CPUUtilization, FreeableMemory, etc.). Changing `instance_class` is an in-place modification (no resource replacement) but will cause a service interruption when `apply_immediately = true`; set it to `false` if you prefer the next maintenance window instead.

        Verification: `terraform plan` should show `instance_class` changing from the current value to `NEW_DB_INSTANCE_CLASS` (and `apply_immediately` set as configured) with an in-place update on `aws_db_instance.THIS_DB`.
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>
