יצירת סביבות באמצעות Terraform

Managed Airflow (דור 3) | Managed Airflow (דור 2) | Managed Airflow (דור 1 מדור קודם)

הדף הזה הוא דף נלווה לדף הראשי בנושא יצירת סביבות. המדריך מדגים איך להגדיר סביבת Managed Airflow וחשבון שירות בניהול המשתמשים עבור הסביבה הזו בפרויקט קיים Google Cloud עם Terraform. אפשר להשתמש בדף הזה כנקודת התחלה, ואז להוסיף פרמטרים נוספים של הגדרות לסביבה שלכם, לפי הצורך.

מידע על מודולים של Terraform ל-Managed Airflow

‫Google מתחזקת את פלאגין שמתממשק עם שירותים חיצוניים Terraform ל- Google Cloud, שכולל את המשאב google_composer_environment. זהו המודול הרשמי שנתמך ב-Terraform.

יש מודולים אחרים של Terraform שמתוחזקים על ידי קהילת הקוד הפתוח. כדי לפתור בעיות במודולים כאלה, אפשר ליצור בקשות בקשר לבעיות במאגרים שלהם ב-GitHub. דוגמה אחת לפרויקט קוד פתוח שמתוחזק ב-GitHub היא המודול terraform-google-composer, שהוא חלק מתוכניות ומודולים של Terraform ל- Google Cloud. ‫Google תורמת לפרויקט הזה, אבל היא לא מתחזקת את המודול והוא לא קשור לספק Terraform ל- Google Cloud.

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

  • במדריך הזה אנחנו יוצאים מנקודת הנחה שיש לכם פרויקט עם חיוב מוגדר: Google Cloud

    • אפשר להשתמש בפרויקט קיים.
    • אפשר ליצור פרויקט חדש באמצעות מסוף Google Cloud ,‏ Google Cloud CLI,‏ API או ספריית לקוח של Python.
    • אתם יכולים ליצור ולנהל את הפרויקט באמצעות Terraform. מידע נוסף מופיע במסמכי העזרה של Terraform בנושא המשאב google_project.
  • מתקינים את ה-CLI של gcloud.

אימות באמצעות Google Cloud

כדי לבצע אימות באמצעות Google Cloud, מריצים את הפקודה:

gcloud auth application-default login

מידע נוסף על הפקודה הזו זמין במאמר gcloud auth application-default.

הגדרת ספק Google ב-Terraform

מציינים את מזהה הפרויקט הקיים ואזור ברירת מחדל למשאבים. האזור הזה משמש את סביבת Managed Airflow שלכם.

הספק google-beta תומך בתכונות של Managed Airflow שנמצאות בגרסת טרום-השקה (Preview). אם רוצים להשתמש רק בתכונות GA של Managed Airflow, צריך להשתמש בספק google במקום בספק google-beta.

provider "google-beta" {
  project = "example-project"
  region  = "us-central1"
}

הפעלת Cloud Composer API

מפעילים את Cloud Composer API בפרויקט:

resource "google_project_service" "composer_api" {
  provider = google-beta
  project = "example-project"
  service = "composer.googleapis.com"
  // Disabling Cloud Composer API might irreversibly break all other
  // environments in your project.
  // This parameter prevents automatic disabling
  // of the API when the resource is destroyed.
  // We recommend to disable the API only after all environments are deleted.
  disable_on_destroy = false
  // this flag is introduced in 5.39.0 version of Terraform. If set to true it will
  //prevent you from disabling composer_api through Terraform if any environment was
  //there in the last 30 days
  check_if_service_has_usage_on_destroy = true
}

יצירת חשבון שירות של סביבה בפרויקט

במדריך הזה מוסבר איך ליצור חשבון שירות בסביבה עם כל ההרשאות הנדרשות להרצת סביבת Managed Airflow.

מומלץ מאוד להגדיר חשבון שירות שמנוהל על ידי משתמש עבור סביבות Managed Airflow, עם הרשאות שנדרשות להפעלת הסביבה והפעולות ב-DAG, כמו שמתואר במדריך הזה.

יכול להיות שחשבון השירות של הסביבה שלכם יזדקק להרשאות נוספות כדי לגשת למשאבים אחרים בפרויקט. לדוגמה, אם ה-DAGs מעבירים נתונים ל-BigQuery, יכול להיות שהחשבון הזה יצטרך הרשאות או תפקידים ספציפיים ל-BigQuery.

מגדירים חשבון שירות בהתאמה אישית עם התפקידים וההרשאות הבאים:

resource "google_service_account" "custom_service_account" {
  provider = google-beta
  account_id   = "custom-service-account"
  display_name = "Example Custom Service Account"
}

resource "google_project_iam_member" "custom_service_account" {
  provider = google-beta
  project  = "example-project"
  member   = format("serviceAccount:%s", google_service_account.custom_service_account.email)
  // Role for Public IP environments
  role     = "roles/composer.worker"
}

יצירת סביבה

יוצרים את הסביבה באמצעות Terraform.

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

מידע נוסף על פרמטרים אחרים זמין במאמר יצירת סביבות.

resource "google_composer_environment" "example_environment" {
  provider = google-beta
  name = "example-environment"

  config {

    software_config {
      image_version = "composer-3-airflow-2.11.1-build.6"
    }

    node_config {
      service_account = google_service_account.custom_service_account.email
    }

  }
}

סקריפט מלא של Terraform

provider "google-beta" {
  project = "example-project"
  region  = "us-central1"
}

resource "google_project_service" "composer_api" {
  provider = google-beta
  project = "example-project"
  service = "composer.googleapis.com"
  // Disabling Cloud Composer API might irreversibly break all other
  // environments in your project.
  disable_on_destroy = false
  // this flag is introduced in 5.39.0 version of Terraform. If set to true it will
  //prevent you from disabling composer_api through Terraform if any environment was
  //there in the last 30 days
  check_if_service_has_usage_on_destroy = true
}

resource "google_service_account" "custom_service_account" {
  provider = google-beta
  account_id   = "custom-service-account"
  display_name = "Example Custom Service Account"
}

resource "google_project_iam_member" "custom_service_account" {
  provider = google-beta
  project  = "example-project"
  member   = format("serviceAccount:%s", google_service_account.custom_service_account.email)
  // Role for Public IP environments
  role     = "roles/composer.worker"
}

resource "google_composer_environment" "example_environment" {
  provider = google-beta
  name = "example-environment"

  config {

    software_config {
      image_version = "composer-3-airflow-2.11.1-build.6"
    }

    node_config {
      service_account = google_service_account.custom_service_account.email
    }

  }
}

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

במאמרים אחרים בנושא Terraform מוסבר איך להגדיר את הסביבה. לדוגמה: