מדריך למתחילים: יצירת מכונה וירטואלית באמצעות Terraform
במדריך למתחילים הזה מוסבר איך להשתמש ב-Terraform כדי ליצור מכונה וירטואלית (VM) ב-Compute Engine ולהתחבר למכונה הווירטואלית הזו.
Hashicorp Terraform הוא כלי של תשתית כקוד (IaC), שבעזרתו אפשר להקצות ולנהל תשתית ענן. Terraform provider for Google Cloud (Google Cloud provider) מאפשר הקצאה וניהול של תשתית Google Cloud .
לפני שמתחילים
כדי להשתמש בטרמינל אונליין שכבר מוגדרים בו ה-CLI של gcloud ו-Terraform, צריך להפעיל את Cloud Shell:
בחלק התחתון של הדף הזה מתחיל סשן של Cloud Shell ומופיעה הנחיה של שורת הפקודה. הסשן יופעל תוך כמה שניות.
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith 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_IDwith your Google Cloud project name.
-
Verify that billing is enabled for your Google Cloud project.
-
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 theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable compute.googleapis.com
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/compute.instanceAdmin.v1gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
Replace the following:
PROJECT_ID: Your project ID.USER_IDENTIFIER: The identifier for your user account. For examples, see Represent workforce pool users in IAM policies.ROLE: The IAM role that you grant to your user account.
הכנת הסביבה
משכפלים את מאגר GitHub שמכיל דוגמאות ל-Terraform:
git clone https://github.com/terraform-google-modules/terraform-docs-samples.git --single-branchעוברים לספרייה שמכילה את הדוגמה למדריך למתחילים:
cd terraform-docs-samples/compute/quickstart/create_vm
בדיקת קובצי Terraform
בודקים את הקובץ main.tf. בקובץ הזה מוגדרים המשאבים שרוצים ליצור. Google Cloud
cat main.tf
הפלט אמור להיראות כך:
בקובץ הזה מתואר המשאב 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
ב-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מפעילים את 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!מריצים את הפקודה הבאה כדי לאמת את הגדרות 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.- מוודאים שהתחביר של
מחילים את התצורה כדי להקצות את המשאבים שמתוארים בקובץ
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.