使用 Terraform 创建触发器

本文档介绍如何在 Terraform 中使用 Google Cloud 资源,针对以下目标使用 google_eventarc_trigger 资源创建 Eventarc 触发器:

本教程中的示例使用来自 Cloud Storage 的直接事件,但可以适用于任何事件提供方。在本教程中,我们将创建新资源作为事件来源。

如需了解有关使用 Terraform 的资源和指导信息,请参阅 Google Cloud 上的 Terraform 文档。

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. 确保您的 Google Cloud 项目已启用结算功能

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. 确保您的 Google Cloud 项目已启用结算功能

  6. Enable the Resource Manager and Identity and Access Management (IAM) APIs.

    Enable the APIs

  7. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  8. Cloud Shell 是一个已集成 Terraform 的 Shell 环境。

创建 Eventarc 触发器

您可以使用 Terraform 为不同的目标创建 Eventarc 触发器。

此示例使用替代插值类型,例如引用变量、资源属性和调用函数。

Cloud Run

通过 Cloud Shell,使用 Terraform 部署资源以创建 Eventarc 触发器。

1. 启用 API

使用以下代码启用所需的 API:

# Enable Cloud Run API
resource "google_project_service" "run" {
  service            = "run.googleapis.com"
  disable_on_destroy = false
}

# Enable Eventarc API
resource "google_project_service" "eventarc" {
  service            = "eventarc.googleapis.com"
  disable_on_destroy = false
}

# Enable Pub/Sub API
resource "google_project_service" "pubsub" {
  service            = "pubsub.googleapis.com"
  disable_on_destroy = false
}

2. 创建服务账号并配置 IAM

使用以下代码创建专用服务账号和 IAM 角色:

# Used to retrieve project information later
data "google_project" "project" {}

# Create a dedicated service account
resource "google_service_account" "eventarc" {
  account_id   = "eventarc-trigger-sa"
  display_name = "Eventarc Trigger Service Account"
}

# Grant permission to receive Eventarc events
resource "google_project_iam_member" "eventreceiver" {
  project = data.google_project.project.id
  role    = "roles/eventarc.eventReceiver"
  member  = "serviceAccount:${google_service_account.eventarc.email}"
}

# Grant permission to invoke Cloud Run services
resource "google_project_iam_member" "runinvoker" {
  project = data.google_project.project.id
  role    = "roles/run.invoker"
  member  = "serviceAccount:${google_service_account.eventarc.email}"
}

如果您在 2021 年 4 月 8 日或之前启用了 Pub/Sub 服务代理,请向该服务代理授予 iam.serviceAccountTokenCreator 角色:

resource "google_project_iam_member" "tokencreator" {
   project  = data.google_project.project.id
   role     = "roles/iam.serviceAccountTokenCreator"
   member   = "serviceAccount:service-${data.google_project.project.number}@gcp-sa-pubsub.iam.gserviceaccount.com"
}

3. 创建 Cloud Storage 存储桶作为事件提供方

使用以下代码创建具有 Eventarc 相关权限的 Cloud Storage 存储桶:

# Cloud Storage bucket names must be globally unique
resource "random_id" "bucket_name_suffix" {
  byte_length = 4
}

# Create a Cloud Storage bucket
resource "google_storage_bucket" "default" {
  name          = "trigger-cloudrun-${data.google_project.project.name}-${random_id.bucket_name_suffix.hex}"
  location      = google_cloud_run_v2_service.default.location
  force_destroy = true

  uniform_bucket_level_access = true
}

# Grant the Cloud Storage service account permission to publish pub/sub topics
data "google_storage_project_service_account" "gcs_account" {}
resource "google_project_iam_member" "pubsubpublisher" {
  project = data.google_project.project.id
  role    = "roles/pubsub.publisher"
  member  = "serviceAccount:${data.google_storage_project_service_account.gcs_account.email_address}"
}

4. 将 Cloud Run 服务定义为事件目标

创建 Cloud Run 服务作为触发器的事件目标。使用 google_cloud_run_v2_service 资源定义 Cloud Run 服务:

