במדריך הזה נדגים איך לפרוס פונקציית HTTP באמצעות העלאה של קובץ zip של קוד המקור של הפונקציה לקטגוריה של Cloud Storage, תוך שימוש ב-Terraform כדי להקצות את המשאבים. Terraform הוא כלי בקוד פתוח שמאפשר להקצות Google Cloud משאבים באמצעות קובצי תצורה מוצהרים.
במדריך הזה נעשה שימוש בפונקציית HTTP של Node.js כדוגמה, אבל הוא מתאים גם לפונקציות HTTP של Python, Go ו-Java. ההוראות זהות לא משנה באיזה זמן ריצה משתמשים.
כשפורסים באמצעות Terraform, צריך להעלות את קובץ המקור הדחוס של הפונקציה לקטגוריה של Cloud Storage (source_archive_bucket), וגם לציין את שם האובייקט ב-Cloud Storage (source_archive_object) בהגדרות של Terraform. מידע נוסף זמין במדריך המפרט של Terraform.
פונקציות Cloud Run מעתיקות את קובץ המקור שמעלים ב-source_archive_bucket לקטגוריה בפרויקט עם שם קטגוריה בפורמט gcf-v2-sources-PROJECT_NUMBER-REGION(פונקציות Cloud Run) או gcf-sources-PROJECT_NUMBER-REGION פונקציות Cloud Run (דור ראשון). ההגדרה הזו משתנה בהתאם לתלות ב-CMEK.
מטרות
- כאן תוכלו ללמוד איך משתמשים ב-Terraform כדי לפרוס פונקציית HTTP.
עלויות
במסמך הזה משתמשים ברכיבים הבאים של Google Cloud, והשימוש בהם כרוך בתשלום:
For details, see Cloud Run functions pricing.
כדי ליצור הערכת עלויות בהתאם לשימוש החזוי, אתם יכולים להשתמש במחשבון התמחור.
לפני שמתחילים
- נכנסים לחשבון Google Cloud . אנחנו ממליצים למשתמשים חדשים ב- Google Cloud ליצור חשבון כדי שיוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Functions, Cloud Run, Cloud Build, Artifact Registry, and Cloud Storage APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Functions, Cloud Run, Cloud Build, Artifact Registry, and Cloud Storage APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
הכנת סביבת הפיתוח.
אם כבר התקנתם את ה-CLI של gcloud, מריצים את הפקודה הבאה כדי לעדכן אותו:
gcloud components update
התפקידים הנדרשים
למשתמשים שמבצעים פריסה של פונקציות צריך להיות תפקיד Cloud Functions Developer (
roles/cloudfunctions.developer) ב-IAM או תפקיד שכולל את אותן הרשאות. אפשר לעיין גם במאמר הגדרות נוספות לפריסה.כדי לקבל את ההרשאות לגישה לקטגוריה של Cloud Storage, צריך לבקש מהאדמין להקצות לזהות ה-IAM שמבצעת את הפריסה של הפונקציה את התפקיד אדמין לניהול אחסון (
roles/storage.admin). פרטים נוספים על תפקידים והרשאות ב-Cloud Storage זמינים במאמר IAM ל-Cloud Storage.
הגדרת הסביבה
במדריך הזה מריצים פקודות ב-Cloud Shell. Cloud Shell היא סביבת מעטפת שבה ה-CLI של Google Cloud כבר מותקן, כולל Google Cloud CLI, ומוגדרים ערכים לפרויקט הקיים. יכול להיות שיחלפו כמה דקות עד ש-Cloud Shell יופעל:
הכנת הבקשה
ב-Cloud Shell, מבצעים את השלבים הבאים:
משכפלים את מאגר האפליקציה לדוגמה למכונת Cloud Shell:
git clone https://github.com/terraform-google-modules/terraform-docs-samples.git
עוברים לספרייה שמכילה את דוגמאות הקוד של פונקציות Cloud Run:
cd terraform-docs-samples/functions/basic
הדוגמה ל-Node.JS שבה נעשה שימוש במדריך הזה היא פונקציית HTTP בסיסית של Hello World. הנה הקובץ
main.tf:
הפעלת Terraform
בספרייה terraform-docs-samples/functions/basic שמכילה את הקובץ main.tf, מריצים את הפקודה הבאה כדי להוסיף את הפלאגינים הנדרשים וליצור את הספרייה .terraform:
terraform init
החלת ההגדרות של Terraform
באותה תיקייה terraform-docs-samples/functions/basic שמכילה את הקובץ main.tf, פורסים את הפונקציה על ידי החלת ההגדרה. כשמופיעה בקשה, מזינים את
yes:
terraform apply
בדיקת הפונקציה
כשהפונקציה מסיימת את הפריסה, כדאי לשים לב למאפיין ה-URI או לאתר אותו באמצעות הפקודה הבאה:
gcloud functions describe function-v2 --gen2 --region=us-central1 --format="value(serviceConfig.uri)"שולחים בקשה לכתובת ה-URL הזו כדי לראות את ההודעה 'Hello World' של הפונקציה. שימו לב: הפונקציה נפרסת ונדרש אימות. לכן, עליך לספק פרטי כניסה בבקשה:
curl -H "Authorization: Bearer $(gcloud auth print-identity-token)" YOUR_FUNCTION_URL
הסרת המשאבים
כשתסיימו את המדריך תוכלו למחוק את כל מה שיצרתם, כדי שלא תחויבו.
תוכלו להריץ את הפקודה terraform destroy כדי להסיר את כל המשאבים שהוגדרו בקובץ התצורה של Terraform. הפקודה מופעלת בספרייה terraform-docs-samples/functions/basic שמכילה את הקובץ main.tf:
terraform destroy
כדי לאפשר ל-Terraform למחוק את המשאבים, כותבים yes.