在 Google Cloud 專案之間轉送事件


本教學課程說明如何使用 Eventarc,從一個 Google Cloud 專案中的來源讀取事件,並將事件轉送至另一個 Google Cloud 專案中的目標目的地。您可以使用 Pub/Sub 做為跨專案傳輸層,達到這個目的。

目標

在這個教學課程中,您將執行下列操作:

  1. 在一個專案中建立主題,然後從另一個專案發布至該主題。這會使用 Eventarc 觸發條件,將事件轉送至未經驗證的 Cloud Run 服務。

  2. 使用 Cloud Storage 適用的 Pub/Sub 通知,將 Cloud Storage 事件從一個專案發布至另一個專案。使用 Eventarc 觸發條件,將事件轉送至未經驗證的 Cloud Run 服務。

  3. 使用 Cloud Logging 接收器,將一個專案的 Cloud 稽核記錄發布至另一個專案。使用 Eventarc 觸發條件,將事件轉送至未經驗證的 Cloud Run 服務。

費用

在本文件中,您會使用 Google Cloud的下列計費元件:

如要根據預測用量估算費用,請使用 Pricing Calculator

初次使用 Google Cloud 的使用者可能符合免費試用資格。

事前準備

貴機構定義的安全性限制,可能會導致您無法完成下列步驟。如需疑難排解資訊,請參閱「在受限的 Google Cloud 環境中開發應用程式」。

請注意,您需要兩個專案才能完成本教學課程。下列步驟適用於這兩個專案。

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Install the Google Cloud CLI.

  5. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  6. To initialize the gcloud CLI, run the following command:

    gcloud init
  7. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  8. Make sure that billing is enabled for your Google Cloud project.

  9. Install the Google Cloud CLI.

  10. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  11. To initialize the gcloud CLI, run the following command:

    gcloud init
  12. 更新 gcloud 元件:
    gcloud components update
  13. 使用帳戶登入:
    gcloud auth login

跨專案傳送 Pub/Sub 事件

由於 Pub/Sub 是全球分散式服務,您可以在一個專案中建立主題,從另一個專案發布至該主題,然後觸發 Eventarc,將訊息傳送至 Cloud Run 服務:

跨專案事件:Cloud Pub/Sub 和 Eventarc

  1. 將 Google Cloud 專案 ID 設為第二個專案:

    gcloud config set project PROJECT_TWO_ID

    PROJECT_TWO_ID 替換為第二個專案的 ID。Google Cloud

  2. 在第二個專案中,執行下列步驟:

    1. 啟用 Cloud Run 和 Eventarc API:

      gcloud services enable run.googleapis.com eventarc.googleapis.com
    2. 設定預設位置:

      REGION=REGION

      REGION 替換為您選擇的支援 Eventarc 位置。例如:us-central1

    3. 建立 Pub/Sub 主題:

      TOPIC=my-topic
      gcloud pubsub topics create $TOPIC
    4. 使用預先建構的映像檔部署未經驗證的 Cloud Run 服務:us-docker.pkg.dev/cloudrun/container/hello

      gcloud run deploy hello \
          --image=us-docker.pkg.dev/cloudrun/container/hello \
          --allow-unauthenticated \
          --region=$REGION

      看到服務網址時,表示部署作業已完成。

    5. 使用 Eventarc 觸發程序將主題連結至服務:

      gcloud eventarc triggers create cross-project-trigger \
          --destination-run-service=hello \
          --destination-run-region=${REGION} \
          --location=${REGION} \
          --event-filters="type=google.cloud.pubsub.topic.v1.messagePublished" \
          --transport-topic=projects/PROJECT_TWO_ID/topics/$TOPIC

      這項操作會建立名為 cross-project-trigger 的觸發條件。

  3. 將 Google Cloud 專案 ID 設為第一個專案:

    gcloud config set project PROJECT_ONE_ID

    PROJECT_ONE_ID 替換為第一個專案的 ID。Google Cloud

  4. 在第一個專案中,將訊息發布到第二個專案的主題:

    gcloud pubsub topics publish projects/PROJECT_TWO_ID/topics/$TOPIC --message="hello"
  5. 將 Google Cloud 專案 ID 設為第二個專案:

    gcloud config set project PROJECT_TWO_ID
  6. 在第二個專案中,確認系統是否已記錄產生的事件:

    gcloud logging read "resource.labels.service_name=hello AND jsonPayload.message:hello" --format=json

    系統會傳回類似以下的記錄項目:

    "message": "Received event of type google.cloud.pubsub.topic.v1.messagePublished. Event data: hello"