# Deploy Cloud Run service
resource "google_cloud_run_v2_service" "default" {
  name     = "hello-events"
  location = "us-central1"

  template {
    containers {
      # This container will log received events
      image = "us-docker.pkg.dev/cloudrun/container/hello"
    }
    service_account = google_service_account.eventarc.email
  }

  depends_on = [google_project_service.run]
}

5. 定义 Eventarc 触发器

Eventarc 触发器会将事件提供方连接到事件目标。使用 google_eventarc_trigger 资源定义 Cloud Storage 直接事件提供方,以将事件发送到 Cloud Run 目标。

您可以使用 Eventarc 支持的 CloudEvents 属性,定义多个 matching_criteria,这些属性类似于您在创建触发器时指定的 event-filters。如需了解详情,请按照说明为特定提供商、事件类型和 Cloud Run 目标创建触发器。符合所有过滤条件的事件会被发送到目标。

# Create an Eventarc trigger, routing Cloud Storage events to Cloud Run
resource "google_eventarc_trigger" "default" {
  name     = "trigger-storage-cloudrun-tf"
  location = google_cloud_run_v2_service.default.location

  # Capture objects changed in the bucket
  matching_criteria {
    attribute = "type"
    value     = "google.cloud.storage.object.v1.finalized"
  }
  matching_criteria {
    attribute = "bucket"
    value     = google_storage_bucket.default.name
  }

  # Send events to Cloud Run
  destination {
    cloud_run_service {
      service = google_cloud_run_v2_service.default.name
      region  = google_cloud_run_v2_service.default.location
    }
  }

  service_account = google_service_account.eventarc.email
  depends_on = [
    google_project_service.eventarc,
    google_project_iam_member.pubsubpublisher
  ]
}

6. 应用更改

如需了解如何应用或移除 Terraform 配置,请参阅基本 Terraform 命令

如需在 Google Cloud 项目中应用 Terraform 配置,请完成以下部分中的步骤。

准备 Cloud Shell

  1. 启动 Cloud Shell
  2. 设置要在其中应用 Terraform 配置的默认 Google Cloud 项目。

    您只需为每个项目运行一次以下命令,即可在任何目录中运行它。

    export GOOGLE_CLOUD_PROJECT=PROJECT_ID

    如果您在 Terraform 配置文件中设置显式值,则环境变量会被替换。

准备目录

每个 Terraform 配置文件都必须有自己的目录(也称为“根模块”)。

  1. Cloud Shell 中,创建一个目录,并在该目录中创建一个新文件。文件名必须具有 .tf 扩展名,例如 main.tf。在本教程中,该文件称为 main.tf
    mkdir DIRECTORY && cd DIRECTORY && touch main.tf
  2. 如果您按照教程进行操作,可以在每个部分或步骤中复制示例代码。

    将示例代码复制到新创建的 main.tf 中。

    (可选)从 GitHub 中复制代码。如果端到端解决方案包含 Terraform 代码段,则建议这样做。

  3. 查看和修改要应用到您的环境的示例参数。
  4. 保存更改。
  5. 初始化 Terraform。您只需为每个目录执行一次此操作。
    terraform init

    (可选)如需使用最新的 Google 提供程序版本,请添加 -upgrade 选项:

    terraform init -upgrade

应用更改

  1. 查看配置并验证 Terraform 将创建或更新的资源是否符合您的预期:
    terraform plan

    根据需要更正配置。

  2. 通过运行以下命令并在提示符处输入 yes 来应用 Terraform 配置:
    terraform apply

    等待 Terraform 显示“应用完成!”消息。

  3. 打开您的 Google Cloud 项目以查看结果。在 Google Cloud 控制台的界面中找到资源,以确保 Terraform 已创建或更新它们。

7. 验证资源的创建

如需确认服务已创建,请运行以下命令:

gcloud run services list --region us-central1

如需确认触发器已创建,请运行以下命令:

gcloud eventarc triggers list --location us-central1

输出应类似如下所示:

