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

# PubSub Lite Topic Partition Count Shoul Be At Least 1

### More Info:

Ensure topic partition count is set to atleast 1

### Risk Level

Low

### Address

Operational Maturity, Reliability

### Compliance Standards

CBP

### 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 PubSub Lite Topic partition count misconfiguration for GCP using the GCP console:

        1. Open the GCP Console and navigate to the Pub/Sub Lite Topics page.
        2. Select the Pub/Sub Lite Topic for which you want to remediate the partition count misconfiguration.
        3. Click on the "Edit" button at the top of the page.
        4. In the "Edit topic" dialog box, scroll down to the "Partition count" section.
        5. Set the partition count to at least 1.
        6. Click on the "Save" button to save the changes.

        That's it! The Pub/Sub Lite Topic partition count misconfiguration has now been remediated for GCP using the GCP console.

        #
      </Accordion>

      <Accordion title="Using CLI">
        To remediate the misconfiguration "PubSub Lite Topic Partition Count Should Be At Least 1" for GCP using GCP CLI, follow these steps:

        1. Open the Cloud Shell on GCP console.

        2. Run the following command to list all the Pub/Sub Lite topics in your project:

        ```
        gcloud pubsub lite-topics list --location=LOCATION --project=PROJECT_ID
        ```

        Replace `LOCATION` with the location of your Pub/Sub Lite topic and `PROJECT_ID` with your GCP project ID.

        3. Choose the Pub/Sub Lite topic that you want to remediate and run the following command to update the partition count to at least 1:

        ```
        gcloud pubsub lite-topics update TOPIC_ID --location=LOCATION --partition-count=1 --project=PROJECT_ID
        ```

        Replace `TOPIC_ID` with the ID of the Pub/Sub Lite topic that you want to remediate, `LOCATION` with the location of your Pub/Sub Lite topic, and `PROJECT_ID` with your GCP project ID.

        4. Verify that the partition count has been updated by running the following command:

        ```
        gcloud pubsub lite-topics describe TOPIC_ID --location=LOCATION --project=PROJECT_ID
        ```

        Replace `TOPIC_ID` with the ID of the Pub/Sub Lite topic that you want to remediate, `LOCATION` with the location of your Pub/Sub Lite topic, and `PROJECT_ID` with your GCP project ID.

        This should remediate the misconfiguration "PubSub Lite Topic Partition Count Should Be At Least 1" for GCP using GCP CLI.
      </Accordion>

      <Accordion title="Using Python">
        To remediate the PubSub Lite Topic Partition Count should be at least 1 for GCP, you can use the following Python code:

        1. First, you need to import the necessary libraries:

        ```python theme={null}
        from google.cloud import pubsub_v1
        from google.api_core import exceptions
        ```

        2. Then, you need to create a `PublisherClient` object:

        ```python theme={null}
        publisher_client = pubsub_v1.PublisherClient()
        ```

        3. Next, you need to get the topic path:

        ```python theme={null}
        project_id = "your-project-id"
        topic_name = "your-topic-name"
        topic_path = publisher_client.topic_path(project_id, topic_name)
        ```

        4. After that, you need to get the current topic configuration:

        ```python theme={null}
        try:
            topic = publisher_client.get_topic(request={"topic": topic_path})
        except exceptions.NotFound:
            print(f"Topic {topic_path} not found.")
            return
        ```

        5. Then, you need to check if the partition count is less than 1:

        ```python theme={null}
        if topic.partition_config.count < 1:
            topic.partition_config.count = 1
        ```

        6. Finally, you need to update the topic with the new configuration:

        ```python theme={null}
        update_mask = {"paths": ["partition_config.count"]}
        try:
            publisher_client.update_topic(topic=topic, update_mask=update_mask)
            print(f"Topic {topic_path} partition count updated to {topic.partition_config.count}.")
        except exceptions.GoogleAPICallError:
            print(f"Failed to update topic {topic_path}.")
        ```

        The complete Python code to remediate the PubSub Lite Topic Partition Count should be at least 1 for GCP would look like this:

        ```python theme={null}
        from google.cloud import pubsub_v1
        from google.api_core import exceptions

        publisher_client = pubsub_v1.PublisherClient()

        project_id = "your-project-id"
        topic_name = "your-topic-name"
        topic_path = publisher_client.topic_path(project_id, topic_name)

        try:
            topic = publisher_client.get_topic(request={"topic": topic_path})
        except exceptions.NotFound:
            print(f"Topic {topic_path} not found.")
            exit()

        if topic.partition_config.count < 1:
            topic.partition_config.count = 1

        update_mask = {"paths": ["partition_config.count"]}
        try:
            publisher_client.update_topic(topic=topic, update_mask=update_mask)
            print(f"Topic {topic_path} partition count updated to {topic.partition_config.count}.")
        except exceptions.GoogleAPICallError:
            print(f"Failed to update topic {topic_path}.")
        ```
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>
