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

# Minimum Number of Compute Instances Should Be Configured For Load Balancers Global Instance Groups

### More Info:

Minimum number of instances should be configured for your Load Balancer global instance groups to improve the reliability.

### Risk Level

Low

### Address

Reliability, Security

### Compliance Standards

* Cloudanix Best Practice

### Triage and Remediation

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

    <AccordionGroup>
      <Accordion title="Using Console" defaultOpen="true">
        To remediate the misconfiguration "Minimum Number of Compute Instances Should Be Configured For Load Balancers Global Instance Groups" in GCP using GCP console, follow the below steps:

        1. Login to your GCP console and navigate to the Compute Engine section.

        2. Click on the "Instance groups" tab from the left-hand side menu.

        3. Select the instance group that is associated with the global load balancer.

        4. Click on the "Edit" button to edit the instance group configuration.

        5. Scroll down to the "Autoscaling" section and click on the "Advanced" button.

        6. In the "Minimum number of instances" field, enter the minimum number of compute instances that should be configured for the load balancer global instance group.

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

        8. Verify that the changes have been applied by checking the instance group details.

        By following these steps, you can remediate the misconfiguration "Minimum Number of Compute Instances Should Be Configured For Load Balancers Global Instance Groups" in GCP using GCP console.

        #
      </Accordion>

      <Accordion title="Using CLI">
        To remediate the misconfiguration "Minimum Number of Compute Instances Should Be Configured For Load Balancers Global Instance Groups" in GCP using GCP CLI, follow these steps:

        1. Open the Google Cloud SDK Shell or any other terminal where you have installed the GCP CLI.

        2. Authenticate with your GCP account using the following command:

        ```
        gcloud auth login
        ```

        3. Once you are authenticated, set the default project using the following command:

        ```
        gcloud config set project [PROJECT_ID]
        ```

        4. Now, list all the global instance groups in your project using the following command:

        ```
        gcloud compute instance-groups list
        ```

        5. Identify the global instance group that has the misconfiguration of not having the minimum number of compute instances configured.

        6. Set the minimum number of compute instances for the identified global instance group using the following command:

        ```
        gcloud compute instance-groups managed set-autoscaling [INSTANCE_GROUP_NAME] --min-num-replicas=[MINIMUM_INSTANCES]
        ```

        Replace \[INSTANCE\_GROUP\_NAME] with the name of the identified global instance group and \[MINIMUM\_INSTANCES] with the minimum number of compute instances you want to configure.

        7. Verify that the minimum number of compute instances has been configured successfully using the following command:

        ```
        gcloud compute instance-groups managed describe [INSTANCE_GROUP_NAME]
        ```

        This command will display the details of the identified global instance group, including the minimum number of compute instances configured.

        By following these steps, you can remediate the misconfiguration "Minimum Number of Compute Instances Should Be Configured For Load Balancers Global Instance Groups" in GCP using GCP CLI.
      </Accordion>

      <Accordion title="Using Python">
        To remediate the misconfiguration of minimum number of compute instances should be configured for load balancers global instance groups in GCP using python, you can follow the below steps:

        1. First, you need to get the instance group name for which you want to set the minimum number of instances. You can use the following command to get the instance group name:

        ```
        gcloud compute instance-groups list
        ```

        2. Once you have the instance group name, you can use the following python code to set the minimum number of instances to 1:

        ```python theme={null}
        from googleapiclient import discovery
        from oauth2client.client import GoogleCredentials

        # Set the project id and instance group name
        project_id = 'your-project-id'
        instance_group_name = 'your-instance-group-name'

        # Authenticate and create the compute engine API client
        credentials = GoogleCredentials.get_application_default()
        service = discovery.build('compute', 'v1', credentials=credentials)

        # Set the minimum number of instances to 1
        request_body = {
            'namedPorts': [{
                'name': 'http',
                'port': 80
            }],
            'targetSize': 1
        }
        response = service.instanceGroupManagers().setTargetSize(project=project_id, zone='us-central1-a', instanceGroupManager=instance_group_name, body=request_body).execute()

        # Print the response
        print(response)
        ```

        3. In the above code, replace the `project_id` and `instance_group_name` variables with your own values.

        4. After running the above code, the minimum number of instances for the specified instance group will be set to 1.

        Note: Make sure that you have the necessary permissions to make changes to the instance group.
      </Accordion>

      <Accordion title="Using Terraform">
        ```hcl theme={null}
        resource "google_compute_instance_group_manager" "LB_INSTANCE_GROUP" {
          name               = "LB-INSTANCE-GROUP-NAME"     # replace with your MIG name
          base_instance_name = "LB-INSTANCE-BASE-NAME"      # replace with your base instance name
          zone               = "ZONE"                       # e.g. "us-central1-a"
          target_size        = 2                            # optional: initial number of instances

          version {
            instance_template = google_compute_instance_template.LB_INSTANCE_TEMPLATE.self_link
          }

          # Add any other arguments you already use (named_port, auto_healing_policies, etc.)
        }

        resource "google_compute_autoscaler" "LB_INSTANCE_GROUP_AUTOSCALER" {
          name   = "LB-INSTANCE-GROUP-AUTOSCALER-NAME"      # replace with your autoscaler name
          zone   = google_compute_instance_group_manager.LB_INSTANCE_GROUP.zone
          target = google_compute_instance_group_manager.LB_INSTANCE_GROUP.self_link

          autoscaling_policy {
            min_replicas = 2                                # required minimum number of instances
            max_replicas = 10                               # replace with your desired max
            # add cpu_utilization / load_balancing_utilization / custom_metric if needed
          }
        }
        ```

        Substitute:

        * `LB-INSTANCE-GROUP-NAME` with the managed instance group name used by your load balancer backend.
        * `LB-INSTANCE-BASE-NAME` with the base name you want for instances.
        * `ZONE` with the zone of the MIG.
        * `LB_INSTANCE_TEMPLATE` with your existing `google_compute_instance_template` resource name.
        * `LB-INSTANCE-GROUP-AUTOSCALER-NAME` and `max_replicas` as appropriate.

        This change does not force replacement of the instance group; Terraform will update or create the autoscaler in place and adjust the group size as needed.

        To verify, `terraform plan` should show a `google_compute_autoscaler` being created or updated with `autoscaling_policy.min_replicas = 2` and no destructive `destroy`/`create` cycle for the instance group manager.
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>

### Additional Reading:

* [https://cloud.google.com/compute/docs/instance-groups/adding-an-instance-group-to-a-load-balancer](https://cloud.google.com/compute/docs/instance-groups/adding-an-instance-group-to-a-load-balancer)