NAME: trigger-storage-cloudrun-tf
TYPE: google.cloud.storage.object.v1.finalized
DESTINATION: Cloud Run service: hello-events
ACTIVE: Yes
LOCATION: us-central1

GKE

通过 Cloud Shell,使用 Terraform 部署资源以创建 Eventarc 触发器。

Eventarc 触发器需要 Google Kubernetes Engine 服务。为简化本教程,您将在应用 Terraform 配置期间,在 Terraform 之外配置此服务。

1. 创建 GKE 集群

使用以下代码启用所需的 API:

# Enable GKE API
resource "google_project_service" "container" {
  service            = "container.googleapis.com"
  disable_on_destroy = false
}

# Enable Eventarc API
resource "google_project_service" "eventarc" {
  service            = "eventarc.googleapis.com"
  disable_on_destroy = false
}

# Enable Pub/Sub API
resource "google_project_service" "pubsub" {
  service            = "pubsub.googleapis.com"
  disable_on_destroy = false
}

使用以下代码创建 GKE 集群:

# Create an auto-pilot GKE cluster
resource "google_container_cluster" "gke_cluster" {
  name     = "eventarc-cluster"
  location = "us-central1"

  enable_autopilot = true

  depends_on = [
    google_project_service.container
  ]
}

2. 应用更改

如需了解如何应用或移除 Terraform 配置,请参阅基本 Terraform 命令

如需在 Google Cloud 项目中应用 Terraform 配置,请完成以下部分中的步骤。

准备 Cloud Shell

  1. 启动 Cloud Shell
  2. 设置要在其中应用 Terraform 配置的默认 Google Cloud 项目。

    您只需为每个项目运行一次以下命令,即可在任何目录中运行它。

    export GOOGLE_CLOUD_PROJECT=PROJECT_ID

    如果您在 Terraform 配置文件中设置显式值,则环境变量会被替换。

准备目录

每个 Terraform 配置文件都必须有自己的目录(也称为“根模块”)。

  1. Cloud Shell 中,创建一个目录,并在该目录中创建一个新文件。文件名必须具有 .tf 扩展名,例如 main.tf。在本教程中,该文件称为 main.tf
    mkdir DIRECTORY && cd DIRECTORY && touch main.tf
  2. 如果您按照教程进行操作,可以在每个部分或步骤中复制示例代码。

    将示例代码复制到新创建的 main.tf 中。

    (可选)从 GitHub 中复制代码。如果端到端解决方案包含 Terraform 代码段,则建议这样做。

  3. 查看和修改要应用到您的环境的示例参数。
  4. 保存更改。
  5. 初始化 Terraform。您只需为每个目录执行一次此操作。
    terraform init

    (可选)如需使用最新的 Google 提供程序版本,请添加 -upgrade 选项:

    terraform init -upgrade

应用更改

  1. 查看配置并验证 Terraform 将创建或更新的资源是否符合您的预期:
    terraform plan

    根据需要更正配置。

  2. 通过运行以下命令并在提示符处输入 yes 来应用 Terraform 配置:
    terraform apply

    等待 Terraform 显示“应用完成!”消息。

  3. 打开您的 Google Cloud 项目以查看结果。在 Google Cloud 控制台的界面中找到资源,以确保 Terraform 已创建或更新它们。

3. 配置 GKE

在 GKE 上部署 Kubernetes 服务,该服务将使用预构建的 Cloud Run 映像 us-docker.pkg.dev/cloudrun/container/hello 接收 HTTP 请求和日志事件:

  1. 获取身份验证凭据以便与集群进行交互:

    gcloud container clusters get-credentials eventarc-cluster \
       --region=us-central1
    
  2. 创建名为 hello-gke 的部署:

    kubectl create deployment hello-gke \
       --image=us-docker.pkg.dev/cloudrun/container/hello
    
  3. 将部署公开为 Kubernetes service:x

    kubectl expose deployment hello-gke \
       --type ClusterIP --port 80 --target-port 8080
    
  4. 确保 Pod 正在运行:

    kubectl get pods
    

    输出应类似如下所示:

    NAME                        READY   STATUS
    hello-gke-df6469d4b-5vv22   1/1     Running
    

    如果 STATUSPending,则表示 Pod 正在部署。等待一分钟,等待部署完成,然后再次检查状态。

  5. 确保服务正在运行:

    kubectl get svc
    

    输出应类似如下所示:

    NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
    hello-gke    ClusterIP   34.118.226.144   <none>        80/TCP
    kubernetes   ClusterIP   34.118.224.1     <none>        443/TCP
    