在專案間轉送 Cloud Storage 事件

使用 Cloud Storage 適用的 Pub/Sub 通知,將事件從一個專案發布至另一個專案,然後透過 Eventarc 觸發條件將事件轉送至 Cloud Run 服務:

跨專案事件:Cloud Storage 和 Eventarc

  1. 將 Google Cloud 專案 ID 設為第一個專案:

    gcloud config set project PROJECT_ONE_ID
  2. 建立 Cloud Storage bucket:

    PROJECT1=$(gcloud config get-value project)
    BUCKET=$PROJECT1-cross-project
    gcloud storage buckets create gs://$BUCKET --location=${REGION}
  3. 為第二個專案中的主題建立 bucket 的 Pub/Sub 通知:

    gcloud storage buckets notifications create gs://$BUCKET --topic=projects/PROJECT_TWO_ID/topics/$TOPIC --payload-format=json
  4. 將檔案上傳至 bucket:

    echo "Hello World" > random.txt
    gcloud storage cp random.txt gs://$BUCKET/random.txt
  5. 將 Google Cloud 專案 ID 設為第二個專案:

    gcloud config set project PROJECT_TWO_ID
  6. 在第二個專案中,確認系統是否已記錄產生的事件:

    gcloud logging read "resource.labels.service_name=hello AND jsonPayload.message:random.txt" --format=json

    系統會傳回類似以下的記錄項目:

    Received event of type google.cloud.pubsub.topic.v1.messagePublished. Event data: {
      "kind": "storage#object",
      "id": "project1-cross-project/random.txt/1635327604259719",
      "selfLink": "https://www.googleapis.com/storage/v1/b/project1-cross-project/o/random.txt",
      "name": "random.txt",
      "bucket": "project1-cross-project",
      "generation": "1635327604259719",
    [...]
    }

在專案間傳送 Cloud 稽核記錄事件

如果建立的稽核記錄項目符合觸發條件的篩選條件,系統就會觸發對服務的要求。(詳情請參閱「決定 Cloud 稽核記錄的事件篩選器」一文)。在本例中,當您在第一個專案中建立 Compute Engine VM 執行個體時,符合觸發條件篩選條件的稽核記錄項目會擷取事件,並將事件傳送至第二個專案中的 Cloud Run 服務:

跨專案事件:Cloud 稽核記錄和 Eventarc

  1. 將 Google Cloud 專案 ID 設為第一個專案:

    gcloud config set project PROJECT_ONE_ID
  2. 在第一個專案中,為 Compute Engine 啟用「管理員讀取」、「資料讀取」和「資料寫入」記錄類型:

    請注意,在專案層級,您需要 Identity and Access Management (IAM) 角色,才能為資源設定資料存取稽核記錄。roles/owner Google Cloud

    1. 讀取專案的 IAM 政策,並儲存在檔案中:

      gcloud projects get-iam-policy PROJECT_ONE_ID > /tmp/policy.yaml
      
    2. 編輯 /tmp/policy.yaml,僅新增或變更資料存取稽核記錄設定。

      auditConfigs:
      - auditLogConfigs:
        - logType: ADMIN_READ
        - logType: DATA_READ
        - logType: DATA_WRITE
        service: compute.googleapis.com
      
    3. 撰寫新的 IAM 政策:

      gcloud projects set-iam-policy PROJECT_ONE_ID /tmp/policy.yaml
      

      如果前述指令回報與其他變更發生衝突,請重複這些步驟,從讀取專案的 IAM 政策開始。

  3. 在第一個專案中,建立 Cloud Logging 接收器,將 Cloud 稽核記錄傳送至第二個專案中的主題:

    gcloud logging sinks create cross-project-sink \
        pubsub.googleapis.com/projects/PROJECT_TWO_ID/topics/my-topic \
        --log-filter='protoPayload.methodName="beta.compute.instances.insert"'

    系統應會傳回類似以下的提醒:

    Please remember to grant `serviceAccount:p1011272509317-375795@gcp-sa-logging.iam.gserviceaccount.com` the Pub/Sub Publisher role on the topic.
  4. 將 Google Cloud 專案 ID 設為第二個專案:

    gcloud config set project PROJECT_TWO_ID
  5. 在第二個專案中,將角色授予服務帳戶:

    gcloud pubsub topics add-iam-policy-binding my-topic \
        --member=SERVICE_ACCOUNT \
        --role=roles/pubsub.publisher

    SERVICE_ACCOUNT 替換為上一個步驟傳回的服務帳戶電子郵件地址。

  6. 將 Google Cloud 專案 ID 設為第一個專案:

    gcloud config set project PROJECT_ONE_ID
  7. 在第一個專案中建立 Compute Engine VM 執行個體

    如果您使用 Google Cloud 控制台建立 VM 執行個體,可以接受預設值,以符合本教學課程的目的。

  8. 將 Google Cloud 專案 ID 設為第二個專案:

    gcloud config set project PROJECT_TWO_ID
  9. 在第二個專案中,確認系統是否已記錄產生的事件:

    gcloud logging read "resource.labels.service_name=hello AND jsonPayload.message:beta.compute.instances.insert" --format=json

    系統會傳回類似以下的記錄項目:

    Received event of type google.cloud.pubsub.topic.v1.messagePublished. Eventdata: {
      "logName": "projects/workflows-atamel/logs/cloudaudit.googleapis.com%2Factivity",
      "operation": {
        "id": "operation-1635330842489-5cf5321f4f454-ecc363cd-3883c08d",
        "last": true,
        "producer": "compute.googleapis.com"
      },
      "protoPayload": {
        "@type": "type.googleapis.com/google.cloud.audit.AuditLog",
        "methodName": "beta.compute.instances.insert",
      }
    [...]
    }

清除所用資源

如果您是為了這個教學課程建立新專案,請刪除專案。如果您使用現有專案,並想保留專案,但不要本教學課程新增的變更,請刪除為本教學課程建立的資源

刪除專案

如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。

如要刪除專案:

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

刪除教學課程資源

  1. 刪除您在本教學課程中部署的 Cloud Run 服務:

    gcloud run services delete SERVICE_NAME

    其中 SERVICE_NAME 是您選擇的服務名稱。

    您也可以從Google Cloud 控制台刪除 Cloud Run 服務。

  2. 移除您在教學課程設定期間新增的任何 gcloud CLI 預設設定。

    例如:

    gcloud config unset run/region

    gcloud config unset project

  3. 刪除在本教學課程中建立的其他 Google Cloud 資源:

    • 刪除 Eventarc 觸發條件:

      gcloud eventarc triggers delete TRIGGER_NAME
      
      TRIGGER_NAME 替換為觸發條件的名稱。

    • 刪除 Pub/Sub 主題:

      gcloud pubsub topics delete TOPIC TOPIC_ID
      
      TOPIC_ID 替換為主題 ID。

    • 刪除 Cloud Logging 接收器:

      gcloud logging sinks delete SINK_NAME
      
      SINK_NAME 替換為接收器的名稱。

後續步驟