יצירת טריגר באמצעות Terraform

במאמר הזה מוסבר איך להשתמש ב-Terraform ובמשאב google_eventarc_trigger כדי ליצור טריגרים של Eventarc ליעדים הבאים ב- Google Cloud:

מידע נוסף על השימוש ב-Terraform מופיע במאמרי העזרה של Terraform ב- Google Cloud.

דוגמאות הקוד במדריך הזה מנתבות אירועים ישירים מ-Cloud Storage, אבל אפשר להתאים אותן לכל ספק אירועים. לדוגמה, כדי ללמוד איך לנתב אירועים ישירים מ-Pub/Sub ל-Cloud Run, קראו את המדריך למתחילים של Terraform.

לפני שמתחילים

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

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  2. Verify that billing is enabled for your Google Cloud project.

  3. מפעילים את Cloud Resource Manager API ואת Identity and Access Management (IAM) API, אם הם עדיין לא מופעלים.

    התפקידים שנדרשים כדי להפעיל ממשקי API

    כדי להפעיל ממשקי API, צריך את ההרשאה serviceusage.services.enable. אם אתם יצרתם את הפרויקט, סביר להניח שכבר יש לכם את ההרשאה הזו דרך התפקיד "בעלים" (roles/owner). אחרת, אתם יכולים לקבל את ההרשאה הזו דרך התפקיד "אדמין בממשק 'שימוש בשירות'" (roles/serviceusage.serviceUsageAdmin). איך מקצים תפקידים

    הפעלת ממשקי ה-API

  4. מפעילים את Cloud Shell במסוף Google Cloud .

    הפעלת Cloud Shell

    בחלק התחתון של מסוף Google Cloud יתחיל סשן של Cloud Shell ותופיע הודעה של שורת הפקודה. Cloud Shell היא סביבת מעטפת שבה ה-CLI של Google Cloud מותקן ומוגדרים ערכים לפרויקט הקיים. הסשן יופעל תוך כמה שניות.

  5. מתקינים את Terraform ב-Cloud Shell. אם אתם משתמשים ב-Cloud Shell, אתם צריכים להתקין את ה-CLI בספריית הבית או בסביבה שלכם כדי להשתמש בפקודות של terraform.

הכנה לפריסת Terraform

לפני שפורסים משאבים של Terraform, צריך ליצור קובץ תצורה של Terraform. קובץ תצורה של Terraform מאפשר לכם להגדיר את מצב הסיום המועדף של התשתית באמצעות התחביר של Terraform.

הכנת Cloud Shell

ב-Cloud Shell, מגדירים את פרויקט ברירת המחדל ב- Google Cloud שבו רוצים להחיל את ההגדרות של Terraform. תצטרכו להריץ את הפקודה הזו רק פעם אחת לכל פרויקט, ותוכלו לעשות זאת בכל ספרייה:

export GOOGLE_CLOUD_PROJECT=PROJECT_ID

מחליפים את PROJECT_ID במזהה הפרויקט ב- Google Cloud .

חשוב לזכור שאם תגדירו ערכים ספציפיים בקובץ התצורה של Terraform, הם יבטלו את ערכי ברירת המחדל של משתני הסביבה.

הכנת הספרייה

לכל קובץ תצורה של Terraform צריכה להיות ספרייה משלו (שנקראת גם מודול ברמה הבסיסית). יוצרים ספרייה חדשה ב-Cloud Shell ובה יוצרים קובץ חדש:

mkdir DIRECTORY && cd DIRECTORY && touch main.tf

שם הקובץ חייב לכלול את הסיומת .tf, למשל, במסמך הזה הקובץ נקרא main.tf.

הגדרת התצורה של Terraform

מעתיקים את דוגמאות הקוד הרלוונטיות של Terraform לקובץ main.tf החדש שיצרתם. לחלופין, אפשר גם להעתיק את הקוד מ-GitHub. כדאי לעשות את זה כשקטע הקוד של Terraform הוא חלק מפתרון מקצה לקצה.

בדרך כלל, מחילים את כל ההגדרות בבת אחת. אבל אפשר גם לטרגט משאב ספציפי. לדוגמה:

terraform apply -target="google_eventarc_trigger.default"

שימו לב שבדוגמאות הקוד של Terraform נעשה שימוש באינטרפולציה להחלפות כמו משתני עזר, מאפיינים של משאבים ופונקציות קריאה.

הפעלת ממשקי ה-API

בדרך כלל הנחת העבודה בדוגמאות של Terraform היא שממשקי ה-API הנדרשים מופעלים בפרויקט ב-Google Cloud . כדי להפעיל את ממשקי ה-API, משתמשים בקוד הבא:

Cloud Run

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

GKE

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

Workflows

# Enable Workflows API
resource "google_project_service" "workflows" {
  service            = "workflows.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
}

יצירת חשבון שירות והגדרת הגישה שלו

כל טריגר של Eventarc משויך לחשבון שירות ב-IAM בזמן שיוצרים את הטריגר. משתמשים בקוד הבא כדי ליצור חשבון שירות ייעודי ולהקצות לחשבון השירות בניהול המשתמש תפקידים ספציפיים של ניהול זהויות והרשאות גישה (IAM) לניהול אירועים:

Cloud Run

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

# Create a dedicated service account
resource "google_service_account" "default" {
  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.default.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.default.email}"
}

סוכן השירות של Pub/Sub נוצר באופן אוטומטי כשמפעילים את Pub/Sub API. אם סוכן השירות של Pub/Sub נוצר ב-8 באפריל 2021 או לפני כן, ולחשבון השירות אין את התפקיד "סוכן שירות של Cloud Pub/Sub" (roles/pubsub.serviceAgent), צריך להקצות לסוכן השירות את התפקיד "יצירת אסימונים בחשבון שירות" (roles/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."
}

GKE

  1. לפני שיוצרים את חשבון השירות, צריך להפעיל את 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
    }
  2. יוצרים את חשבון השירות:

    # 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}"
    }
    

Workflows

# 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}"
}

# Grant permission to write logs
resource "google_project_iam_member" "logwriter" {
  project = data.google_project.project.id
  role    = "roles/logging.logWriter"
  member  = "serviceAccount:${google_service_account.eventarc.email}"
}

סוכן השירות של Pub/Sub נוצר באופן אוטומטי כשמפעילים את Pub/Sub API. אם סוכן השירות של Pub/Sub נוצר ב-8 באפריל 2021 או לפני כן, ולחשבון השירות אין את התפקיד "סוכן שירות של Cloud Pub/Sub" (roles/pubsub.serviceAgent), צריך להקצות לסוכן השירות את התפקיד "יצירת אסימונים בחשבון שירות" (roles/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."
}

יצירת קטגוריה של Cloud Storage כספק אירועים

משתמשים בקוד הבא כדי ליצור קטגוריה של Cloud Storage, ולהקצות את התפקיד Pub/Sub Publisher (roles/pubsub.publisher) לסוכן השירות של Cloud Storage.

Cloud Run

# 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}"

  depends_on = [data.google_storage_project_service_account.gcs_account]
}

GKE

# 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}"
}

Workflows

# 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}"
}

יצירת מקלט אירועים שיהיה יעד האירוע

יוצרים מקלט אירועים באמצעות אחד מהמשאבים הבאים של Terraform:

Cloud Run

יוצרים שירות Cloud Run כיעד לאירוע של הטריגר ב-Eventarc:

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

  deletion_protection = false # set to "true" in production

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

  depends_on = [google_project_service.run]
}

GKE

