> ## 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.

# Sql instance counts remediation

### Triage and Remediation

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

    <AccordionGroup>
      <Accordion title="Using Console" defaultOpen="true">
        To remediate the misconfiguration "Provisioned Instances Should Not Exceed Set Threshold" for GCP using GCP console, follow the below steps:

        1. Login to the GCP console ([https://console.cloud.google.com/](https://console.cloud.google.com/)).
        2. Navigate to the "Compute Engine" service from the navigation menu on the left-hand side.
        3. Click on the "Instance Groups" option from the sub-menu.
        4. Select the instance group that has exceeded the set threshold.
        5. Click on the "Edit Group" button at the top of the page.
        6. In the "Autoscaling" section, adjust the maximum number of instances to the desired threshold.
        7. Click on the "Save" button to apply the changes.

        Once the above steps are completed, the instance group will be remediated and the number of provisioned instances will be within the set threshold.

        #
      </Accordion>

      <Accordion title="Using CLI">
        To remediate the issue of Provisioned Instances Should Not Exceed Set Threshold in GCP using GCP CLI, follow the below steps:

        Step 1: Open the Cloud Shell in your GCP console.

        Step 2: Run the following command in your Cloud Shell to get the list of all the instances running in your GCP project:

        ```
        gcloud compute instances list
        ```

        Step 3: Check the number of instances running and compare it with the set threshold. If the number of instances is exceeding the set threshold, then you need to delete some of the instances.

        Step 4: To delete an instance, run the following command:

        ```
        gcloud compute instances delete [INSTANCE_NAME]
        ```

        Replace \[INSTANCE\_NAME] with the actual name of the instance you want to delete.

        Step 5: Confirm the deletion by typing "Y" when prompted.

        Step 6: Repeat Step 4 and Step 5 for all the instances you want to delete.

        Step 7: Once you have deleted the required number of instances, re-run the command in Step 2 to verify that the number of instances is now within the set threshold.

        By following the above steps, you can remediate the issue of Provisioned Instances Should Not Exceed Set Threshold in GCP using GCP CLI.
      </Accordion>

      <Accordion title="Using Python">
        To remediate the "Provisioned Instances Should Not Exceed Set Threshold" misconfiguration in GCP using Python, you can use the following steps:

        1. Define the set threshold for the number of provisioned instances.
        2. Use the GCP Python SDK to retrieve a list of all the instances currently provisioned in the project.
        3. Count the number of instances in the list.
        4. If the number of instances exceeds the set threshold, use the GCP Python SDK to delete the excess instances.

        Here's some sample Python code that can be used to remediate this misconfiguration:

        ```python theme={null}
        # Import the required libraries
        from google.cloud import compute_v1

        # Define the set threshold for the number of provisioned instances
        threshold = 10

        # Create a Compute Engine client using the GCP Python SDK
        client = compute_v1.InstancesClient()

        # Retrieve a list of all the instances currently provisioned in the project
        project = "my-gcp-project"
        zone = "us-central1-a"
        instances = client.list(project=project, zone=zone).items

        # Count the number of instances in the list
        num_instances = len(instances)

        # If the number of instances exceeds the set threshold, delete the excess instances
        if num_instances > threshold:
            excess_instances = instances[threshold:]
            for instance in excess_instances:
                client.delete(project=project, zone=zone, instance=instance.name)
        ```

        Note that you will need to replace the `my-gcp-project` and `us-central1-a` placeholders in the code with your own GCP project and zone information. Additionally, you may need to authenticate with GCP using a service account key before running this code.
      </Accordion>

      <Accordion title="Using Terraform">
        ```hcl theme={null}
        # Example: manage your Cloud SQL instances via Terraform and keep their
        # total count <= 50 (the organizational threshold).

        variable "cloud_sql_instances" {
          description = "Map of Cloud SQL instances to create. Keep length(map) <= 50."
          type = map(object({
            database_version = string
            region           = string
            tier             = string
          }))
        }

        # ORGANIZATIONAL POLICY ENFORCEMENT (PROCESS):
        # - Ensure that length(var.cloud_sql_instances) is at most 50.
        # - To remediate the current finding, remove entries from this map until
        #   the total number of instances (managed here + any unmanaged ones)
        #   is <= 50, then run `terraform apply` to destroy the excess.

        resource "google_sql_database_instance" "this" {
          for_each = var.cloud_sql_instances

          name             = each.key
          database_version = each.value.database_version
          region           = each.value.region

          settings {
            tier = each.value.tier
          }
        }
        ```

        Terraform and the `google_sql_database_instance` resource do not expose any argument that can globally cap the number of SQL instances; the only way to remediate is to reduce the number of instances you declare (or destroy existing ones) so the total in the project/organization is at most 50.

        This change is destructive for any instances you remove from `var.cloud_sql_instances`: those instances will be destroyed, which is irreversible and will cause downtime unless you migrate workloads first.

        Verification: `terraform plan` should show a set of `google_sql_database_instance.this["INSTANCE_NAME"]` resources with `-/+` or `-` actions for the instances you removed until the total remaining instances is ≤ 50, and no plans to create instances beyond that count.
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>
