מדריך למתחילים: יצירת מכונה וירטואלית באמצעות Terraform

במדריך למתחילים הזה מוסבר איך להשתמש ב-Terraform כדי ליצור מכונה וירטואלית (VM) ב-Compute Engine ולהתחבר למכונה הווירטואלית הזו.

‫Hashicorp Terraform הוא כלי של תשתית כקוד (IaC), שבעזרתו אפשר להקצות ולנהל תשתית ענן. ‫Terraform provider for Google Cloud (Google Cloud provider) מאפשר הקצאה וניהול של תשתית Google Cloud .

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

  1. כדי להשתמש בטרמינל אונליין שכבר מוגדרים בו ה-CLI של gcloud ו-Terraform, צריך להפעיל את Cloud Shell:

    בחלק התחתון של הדף הזה מתחיל סשן של Cloud Shell ומופיעה הנחיה של שורת הפקודה. הסשן יופעל תוך כמה שניות.

  2. Create or select 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.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

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

  4. Enable the Compute Engine API:

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable compute.googleapis.com
  5. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/compute.instanceAdmin.v1

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE

    Replace the following:

הכנת הסביבה

  1. משכפלים את מאגר GitHub שמכיל דוגמאות ל-Terraform:

    git clone https://github.com/terraform-google-modules/terraform-docs-samples.git --single-branch
    
  2. עוברים לספרייה שמכילה את הדוגמה למדריך למתחילים:

    cd terraform-docs-samples/compute/quickstart/create_vm
    

בדיקת קובצי Terraform

בודקים את הקובץ main.tf. בקובץ הזה מוגדרים המשאבים שרוצים ליצור. Google Cloud

cat main.tf

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

resource "google_compute_instance" "default" {
  name         = "my-vm"
  machine_type = "n1-standard-1"
  zone         = "us-central1-a"

  boot_disk {
    initialize_params {
      image = "ubuntu-minimal-2210-kinetic-amd64-v20230126"
    }
  }

  network_interface {
    network = "default"
    access_config {}
  }
}

בקובץ הזה מתואר המשאב google_compute_instance, שהוא משאב Terraform של מכונה וירטואלית ב-Compute Engine. ההגדרות של google_compute_instance הן:

  • הערך של name הוא my-vm.
  • הערך של machine_type הוא n1-standard-1.
  • הערך של zone הוא us-central1-a.
  • boot_disk מגדיר את דיסק האתחול של המכונה.
  • network_interface מוגדר לשימוש ברשת שמוגדרת כברירת מחדל בGoogle Cloud פרויקט.

יצירת מכונה וירטואלית ב-Compute Engine

  1. ב-Cloud Shell, מריצים את הפקודה הבאה כדי לוודא ש-Terraform זמין:

    terraform
    

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

    
    Usage: terraform [global options] <subcommand> [args]
    
    The available commands for execution are listed below.
    The primary workflow commands are given first, followed by
    less common or more advanced commands.
    
    Main commands:
      init          Prepare your working directory for other commands
      validate      Check whether the configuration is valid
      plan          Show changes required by the current configuration
      apply         Create or update infrastructure
      destroy       Destroy previously-created infrastructure
    
    
  2. מפעילים את Terraform באמצעות הפקודה הבאה. הפקודה הזו מכינה את סביבת העבודה כדי ש-Terraform יוכל להחיל את ההגדרות.

    terraform init
    

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

    
    Initializing the backend...
    
    Initializing provider plugins...
    - Finding latest version of hashicorp/google...
    - Installing hashicorp/google v5.35.0...
    - Installed hashicorp/google v5.35.0 (signed by HashiCorp)
    
    Terraform has created a lock file .terraform.lock.hcl to record the provider
    selections it made above. Include this file in your version control repository
    so that Terraform can guarantee to make the same selections by default when
    you run "terraform init" in the future.
    
    Terraform has been successfully initialized!
    
    
  3. מריצים את הפקודה הבאה כדי לאמת את הגדרות Terraform. הפקודה הזו מבצעת את הפעולות הבאות:

    • מוודאים שהתחביר של main.tf נכון.
    • תיווצר תצוגה מקדימה של המשאבים.
    terraform plan
    

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

    Plan: 1 to add, 0 to change, 0 to destroy.
    
    Note: You didn't use the -out option to save this plan, so Terraform can't
    guarantee to take exactly these actions if you run "terraform apply" now.
    
  4. מחילים את התצורה כדי להקצות את המשאבים שמתוארים בקובץ main.tf:

    terraform apply
    

    כשתופיע בקשה, כותבים yes.

    תישלח מ-Terraform בקשה ל- Google Cloud APIs כדי ליצור את המכונה הווירטואלית שמוגדרת בקובץ main.tf.

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

    Apply complete! Resources: 1 added, 0 changed, 0 destroyed
    

התחברות למכונת ה-VM

מתחברים למכונת ה-VM שיצרתם באמצעות הפקודה הבאה:

gcloud compute ssh --zone=us-central1-a my-vm

הסרת המשאבים

כדי לא לצבור חיובים בחשבון Google Cloud על המשאבים שבהם השתמשתם בדף הזה, אתם צריכים למחוק את Google Cloud הפרויקט יחד עם המשאבים.

ב-Cloud Shell, מריצים את הפקודה הבאה כדי למחוק את משאבי Terraform:

terraform destroy

כשתופיע בקשה, כותבים yes.

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

Destroy complete! Resources: 1 destroyed.

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