כדי לפשט את המדריך, צרו שירות Google Kubernetes Engine כיעד לאירועים מחוץ ל-Terraform, בין החלת ההגדרות האישיות של Terraform.

  1. אם לא יצרתם טריגר בפרויקט הזה ( Google Cloud ) בעבר, הריצו את הפקודה הבאה כדי ליצור את סוכן השירות של Eventarc:

    gcloud beta services identity create --service eventarc.googleapis.com
  2. יוצרים אשכול 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
      ]
    }
  3. פורסים שירות Kubernetes ב-GKE שיקבל בקשות HTTP ואירועי יומן באמצעות קובץ אימג' בנוי מראש של Cloud Run,‏ us-docker.pkg.dev/cloudrun/container/hello:

    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:

      kubectl expose deployment hello-gke \
         --type ClusterIP --port 80 --target-port 8080
      
    4. מוודאים שה-pod פועל:

      kubectl get pods
      

      הפלט אמור להיראות כך:

      NAME                         READY   STATUS    RESTARTS   AGE
      hello-gke-5b6574b4db-rzzcr   1/1     Running   0          2m45s
      

      אם הערך של STATUS הוא Pending או ContainerCreating, ה-pod נפרס. מחכים דקה עד שהפריסה תסתיים ובודקים שוב את הסטטוס.

    5. מוודאים שהשירות פועל:

      kubectl get svc
      

      הפלט אמור להיראות כך:

      NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
      hello-gke    ClusterIP   34.118.230.123   <none>        80/TCP    4m46s
      kubernetes   ClusterIP   34.118.224.1     <none>        443/TCP   14m
      

Workflows

פורסים תהליך עבודה שמופעל כשמעדכנים אובייקט בקטגוריה של Cloud Storage:

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

  deletion_protection = false # set to "true" in production

  # 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
  ]
}

הגדרת טריגר של Eventarc

טריגר של Eventarc מנתב אירועים מספק אירועים ליעד אירועים. משתמשים במשאב google_eventarc_trigger כדי לציין את מאפייני CloudEvents ב-matching_criteria ולסנן את האירועים. מידע נוסף זמין בהוראות ליצירת טריגר לספק, לסוג אירוע וליעד ספציפיים. אירועים שתואמים לכל המסננים נשלחים ליעד.

Cloud Run

יוצרים טריגר של Eventarc שמנתב אירועים של Cloud Storage לשירות hello-event ב-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
    }
  }

  # Specify a single delivery attempt with no retries
  retry_policy {
    max_attempts = 1
  }

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

אפשר גם להשתמש בבלוק retry_policy כדי לציין ניסיון שליחה אחד של הודעת אירוע ללא ניסיונות חוזרים. הערך החוקי היחיד הוא 1. ההגדרה הזו רלוונטית רק ליעדים ב-Cloud Run (כולל פונקציות Cloud Run). אם משמיטים את הבלוק, חלה התנהגות ברירת המחדל של ניסיון חוזר. מידע נוסף מופיע במאמר ניסיון חוזר של אירועים.

GKE

יוצרים טריגר של Eventarc שמנתב אירועים של Cloud Storage לשירות GKE hello-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
}

Workflows

יוצרים טריגר של Eventarc שמנתב אירועים של Cloud Storage לתהליך העבודה שנקרא storage-workflow-tf.

# 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,
  ]
}

החלת Terraform

משתמשים ב-CLI של Terraform כדי להקצות תשתית על סמך קובץ התצורה.

כדי ללמוד איך להחיל הגדרות ב-Terraform או להסיר אותן, ראו פקודות בסיסיות ב-Terraform.

  1. מפעילים את Terraform. צריך לעשות זאת רק פעם אחת לכל ספרייה.

    terraform init

    אופציונלי: תוכלו לכלול את האפשרות -upgrade, כדי להשתמש בגרסה העדכנית ביותר של הספק של Google:

    terraform init -upgrade
  2. בודקים את ההגדרות ומוודאים שהמשאבים שמערכת Terraform תיצור או תעדכן תואמים לציפיות שלכם:

    terraform plan

    מתקנים את ההגדרות לפי הצורך.

  3. מריצים את הפקודה הבאה ומזינים yes בהודעה שמופיעה, כדי להחיל את הגדרות Terraform:

    terraform apply

    ממתינים עד שב-Terraform תוצג ההודעה "Apply complete!‎".

