> ## 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 maintenance scheduled within 30 days remediation

### Triage and Remediation

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

    <AccordionGroup>
      <Accordion title="Using Console" defaultOpen="true">
        To remediate the SQL Instances should have maintenance scheduled within the next 30 days misconfiguration in GCP using GCP console, follow the below steps:

        1. Login to your GCP console.

        2. Navigate to the Cloud SQL Instances page.

        3. Identify the SQL instance that needs maintenance scheduling and click on its name to open its details page.

        4. Click on the "Edit" button at the top of the page.

        5. Scroll down to the "Maintenance" section.

        6. Click on the "Edit" button next to the "Maintenance window" option.

        7. In the "Maintenance window" dialog box, select the preferred day and time for maintenance scheduling.

        8. Click on the "Save" button to save the changes.

        9. Verify that the maintenance scheduling has been successfully updated by checking the "Maintenance" section on the SQL instance details page.

        10. Repeat the above steps for all the SQL instances that need maintenance scheduling.

        By following the above steps, you can remediate the SQL Instances should have maintenance scheduled within the next 30 days misconfiguration in GCP using GCP console.

        #
      </Accordion>

      <Accordion title="Using CLI">
        To remediate the misconfiguration "SQL Instances Should Have Maintenance Scheduled Within the Next 30 Day" for GCP using GCP CLI, you can follow the below steps:

        1. Open the GCP Cloud Shell from the GCP Console.

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

           `gcloud sql instances list`

        3. Select the SQL instance for which you want to schedule maintenance.

        4. Run the following command to schedule maintenance for the selected SQL instance:

           `gcloud sql instances patch [INSTANCE_NAME] --maintenance-window-day [DAY_OF_WEEK] --maintenance-window-hour [HOUR_OF_DAY]`

           Replace \[INSTANCE\_NAME] with the name of your SQL instance, \[DAY\_OF\_WEEK] with the day of the week on which you want to schedule maintenance, and \[HOUR\_OF\_DAY] with the hour of the day on which you want to schedule maintenance.

           For example, to schedule maintenance for an instance named "my-sql-instance" every Sunday at 2:00 AM UTC, run the following command:

           `gcloud sql instances patch my-sql-instance --maintenance-window-day SUN --maintenance-window-hour 02:00`

        5. Verify that the maintenance is scheduled by running the following command:

           `gcloud sql instances describe [INSTANCE_NAME]`

           Replace \[INSTANCE\_NAME] with the name of your SQL instance. The output will show the maintenance window details.

        By following these steps, you can remediate the misconfiguration "SQL Instances Should Have Maintenance Scheduled Within the Next 30 Day" for GCP using GCP CLI.
      </Accordion>

      <Accordion title="Using Python">
        To remediate the SQL Instances Should Have Maintenance Scheduled Within the Next 30 Day misconfiguration in GCP using Python, you can follow the below steps:

        1. Import the required libraries and authenticate to GCP using the service account credentials.

        ```
        from google.oauth2 import service_account
        from google.cloud import sql_v1beta4
        from datetime import date, timedelta

        # Authenticate using service account credentials
        credentials = service_account.Credentials.from_service_account_file('<path_to_service_account_key_file>')
        client = sql_v1beta4.SqlBackupRunsServiceClient(credentials=credentials)
        ```

        2. Get the list of all the SQL instances in your GCP project.

        ```
        project_id = '<your_project_id>'
        instances = client.list_instances(project_id)
        ```

        3. For each SQL instance, check if the maintenance window is not already set and if the instance is not in the process of being deleted.

        ```
        for instance in instances:
            if instance.settings.maintenance_window.start_time is None and instance.lifecycle_state != sql_v1beta4.SqlInstance.LifecycleStateValueValuesEnum.DELETING:
                # Set the maintenance window start time to 30 days from now
                start_time = (date.today() + timedelta(days=30)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')
                maintenance_window = sql_v1beta4.MaintenanceWindow(start_time=start_time)
                operation = client.patch_instance(project_id, instance.name, {'settings': {'maintenance_window': maintenance_window}})
                operation.result()
        ```

        4. The above code will set the maintenance window start time for all the SQL instances that do not have it already set and are not in the process of being deleted.

        Note: Make sure to replace `<path_to_service_account_key_file>` and `<your_project_id>` with the actual values for your GCP project.
      </Accordion>

      <Accordion title="Using Terraform">
        ```hcl theme={null}
        resource "google_sql_database_instance" "SQL_INSTANCE_NAME" {
          name             = "SQL_INSTANCE_NAME"     # replace with your instance name
          project          = "PROJECT_ID"            # replace with your GCP project ID
          region           = "REGION"                # e.g. "us-central1"
          database_version = "POSTGRES_15"           # or your engine/version
          settings {
            tier = "db-custom-1-3840"                # replace with your machine tier

            maintenance_window {
              # Choose a weekday (1=Monday ... 7=Sunday) and hour (0–23, UTC)
              # so that the NEXT occurrence of this weekly window is within the next 30 days.
              day          = 1                       # e.g. 1 for Monday
              hour         = 3                       # e.g. 03:00 UTC
              update_track = "stable"               # or "preview" if you intentionally use preview
            }
          }
        }
        ```

        Terraform cannot dynamically calculate “within the next 30 days”; you must pick a `day` and `hour` such that the next run of this weekly window falls inside the 30‑day threshold your check enforces.

        This change does not force instance replacement; only the maintenance policy is updated.

        For verification, `terraform plan` should show an in-place update adding or modifying the `maintenance_window` block on the existing `google_sql_database_instance.SQL_INSTANCE_NAME`.
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>
