本教學課程說明如何使用 Eventarc,從一個 Google Cloud 專案中的來源讀取事件,並將事件轉送至另一個 Google Cloud 專案中的目標目的地。您可以使用 Pub/Sub 做為跨專案傳輸層,達到這個目的。
目標
在這個教學課程中,您將執行下列操作:
在一個專案中建立主題,然後從另一個專案發布至該主題。這會使用 Eventarc 觸發條件,將事件轉送至未經驗證的 Cloud Run 服務。
使用 Cloud Storage 適用的 Pub/Sub 通知,將 Cloud Storage 事件從一個專案發布至另一個專案。使用 Eventarc 觸發條件,將事件轉送至未經驗證的 Cloud Run 服務。
使用 Cloud Logging 接收器,將一個專案的 Cloud 稽核記錄發布至另一個專案。使用 Eventarc 觸發條件,將事件轉送至未經驗證的 Cloud Run 服務。
費用
在本文件中,您會使用 Google Cloud的下列計費元件:
如要根據預測用量估算費用,請使用 Pricing Calculator。
事前準備
貴機構定義的安全性限制,可能會導致您無法完成下列步驟。如需疑難排解資訊,請參閱「在受限的 Google Cloud 環境中開發應用程式」。
請注意,您需要兩個專案才能完成本教學課程。下列步驟適用於這兩個專案。
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
- 更新 gcloud 元件:
gcloud components update
- 使用帳戶登入:
gcloud auth login
跨專案傳送 Pub/Sub 事件
由於 Pub/Sub 是全球分散式服務,您可以在一個專案中建立主題,從另一個專案發布至該主題,然後觸發 Eventarc,將訊息傳送至 Cloud Run 服務:
將 Google Cloud 專案 ID 設為第二個專案:
gcloud config set project PROJECT_TWO_ID
將
PROJECT_TWO_ID
替換為第二個專案的 ID。Google Cloud在第二個專案中,執行下列步驟:
啟用 Cloud Run 和 Eventarc API:
gcloud services enable run.googleapis.com eventarc.googleapis.com
設定預設位置:
REGION=REGION
將
REGION
替換為您選擇的支援 Eventarc 位置。例如:us-central1
。建立 Pub/Sub 主題:
TOPIC=my-topic gcloud pubsub topics create $TOPIC
使用預先建構的映像檔部署未經驗證的 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
看到服務網址時,表示部署作業已完成。
使用 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
的觸發條件。
將 Google Cloud 專案 ID 設為第一個專案:
gcloud config set project PROJECT_ONE_ID
將
PROJECT_ONE_ID
替換為第一個專案的 ID。Google Cloud在第一個專案中,將訊息發布到第二個專案的主題:
gcloud pubsub topics publish projects/PROJECT_TWO_ID/topics/$TOPIC --message="hello"
將 Google Cloud 專案 ID 設為第二個專案:
gcloud config set project PROJECT_TWO_ID
在第二個專案中,確認系統是否已記錄產生的事件:
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 服務:
將 Google Cloud 專案 ID 設為第一個專案:
gcloud config set project PROJECT_ONE_ID
建立 Cloud Storage bucket:
PROJECT1=$(gcloud config get-value project) BUCKET=$PROJECT1-cross-project gcloud storage buckets create gs://$BUCKET --location=${REGION}
為第二個專案中的主題建立 bucket 的 Pub/Sub 通知:
gcloud storage buckets notifications create gs://$BUCKET --topic=projects/PROJECT_TWO_ID/topics/$TOPIC --payload-format=json
將檔案上傳至 bucket:
echo "Hello World" > random.txt gcloud storage cp random.txt gs://$BUCKET/random.txt
將 Google Cloud 專案 ID 設為第二個專案:
gcloud config set project PROJECT_TWO_ID
在第二個專案中,確認系統是否已記錄產生的事件:
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 服務:
將 Google Cloud 專案 ID 設為第一個專案:
gcloud config set project PROJECT_ONE_ID
在第一個專案中,為 Compute Engine 啟用「管理員讀取」、「資料讀取」和「資料寫入」記錄類型:
請注意,在專案層級,您需要 Identity and Access Management (IAM) 角色,才能為資源設定資料存取稽核記錄。
roles/owner
Google Cloud讀取專案的 IAM 政策,並儲存在檔案中:
gcloud projects get-iam-policy PROJECT_ONE_ID > /tmp/policy.yaml
編輯
/tmp/policy.yaml
,僅新增或變更資料存取稽核記錄設定。auditConfigs: - auditLogConfigs: - logType: ADMIN_READ - logType: DATA_READ - logType: DATA_WRITE service: compute.googleapis.com
撰寫新的 IAM 政策:
gcloud projects set-iam-policy PROJECT_ONE_ID /tmp/policy.yaml
如果前述指令回報與其他變更發生衝突,請重複這些步驟,從讀取專案的 IAM 政策開始。
在第一個專案中,建立 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.
將 Google Cloud 專案 ID 設為第二個專案:
gcloud config set project PROJECT_TWO_ID
在第二個專案中,將角色授予服務帳戶:
gcloud pubsub topics add-iam-policy-binding my-topic \ --member=SERVICE_ACCOUNT \ --role=roles/pubsub.publisher
將
SERVICE_ACCOUNT
替換為上一個步驟傳回的服務帳戶電子郵件地址。將 Google Cloud 專案 ID 設為第一個專案:
gcloud config set project PROJECT_ONE_ID
在第一個專案中建立 Compute Engine VM 執行個體。
如果您使用 Google Cloud 控制台建立 VM 執行個體,可以接受預設值,以符合本教學課程的目的。
將 Google Cloud 專案 ID 設為第二個專案:
gcloud config set project PROJECT_TWO_ID
在第二個專案中,確認系統是否已記錄產生的事件:
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", } [...] }
清除所用資源
如果您是為了這個教學課程建立新專案,請刪除專案。如果您使用現有專案,並想保留專案,但不要本教學課程新增的變更,請刪除為本教學課程建立的資源。
刪除專案
如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。
如要刪除專案:
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
刪除教學課程資源
刪除您在本教學課程中部署的 Cloud Run 服務:
gcloud run services delete SERVICE_NAME
其中
SERVICE_NAME
是您選擇的服務名稱。您也可以從Google Cloud 控制台刪除 Cloud Run 服務。
移除您在教學課程設定期間新增的任何 gcloud CLI 預設設定。
例如:
gcloud config unset run/region
或
gcloud config unset project
刪除在本教學課程中建立的其他 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
替換為接收器的名稱。