אימות יצירת המשאבים

Cloud Run

  1. מוודאים שהשירות נוצר:

    gcloud run services list --region us-central1
    
  2. מוודאים שהטריגר נוצר:

    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

  1. מוודאים שהשירות נוצר:

    kubectl get service hello-gke
    
  2. מוודאים שהטריגר נוצר:

    gcloud eventarc triggers list --location us-central1
    

    הפלט אמור להיראות כך:

    NAME: trigger-storage-gke-tf
    TYPE: google.cloud.storage.object.v1.finalized
    DESTINATION: GKE: hello-gke
    ACTIVE: Yes
    LOCATION: us-central1
    

Workflows

  1. מוודאים שתהליך העבודה נוצר:

    gcloud workflows list --location us-central1
    
  2. מוודאים שהטריגר של 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 פועל כמו שצריך.

  1. מאחזרים את השם של הקטגוריה של Cloud Storage שיצרתם קודם:

    gcloud storage ls
    
  2. מעלים קובץ טקסט לקטגוריה של Cloud Storage:

    echo "Hello World" > random.txt
    gcloud storage cp random.txt gs://BUCKET_NAME/random.txt
    

    מחליפים את BUCKET_NAME בשם הקטגוריה ב-Cloud Storage שאחזרתם בשלב הקודם. לדוגמה:

    gcloud storage cp random.txt gs://BUCKET_NAME/random.txt

    ההעלאה יוצרת אירוע, והשירות של מקבל האירוע מתעד את הודעת האירוע.

  3. מוודאים שהאירוע מתקבל:

    Cloud Run

    1. מסננים את רשומות היומן שנוצרו על ידי השירות:

      gcloud logging read 'jsonPayload.message: "Received event of type google.cloud.storage.object.v1.finalized."'
      
    2. מחפשים רשומה ביומן שדומה לזו:

      Received event of type google.cloud.storage.object.v1.finalized.
      Event data: { "kind": "storage#object", "id": "trigger-cloudrun-BUCKET_NAME/random.txt", ...}
      

    GKE

    1. איך מוצאים את מזהה ה-Pod:

      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.storage.object.v1.finalized","message":
      "Received event of type google.cloud.storage.object.v1.finalized. Event data: ...}
      

    Workflows

    1. כדי לוודא שהופעל תהליך עבודה, מציגים את חמשת תהליכי העבודה האחרונים:

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

      הפלט צריך לכלול רשימה של הפעלות עם NAME,‏ STATE,‏ START_TIME ו-END_TIME.

    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 - BUCKET_NAME, random.txt"'
      startTime: '2024-12-13T17:23:50.451316533Z'
      state: SUCCEEDED
      ...
      

הסרת המשאבים

כדי להסיר משאבים שהוחלו בעבר על הגדרות Terraform, מריצים את הפקודה הבאה ומזינים yes בהודעה שמופיעה:

terraform destroy

אתם יכולים גם למחוק את הפרויקט ב- Google Cloud כדי להימנע מחיובים. כשמוחקים את הפרויקט ב- Google Cloud , החיוב על כל המשאבים שמשמשים את הפרויקט נפסק.

  1. במסוף Google Cloud , נכנסים לדף Manage resources.

    כניסה לדף Manage resources

  2. ברשימת הפרויקטים, בוחרים את הפרויקט שרוצים למחוק ולוחצים על Delete.
  3. כדי למחוק את הפרויקט, כותבים את מזהה הפרויקט בתיבת הדו-שיח ולוחצים על Shut down.

המאמרים הבאים