במדריך למתחילים הזה מוסבר איך ליצור קובץ תצורה של Terraform, שמקצה קטגוריית אחסון ומעלה אובייקט sample_file.txt לקטגוריה. כדי להשלים את המדריך למתחילים הזה, משתמשים בטרמינל ובמעטפת המקומית, או ב-Cloud Shell Editor ובטרמינל Cloud Shell. בנוסף, תשתמשו ב-CLI של Terraform, שמותקן מראש ב-Cloud Shell.
לפני שמתחילים
כדי להגדיר פרויקט בשביל המדריך למתחילים הזה, מבצעים את השלבים הבאים:
-
In the Cloud de Confiance console, on the project selector page, select or create a Cloud de Confiance 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.
-
Verify that billing is enabled for your Cloud de Confiance project.
Enable the Cloud Storage 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.
יצירת מבנה התיקיות וקובץ התצורה של Terraform
כדי ליצור את קובץ התצורה של Terraform ואת הקובץ שרוצים להעלות כאובייקט ל-Cloud Storage, מבצעים את השלבים הבאים:
Cloud Shell
-
במסוף Cloud de Confiance , מפעילים את Cloud Shell.
בחלק התחתון של Cloud de Confiance המסוף יתחיל סשן של Cloud Shell ותופיע הודעה של שורת הפקודה. Cloud Shell היא סביבת מעטפת שבה ה-CLI של Google Cloud מותקן ומוגדרים ערכים לפרויקט הקיים. הסשן יופעל תוך כמה שניות.
- מגדירים את פרויקט ברירת המחדל שעליו רוצים להחיל את ההגדרות האישיות של Terraform:
Cloud de Confiance by S3NSexport GOOGLE_CLOUD_PROJECT=PROJECT_ID
- בטרמינל של Cloud Shell, מגדירים את ספריית הבית כספרייה הפעילה:
cd - יוצרים תיקייה חדשה בשם
terraform:
mkdir terraform - כדי לפתוח את Cloud Shell Editor, לוחצים על Open Editor בסרגל הכלים שבחלון של Cloud Shell.
- בחלונית Explorer לוחצים לחיצה ימנית על התיקייה
terraformואז לוחצים על New File. - מזינים את
main.tfבתור שם הקובץ ולוחצים על OK. - בחלונית Explorer לוחצים לחיצה ימנית על התיקייה
terraformואז לוחצים על New File. - מזינים את
sample_file.txtבתור שם הקובץ ולוחצים על OK.
מעטפת מקומית
- אם עדיין לא עשיתם זאת, מתקינים ומגדירים את Terraform.
חשוב להתקין ולהפעיל את Google Cloud CLI.
כברירת מחדל, Terraform קורא את ההגדרות שנוצרו על ידי ה-CLI של Google Cloud ומפריס את המשאבים שציינתם מאוחר יותר בפרויקט הפעיל של ה-CLI של Google Cloud.
- בטרמינל, מגדירים את ספריית הבית כספרייה הפעילה:
cd - יוצרים תיקייה חדשה בשם
terraform:
mkdir terraform - בכלי לעריכת טקסט שבוחרים, יוצרים קובץ חדש בשם
main.tfבתיקייהterraform. - בכלי לעריכת טקסט שבוחרים, יוצרים קובץ חדש בשם
sample_file.txtבתיקייהterraform.
הגדרת התשתית בקובץ התצורה של Terraform
כדי להגדיר את התשתית שרוצים להקצות בקובץ התצורה של Terraform, מבצעים את השלבים הבאים:
פותחים את הקובץ
main.tf.מעתיקים את הדוגמה הבאה לקובץ
main.tf.# Create new storage bucket in the U-FRANCE-EAST1 # location with Standard Storage resource "google_storage_bucket" "static" { name = "BUCKET_NAME" location = "U-FRANCE-EAST1" storage_class = "STANDARD" uniform_bucket_level_access = true } # Upload a text file as an object # to the storage bucket resource "google_storage_bucket_object" "default" { name = "OBJECT_NAME" source = "OBJECT_PATH" content_type = "text/plain" bucket = google_storage_bucket.static.id }מחליפים את:
BUCKET_NAME בשם הקטגוריה שרוצים ליצור. לדוגמה,
my-bucket.OBJECT_NAME בשם האובייקט שרוצים להעלות. לצורך השימוש במדריך למתחילים הזה, מזינים את השם
sample_file.txt.OBJECT_PATH בנתיב לאובייקט שרוצים להעלות. לצורך השימוש במדריך למתחילים הזה, מזינים את הנתיב
~/terraform/sample_file.txt.
שומרים את קובץ ה-
main.tf.
אתחול ספריית העבודה שמכילה את קובץ התצורה של Terraform
כדי לאתחל את Terraform ואת הספרייה שמכילה את קובץ התצורה של Terraform, מבצעים את השלבים הבאים:
בטרמינל, מגדירים את התיקייה
terraformכספריית העבודה הנוכחית:cd ~/terraformמאתחלים את Terraform:
terraform initאם אתם משתמשים ב-Cloud Shell ומוצגת לכם בקשה לאשר את Cloud Shell, לוחצים על Authorize.
Terraform מאתחלת את ספריית העבודה. אם ספריית העבודה אותחלה בהצלחה, Terraform מחזירה פלט שדומה לזה:
Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary.
תצוגה מקדימה של תוכנית הביצוע
תוכנית הביצוע של Terraform מבוססת על ההגדרות האישיות של Terraform, ומציינת את השינויים ש-Terraform מתכננת לבצע בתשתית ובשירותים של Cloud Storage.
צפייה בתוכנית הביצוע של Terraform:
terraform planפלט לדוגמה:
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# google_storage_bucket.static will be created
+ resource "google_storage_bucket" "static" {
+ force_destroy = false
+ id = (known after apply)
+ location = "U-FRANCE-EAST1"
+ name = "my-bucket"
+ project = "my-project"
+ public_access_prevention = (known after apply)
+ self_link = (known after apply)
+ storage_class = "STANDARD"
+ uniform_bucket_level_access = true
+ url = (known after apply)
+ versioning {
+ enabled = (known after apply)
}
+ website {
+ main_page_suffix = (known after apply)
+ not_found_page = (known after apply)
}
}
# google_storage_bucket_object.default will be created
+ resource "google_storage_bucket_object" "default" {
+ bucket = (known after apply)
+ content_type = "text/plain"
+ crc32c = (known after apply)
+ detect_md5hash = "different hash"
+ id = (known after apply)
+ kms_key_name = (known after apply)
+ md5hash = (known after apply)
+ media_link = (known after apply)
+ name = "sample_file.txt"
+ output_name = (known after apply)
+ self_link = (known after apply)
+ source = "sample_file.txt"
+ storage_class = (known after apply)
}
Plan: 2 to add, 0 to change, 0 to destroy.
החלת השינויים שמוצעים בתוכנית הביצוע
כדי להחיל את השינויים בקובץ התצורה של Terraform, מבצעים את השלבים הבאים:
מחילים את השינויים מתוכנית הביצוע על התשתית של Cloud Storage באמצעות הפקודה הבאה. כשמחילים את השינויים, Terraform יוצרת קטגוריית אחסון ומעלה את
sample_file.txtלקטגוריה.terraform applyפלט לדוגמה:
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # google_storage_bucket.static will be created + resource "google_storage_bucket" "static" { + force_destroy = false + id = (known after apply) + location = "U-FRANCE-EAST1" + name = "my-bucket" + project = "my-project" + public_access_prevention = (known after apply) + self_link = (known after apply) + storage_class = "STANDARD" + uniform_bucket_level_access = true + url = (known after apply) + versioning { + enabled = (known after apply) } + website { + main_page_suffix = (known after apply) + not_found_page = (known after apply) } } # google_storage_bucket_object.default will be created + resource "google_storage_bucket_object" "default" { + bucket = (known after apply) + content_type = "text/plain" + crc32c = (known after apply) + detect_md5hash = "different hash" + id = (known after apply) + kms_key_name = (known after apply) + md5hash = (known after apply) + media_link = (known after apply) + name = "sample_file.txt" + output_name = (known after apply) + self_link = (known after apply) + source = "sample_file.txt" + storage_class = (known after apply) } Plan: 2 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value:מקלידים
yesומקישים Enter.אם הפעולה בוצעה בהצלחה, Terraform מחזירה פלט שדומה לזה:
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
צפייה בקטגוריית האחסון ובאובייקט שהועלה
במסוף Cloud de Confiance , נכנסים לדף Buckets של Cloud Storage.הקטגוריה החדשה תופיע, ותכיל את האובייקט sample_file.txt. שימו לב: יכול להיות שיעברו מספר דקות עד להקצאת המשאבים אחרי הרצה של terraform apply.
פינוי מקום בפרויקט
כדי למנוע חיובים לא צפויים מהמשאבים של Google Cloud שיצרתם במדריך למתחילים הזה, השלימו את השלבים הבאים כדי למחוק את המשאבים: Cloud de Confiance by S3NS
בטרמינל, מגדירים את התיקייה
terraformכספריית העבודה הנוכחית:cd ~/terraformמוחקים את המשאבים של Cloud Storage שנוצרו על סמך קובץ התצורה של Terraform:
terraform destroyאם הפעולה בוצעה בהצלחה, Terraform מחזירה פלט שדומה לזה:
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: - destroy Terraform will perform the following actions: # google_storage_bucket.static will be destroyed - resource "google_storage_bucket" "static" { - default_event_based_hold = false -> null - force_destroy = false -> null - id = "my-bucket" -> null - labels = {} -> null - location = "U-FRANCE-EAST1" -> null - name = "" -> null - project = "example-project" -> null - public_access_prevention = "inherited" -> null - requester_pays = false -> null - self_link = "https://www.s3nsapis.fr/storage/v1/b/cbonnie-bucket-9" -> null - storage_class = "STANDARD" -> null - uniform_bucket_level_access = true -> null - url = "gs://BUCKET_NAME" -> null } # google_storage_bucket_object.default will be destroyed - resource "google_storage_bucket_object" "default" { - bucket = "my-bucket" -> null - content_type = "text/plain" -> null - crc32c = "yZRlqg==" -> null - detect_md5hash = "XrY7u+Ae7tCTyyK7j1rNww==" -> null - event_based_hold = false -> null - id = "my-bucket-sample_file.txt" -> null - md5hash = "XrY7u+Ae7tCTyyK7j1rNww==" -> null - media_link = "https://storage.googleapis.com/download/storage/v1/b/BUCKET_NAME/o/sample_file.txt?generation=1675800386233102&alt=media" -> null - metadata = {} -> null - name = "sample_file.txt" -> null - output_name = "sample_file.txt" -> null - self_link = "https://www.s3nsapis.fr/storage/v1/b/BUCKET_NAME/o/sample_file.txt" -> null - source = "sample_file.txt" -> null - storage_class = "STANDARD" -> null - temporary_hold = false -> null } Plan: 0 to add, 0 to change, 2 to destroy. Do you really want to destroy all resources? Terraform will destroy all your managed infrastructure, as shown above. There is no undo. Only 'yes' will be accepted to confirm. Enter a value:מקלידים
yesומקישים Enter. אם הפעולה בוצעה בהצלחה, Terraform מחזירה פלט שדומה לזה:Destroy complete! Resources: 2 destroyed.בטרמינל, מוחקים את התיקייה
terraform.rm -rf ~/terraformכדי לוודא שהקטגוריה והאובייקט נמחקו, נכנסים לדף Buckets במסוף Cloud de Confiance .