4. 创建和配置 Eventarc

使用以下配置设置服务账号,并为其授予特定角色,以便Eventarc 管理 GKE 的事件。

# Create a service account to be used by GKE trigger
resource "google_service_account" "eventarc_gke_trigger_sa" {
  account_id   = "eventarc-gke-trigger-sa"
  display_name = "Evenarc GKE Trigger Service Account"
}

# Grant permission to receive Eventarc events
resource "google_project_iam_member" "eventreceiver" {
  project = data.google_project.project.id
  role    = "roles/eventarc.eventReceiver"
  member  = "serviceAccount:${google_service_account.eventarc_gke_trigger_sa.email}"
}

# Grant permission to subscribe to Pub/Sub topics
resource "google_project_iam_member" "pubsubscriber" {
  project = data.google_project.project.id
  role    = "roles/pubsub.subscriber"
  member  = "serviceAccount:${google_service_account.eventarc_gke_trigger_sa.email}"
}

使用以下代码创建具有 Eventarc 相关权限的 Cloud Storage 存储桶:

# Cloud Storage bucket names must be globally unique
resource "random_id" "bucket_name_suffix" {
  byte_length = 4
}

# Create a Cloud Storage bucket
resource "google_storage_bucket" "default" {
  name          = "trigger-gke-${data.google_project.project.name}-${random_id.bucket_name_suffix.hex}"
  location      = "us-central1"
  force_destroy = true

  uniform_bucket_level_access = true
}

# Grant the Cloud Storage service account permission to publish pub/sub topics
data "google_storage_project_service_account" "gcs_account" {}
resource "google_project_iam_member" "pubsubpublisher" {
  project = data.google_project.project.id
  role    = "roles/pubsub.publisher"
  member  = "serviceAccount:${data.google_storage_project_service_account.gcs_account.email_address}"
}

使用以下配置启用所需的 API 并初始化 Eventarc GKE 目标服务:

# Used to retrieve project_number later
data "google_project" "project" {}

# Enable Eventarc to manage GKE clusters
# This is usually done with: gcloud eventarc gke-destinations init
#
# Eventarc creates a separate Event Forwarder pod for each trigger targeting a
# GKE service, and  requires explicit permissions to make changes to the
# cluster. This is done by granting permissions to a special service account
# (the Eventarc P4SA) to manage resources in the cluster. This needs to be done
# once per Google Cloud project.

# This identity is created with: gcloud beta services identity create --service eventarc.googleapis.com
# This local variable is used for convenience
locals {
  eventarc_sa = "serviceAccount:service-${data.google_project.project.number}@gcp-sa-eventarc.iam.gserviceaccount.com"
}

resource "google_project_iam_member" "computeViewer" {
  project = data.google_project.project.id
  role    = "roles/compute.viewer"
  member  = local.eventarc_sa
}

resource "google_project_iam_member" "containerDeveloper" {
  project = data.google_project.project.id
  role    = "roles/container.developer"
  member  = local.eventarc_sa
}

resource "google_project_iam_member" "serviceAccountAdmin" {
  project = data.google_project.project.id
  role    = "roles/iam.serviceAccountAdmin"
  member  = local.eventarc_sa
}

创建 Eventarc 触发器,以将 Pub/Sub 事件路由到 hello-gke GKE 服务。

您可以使用 Eventarc 支持的 CloudEvents 属性,定义多个 matching_criteria,这些属性类似于您在创建触发器时指定的 event-filters。如需了解详情,请按照说明为特定提供商、事件类型和 GKE 目标创建触发器。符合所有过滤条件的事件会被发送到目标。

