> ## 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 free storage space remediation

### Triage and Remediation

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

    <AccordionGroup>
      <Accordion title="Using Console" defaultOpen="true">
        Sure, here are the step-by-step instructions to remediate the SQL instance storage auto-resize misconfiguration in GCP:

        1. Open the Google Cloud Console and navigate to the SQL instances page.
        2. Select the SQL instance that needs to be remediated.
        3. Click on the "Edit" button at the top of the page.
        4. Scroll down to the "Storage" section and click on the "Edit" button next to "Storage autoresize".
        5. Toggle the switch to the right to enable storage autoresize.
        6. Set the maximum storage size limit, if required.
        7. Click on the "Save" button at the bottom of the page to save the changes.

        Once the above steps are completed, the SQL instance will have storage autoresize enabled and will automatically increase the storage capacity when required.

        #
      </Accordion>

      <Accordion title="Using CLI">
        To remediate the misconfiguration "SQL Instances should have Storage Auto Resize Enabled" for GCP using GCP CLI, follow these steps:

        1. Open the Cloud Shell on the Google Cloud Platform Console.

        2. Run the following command to list all the SQL instances in your project:

           ```
           gcloud sql instances list
           ```

        3. Select the instance that you want to remediate and run the following command to enable storage auto resize for that instance:

           ```
           gcloud sql instances patch INSTANCE_NAME --enable-storage-autoresize
           ```

           Replace `INSTANCE_NAME` with the name of the instance that you want to remediate.

        4. Verify that the storage auto resize is enabled for the instance by running the following command:

           ```
           gcloud sql instances describe INSTANCE_NAME | grep storageAutoResize
           ```

           Replace `INSTANCE_NAME` with the name of the instance that you remediated.

           If the output shows `storageAutoResize: true`, then the remediation was successful.

        That's it! You have successfully remediated the misconfiguration "SQL Instances should have Storage Auto Resize Enabled" for GCP using GCP CLI.
      </Accordion>

      <Accordion title="Using Python">
        To remediate the misconfiguration "SQL Instances should have Storage Auto Resize Enabled" for GCP using Python, follow the steps below:

        1. Install the `google-cloud-sql` library using pip:

        ```
        pip install google-cloud-sql
        ```

        2. Import the necessary modules:

        ```
        from google.cloud import sql_v1beta4
        from google.oauth2 import service_account
        ```

        3. Set up the authentication by creating a service account and downloading the JSON key file. Then, create a `credentials` object using the JSON key file:

        ```
        credentials = service_account.Credentials.from_service_account_file('/path/to/keyfile.json')
        ```

        4. Create a `sql_v1beta4.CloudSqlClient` object using the `credentials` object:

        ```
        client = sql_v1beta4.CloudSqlClient(credentials=credentials)
        ```

        5. Get the list of SQL instances using the `list()` method of the `client.instances()` object:

        ```
        instances = client.instances().list(project='my-project-id', location='us-central1-a').execute()
        ```

        Replace `my-project-id` with your GCP project ID and `us-central1-a` with the location of your SQL instances.
        6\. Loop through the list of instances and check if the `settings.storageAutoResize` property is set to `True`. If not, enable it using the `patch()` method of the `client.instances()` object:

        ```
        for instance in instances['items']:
            if not instance['settings']['storageAutoResize']:
                instance['settings']['storageAutoResize'] = True
                operation = client.instances().patch(project='my-project-id', instance=instance['name'], body=instance).execute()
                print(f"Enabled storage auto resize for instance {instance['name']}. Operation ID: {operation['name']}")
        ```

        Replace `my-project-id` with your GCP project ID.

        This code will enable storage auto resize for all SQL instances in the specified project and location that do not already have it enabled.
      </Accordion>

      <Accordion title="Using Terraform">
        ```hcl theme={null}
        resource "google_sql_database_instance" "PRIMARY_INSTANCE" {
          name             = "PRIMARY_INSTANCE_NAME"        # replace with your instance name
          database_version = "POSTGRES_14"                  # replace with your engine/version
          region           = "PRIMARY_INSTANCE_REGION"      # replace with your region

          settings {
            tier      = "db-custom-2-7680"                  # replace with your tier
            disk_size = 50                                  # replace with your current size (GB)
            disk_type = "PD_SSD"                            # or PD_HDD

            # Enable storage auto resize to adapt to increased workloads
            disk_autoresize = true

            # Optional: if you already use a limit, keep or set it here to honor a threshold
            # disk_autoresize_limit = 500                   # max size in GB, if you have a policy for it
          }
        }
        ```

        Changing `disk_autoresize` from `false` to `true` is an in-place update and does not force replacement of the instance.

        Verification: `terraform plan` should show an in-place change on `google_sql_database_instance.PRIMARY_INSTANCE` with `settings.0.disk_autoresize` changing from `false` (or unset) to `true` (and any `disk_autoresize_limit` you specify).
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>
