> For the complete documentation index, see [llms.txt](https://docs.blusapphire.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.blusapphire.io/log-forwarding/03_log-forwarding-guide/cloud-log-forwarding/google/gcp-loadbalancer-pubsub.md).

# GCP LoadBalancer (pubsub)

## Phase 1: Configure Your GCP Project

## 1. Enable GCP Load Balancer Logging

1\. In the Google Cloud console, go to the Load Balancing page.

2\. Click the name of your load balancer.

3\. Click edit.

4\. Click Backend Configuration.

5\. Click edit next to your backend service.

6\. In the Logging section, select the Enable logging checkbox.

7\. To finish editing the backend service, click Update.

8\. To finish editing the load balancer, click Update.

## 2. Create a Pub/Sub Topic

This topic will act as the destination for your logs.

1. Navigate to **Pub/Sub > Topics**.
2. Click **Create Topic** and give it an ID (e.g., loadbalancer-logs-topic).

<figure><img src="https://2078222076-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MMRHZBPHlLDUc8519fX%2Fuploads%2FWXvsVr5UdImrccBPq9yQ%2Fimage.png?alt=media&amp;token=84cd0ac3-0fd9-46f0-bdfb-f1cfc6f7909f" alt=""><figcaption></figcaption></figure>

3. Click **Create**.

   1. **Create the Subscription**:

      1. Go to the **Subscriptions** tab and click **Create Subscription**.

      <figure><img src="https://2078222076-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MMRHZBPHlLDUc8519fX%2Fuploads%2FG41mMt7yV8GE9RZ7WLms%2Fimage.png?alt=media&amp;token=f51e2d53-7709-4bc1-9ab5-c00cc9eb399b" alt=""><figcaption></figcaption></figure>

      <figure><img src="https://2078222076-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MMRHZBPHlLDUc8519fX%2Fuploads%2FFbjb5foXFXVDNroMEsqb%2Fimage.png?alt=media&amp;token=c46ab377-680e-4dcb-beec-e225d4a26314" alt=""><figcaption></figcaption></figure>
   2. Provide a **Subscription ID**, like central-loadbalancer-sub.

   <figure><img src="https://2078222076-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MMRHZBPHlLDUc8519fX%2Fuploads%2FkePhSV9eLfeb1DHqnyvv%2Fimage.png?alt=media&amp;token=f63cafb1-be8a-455a-bb9c-56a344d7f63e" alt=""><figcaption></figcaption></figure>

   c. From the dropdown menu, select the central-loadbalancer-logs-topic you just created.

&#x20;     d. Leave all other settings as their defaults and click **Create**.

## 3. Create a Log Sink

The sink will filter your loadbalancer logs and send them to the Pub/Sub topic.

1. Navigate to **Logging > Log Router**.
2. Click **Create Sink**.

<figure><img src="https://2078222076-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MMRHZBPHlLDUc8519fX%2Fuploads%2F6TBi5xdQc66T3t9scsVH%2Fimage.png?alt=media&amp;token=30f9aa70-0ba8-4fab-b934-5d7fe272e03e" alt=""><figcaption></figcaption></figure>

3. **Name** the sink (e.g., loadbalancer-log-sink) and click **Next**.
4. For the **Sink destination**, select "Cloud Pub/Sub topic" as the sink service and choose the loadbalancer-logs-topic you just created. Click **Next**.

<figure><img src="https://2078222076-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MMRHZBPHlLDUc8519fX%2Fuploads%2FmcQCkk5YlIsRreiQexQ5%2Fimage.png?alt=media&amp;token=59a0f340-e574-4b25-b181-ed087f32707c" alt=""><figcaption></figcaption></figure>

5. In the **Build inclusion filter** box, paste the following filter to select only loadbalancer logs:

***logName:"logs/compute.googleapis.com%2Floadbalancer"***

6. Click **Create Sink**.

**4. Set Sink Permissions**

After creating the sink, you must give it permission to publish to your topic.

1. Go to Log Router, select the sink which we have created earlier. Click on the three dots at the right and click on view sink details and copy the writer identity

<figure><img src="https://2078222076-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MMRHZBPHlLDUc8519fX%2Fuploads%2FMIQ3mXq3Nr3HcbZlLkIf%2Fimage.png?alt=media&amp;token=bee15e22-0ede-449c-89bf-a1818ee80a13" alt=""><figcaption></figcaption></figure>

2. The Log Router page will now show a **Writer Identity** for your new sink (an email like <p123...@gcp-sa-logging.iam.gserviceaccount.com>). **Copy this email address.**
3. Go back to **Pub/Sub > Topics**, click the checkbox next to loadbalancer-logs-topic, and open the **PERMISSIONS** tab in the info panel on the right.
4. Click **ADD PRINCIPAL**.

<figure><img src="https://2078222076-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MMRHZBPHlLDUc8519fX%2Fuploads%2FAiAfQUlbj4Znnrn3Wc1O%2Fimage.png?alt=media&amp;token=347af138-2f4c-45a5-a8dc-8d6bb072a79e" alt=""><figcaption></figcaption></figure>

5. Paste the **Writer Identity email** as the new principal.
6. Assign it the role of **Pub/Sub Publisher**.

<figure><img src="https://2078222076-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MMRHZBPHlLDUc8519fX%2Fuploads%2FKy7mugujfIOCI9aYTeRm%2Fimage.png?alt=media&amp;token=96acbfd7-54e6-41d8-a470-ebc66a833aa7" alt=""><figcaption></figcaption></figure>

7. Click **Save**.

## Phase 2: Create a Service Account for Filebeat

Filebeat needs its own identity to read the logs from GCP.

1. Navigate to **IAM & Admin > Service Accounts**.

<figure><img src="https://2078222076-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MMRHZBPHlLDUc8519fX%2Fuploads%2F9q9jNArkfDui6XR6Etxv%2Fimage.png?alt=media&amp;token=00afbcd2-5d86-4440-b653-c92427b03d18" alt=""><figcaption></figcaption></figure>

2. Click **Create Service Account**, give it a name (e.g., filebeat-agent), and click **Create and Continue**.

<figure><img src="https://2078222076-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MMRHZBPHlLDUc8519fX%2Fuploads%2FkfJkpQOnui00EKqsIaFk%2Fimage.png?alt=media&amp;token=cdb46bcb-2c96-44eb-9ff0-b27397826da3" alt=""><figcaption></figcaption></figure>

3. In the "Grant access" step, assign the role of **Pub/Sub Subscriber**. Click **Done**.

<figure><img src="https://2078222076-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MMRHZBPHlLDUc8519fX%2Fuploads%2Fz1rGn6tGpDymJIWq6Vhn%2Fimage.png?alt=media&amp;token=823d6c61-7436-46b4-b224-c0e2a6360200" alt=""><figcaption></figcaption></figure>

4. Find the service account you just created, click on the three dots at the right and select **Manage keys** option.
5. Go to its **KEYS** tab, click **ADD KEY**, and select **Create new key**.

<figure><img src="https://2078222076-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MMRHZBPHlLDUc8519fX%2Fuploads%2FkHkRTtuERHwej7dgI9wS%2Fimage.png?alt=media&amp;token=a0ac96b6-f67e-47e1-98bb-3553f2c79899" alt=""><figcaption></figcaption></figure>

6. Choose **JSON** and click **CREATE**. A JSON key file will be downloaded to your computer.
7. Share the downloaded JSON file with Blusapphire along with project id ,topic , subscription name