# Create an Eventarc trigger, routing Storage events to GKE
resource "google_eventarc_trigger" "default" {
  name     = "trigger-storage-gke-tf"
  location = "us-central1"

  # Capture objects changed in the bucket
  matching_criteria {
    attribute = "type"
    value     = "google.cloud.storage.object.v1.finalized"
  }
  matching_criteria {
    attribute = "bucket"
    value     = google_storage_bucket.default.name
  }

  # Send events to GKE service
  destination {
    gke {
      cluster   = "eventarc-cluster"
      location  = "us-central1"
      namespace = "default"
      path      = "/"
      service   = "hello-gke"
    }
  }

  service_account = google_service_account.eventarc_gke_trigger_sa.email
}

5. 应用其他更改

如需在 Google Cloud 项目中应用其他 Terraform 配置,请完成以下步骤:

  1. 创建 Eventarc 身份账号:

    gcloud beta services identity create --service eventarc.googleapis.com
    
  2. 将上一步中的新 Terraform 代码添加到现有 main.tf 文件中。

  3. 应用更新后的 Terraform 配置:

    terraform plan
    terraform apply
    

    等待 Terraform 显示“应用完成!”消息。

  4. 打开您的 Google Cloud 项目以查看结果。在 Google Cloud 控制台中,在界面中找到资源,以确保 Terraform 已创建或更新它们。

Workflows

通过 Cloud Shell,使用 Terraform 部署资源,以创建工作流和 Eventarc 触发器。

1. 启用 API

使用以下代码启用所需的 API:

# Enable Eventarc API
resource "google_project_service" "eventarc" {
  service            = "eventarc.googleapis.com"
  disable_on_destroy = false
}

# Enable Workflows API
resource "google_project_service" "workflows" {
  service            = "workflows.googleapis.com"
  disable_on_destroy = false
}

# Enable Pub/Sub API
resource "google_project_service" "pubsub" {
  service            = "pubsub.googleapis.com"
  disable_on_destroy = false
}

2. 创建服务账号并配置 IAM

使用以下代码创建专用服务账号并添加 IAM 角色:

# Used to retrieve project information later
data "google_project" "project" {}

# Create a service account for Eventarc trigger and Workflows
resource "google_service_account" "eventarc" {
  account_id   = "eventarc-workflows-sa"
  display_name = "Eventarc Workflows Service Account"
}

# Grant permission to invoke workflows
resource "google_project_iam_member" "workflowsinvoker" {
  project = data.google_project.project.id
  role    = "roles/workflows.invoker"
  member  = "serviceAccount:${google_service_account.eventarc.email}"
}

# Grant permission to receive events
resource "google_project_iam_member" "eventreceiver" {
  project = data.google_project.project.id
  role    = "roles/eventarc.eventReceiver"
  member  = "serviceAccount:${google_service_account.eventarc.email}"
}

如果您在 2021 年 4 月 8 日或之前启用了 Pub/Sub 服务代理,请向该服务代理授予 iam.serviceAccountTokenCreator 角色:

resource "google_project_iam_member" "tokencreator" {
   project  = data.google_project.project.id
   role     = "roles/iam.serviceAccountTokenCreator"
   member   = "serviceAccount:service-${data.google_project.project.number}@gcp-sa-pubsub.iam.gserviceaccount.com"
}

3. 创建 Cloud Storage 存储桶作为事件提供方

使用以下代码创建具有 Eventarc 相关权限的 Cloud Storage 存储桶:

# Cloud Storage bucket names must be globally unique
resource "random_id" "bucket_name_suffix" {
  byte_length = 4
}

# Create a Cloud Storage bucket
resource "google_storage_bucket" "default" {
  name          = "trigger-workflows-${data.google_project.project.name}-${random_id.bucket_name_suffix.hex}"
  location      = google_workflows_workflow.default.region
  force_destroy = true

  uniform_bucket_level_access = true
}

# Grant the Cloud Storage service account permission to publish Pub/Sub topics
data "google_storage_project_service_account" "gcs_account" {}
resource "google_project_iam_member" "pubsubpublisher" {
  project = data.google_project.project.id
  role    = "roles/pubsub.publisher"
  member  = "serviceAccount:${data.google_storage_project_service_account.gcs_account.email_address}"
}

4. 创建和部署工作流

定义和部署在所创建的存储桶中更新对象时执行的工作流:

# Create a workflow
resource "google_workflows_workflow" "default" {
  name        = "storage-workflow-tf"
  region      = "us-central1"
  description = "Workflow that returns information about storage events"

  # Note that $$ is needed for Terraform
  source_contents = <<EOF
  main:
    params: [event]
    steps:
      - log_event:
          call: sys.log
          args:
            text: $${event}
            severity: INFO
      - gather_data:
          assign:
            - bucket: $${event.data.bucket}
            - name: $${event.data.name}
            - message: $${"Received event " + event.type + " - " + bucket + ", " + name}
      - return_data:
          return: $${message}
  EOF

  depends_on = [
    google_project_service.workflows
  ]
}

5. 创建 Eventarc 触发器

创建 Eventarc 触发器,该触发器将创建的存储桶上的直接事件路由到 Workflows。使用 google_eventarc_trigger 资源定义 Eventarc 触发器资源。

您可以使用 Eventarc 支持的 CloudEvents 属性,定义多个 matching_criteria,这些属性类似于您在创建触发器时指定的 event-filters。如需了解详情,请按照说明为特定提供商、事件类型和 Workflows 目标创建触发器

符合所有过滤条件的事件会被发送到目标。

# Create an Eventarc trigger, routing Cloud Storage events to Workflows
resource "google_eventarc_trigger" "default" {
  name     = "trigger-storage-workflows-tf"
  location = google_workflows_workflow.default.region

  # Capture objects changed in the bucket
  matching_criteria {
    attribute = "type"
    value     = "google.cloud.storage.object.v1.finalized"
  }
  matching_criteria {
    attribute = "bucket"
    value     = google_storage_bucket.default.name
  }

  # Send events to Workflows
  destination {
    workflow = google_workflows_workflow.default.id
  }

  service_account = google_service_account.eventarc.email

  depends_on = [
    google_project_service.eventarc,
    google_project_service.workflows,
  ]
}

6. 应用更改

如需了解如何应用或移除 Terraform 配置,请参阅基本 Terraform 命令

如需在 Google Cloud 项目中应用 Terraform 配置,请完成以下部分中的步骤。

准备 Cloud Shell

  1. 启动 Cloud Shell
  2. 设置要在其中应用 Terraform 配置的默认 Google Cloud 项目。

    您只需为每个项目运行一次以下命令,即可在任何目录中运行它。

    export GOOGLE_CLOUD_PROJECT=PROJECT_ID

    如果您在 Terraform 配置文件中设置显式值,则环境变量会被替换。

准备目录

每个 Terraform 配置文件都必须有自己的目录(也称为“根模块”)。

  1. Cloud Shell 中,创建一个目录,并在该目录中创建一个新文件。文件名必须具有 .tf 扩展名,例如 main.tf。在本教程中,该文件称为 main.tf
    mkdir DIRECTORY && cd DIRECTORY && touch main.tf
  2. 如果您按照教程进行操作,可以在每个部分或步骤中复制示例代码。

    将示例代码复制到新创建的 main.tf 中。

    (可选)从 GitHub 中复制代码。如果端到端解决方案包含 Terraform 代码段,则建议这样做。

  3. 查看和修改要应用到您的环境的示例参数。
  4. 保存更改。
  5. 初始化 Terraform。您只需为每个目录执行一次此操作。
    terraform init

    (可选)如需使用最新的 Google 提供程序版本,请添加 -upgrade 选项:

    terraform init -upgrade

应用更改

  1. 查看配置并验证 Terraform 将创建或更新的资源是否符合您的预期:
    terraform plan

    根据需要更正配置。

  2. 通过运行以下命令并在提示符处输入 yes 来应用 Terraform 配置:
    terraform apply

    等待 Terraform 显示“应用完成!”消息。

  3. 打开您的 Google Cloud 项目以查看结果。在 Google Cloud 控制台的界面中找到资源,以确保 Terraform 已创建或更新它们。

7. 验证工作流创建

如需验证工作流是否已创建,请运行以下命令:

gcloud workflows list --location us-central1

8. 验证 Eventarc 触发器创建

如需验证 Eventarc 触发器是否已创建,请运行以下命令:

gcloud eventarc triggers list --location us-central1

输出应类似如下所示:

NAME: trigger-storage-workflows-tf
TYPE: google.cloud.storage.object.v1.finalized
DESTINATION: Workflows: storage-workflow-tf
ACTIVE: Yes
LOCATION: us-central1

生成并查看事件

您可以生成事件并确认 Eventarc 触发器按预期运行。

Cloud Run

  1. 要生成事件,请执行以下操作:

    将文本文件上传到 Cloud Storage:

    echo "Hello World" > random.txt
    gsutil cp random.txt gs://trigger-cloudrun-PROJECT_ID/random.txt
    

    上传操作会生成事件,而 Cloud Run 服务会记录事件的消息。

  2. 如需验证是否收到了事件,请运行以下命令:

    1. 如需查看服务创建的与事件相关的日志条目,请运行以下命令:
    gcloud logging read "resource.type=cloud_run_revision \
        AND resource.labels.service_name=hello-events"
    

    或者,打开 Google Cloud 控制台,找到 Cloud Run 资源,然后查看日志。

    1. 查找如下日志条目:
    Received event of type google.cloud.storage.object.v1.finalized.
    Event data: { "kind": "storage#object", "id": "trigger-cloudrun-PROJECT_ID/random.txt", ...}
    

GKE

  1. 要生成事件,请执行以下操作:

    将文本文件上传到 Cloud Storage:

    echo "Hello World" > random.txt
    gsutil cp random.txt gs://trigger-gke-PROJECT_ID/random.txt
    

    上传操作会生成事件,而 Cloud Run 服务会记录事件的消息。

  2. 如需验证是否收到了事件,请运行以下命令:

    1. 找到 Pod ID:

      POD_NAME=$(kubectl get pods -o custom-columns=":metadata.name" --no-headers)
      

      此命令使用 kubectl 设置格式的输出

    2. 查看 Pod 的日志:

      kubectl logs $POD_NAME
      
    3. 查找如下日志条目:

      {"severity":"INFO","eventType":"google.cloud.pubsub.topic.v1.messagePublished",
      "message":"Received event of type google.cloud.pubsub.topic.v1.messagePublished.", [...]}
      

Workflows

  1. 要生成事件,请执行以下操作:

    将文本文件上传到 Cloud Storage:

    echo "Hello World" > random.txt
    gsutil cp random.txt gs://trigger-workflows-PROJECT_ID/random.txt
    

    上传操作会生成事件,而 Cloud Run 服务会记录事件的消息。

  2. 如需验证是否收到了事件,请运行以下命令:

    1. 通过列出最近五次执行,验证工作流执行是否已触发:

      gcloud workflows executions list storage-workflow-tf --limit=5
      

      输出应包含带 NAMESTART_TIMEEND_TIMESTATUS 的执行列表。

    2. 获取最近一次执行的结果:

      EXECUTION_NAME=$(gcloud workflows executions list storage-workflow-tf --limit=1 --format "value(name)")
      gcloud workflows executions describe $EXECUTION_NAME
      
    3. 确认输出类似于以下内容:

      ...
      result: '"Received event google.cloud.storage.object.v1.finalized - trigger-workflows-PROJECT_ID, random.txt"'
      state: SUCCEEDED
      ...
      

      在工作流输出中查找 state: SUCCEEDEDresult: "Received event"

清理

通过运行以下命令并在提示符处输入 yes,移除之前使用 Terraform 配置应用的资源:

terraform destroy

您还可以删除 Google Cloud 项目,以避免产生费用。删除 Google Cloud 项目后,系统会停止对该项目中使用的所有资源计费。

  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.

后续步骤