במדריך הפשוט הזה נסביר איך לכתוב, לפרוס ולהפעיל פונקציית HTTP Cloud Run.
מטרות
- כתיבה, פריסה והפעלה של פונקציית HTTP Cloud Run.
עלויות
במסמך הזה משתמשים ברכיבים הבאים של Google Cloud, והשימוש בהם כרוך בתשלום:
- Cloud Run functions
כדי להעריך את ההוצאות בהתאם לתחזית השימוש שלכם, אתם יכולים להיעזר במחשבון העלויות.
לפני שמתחילים
- נכנסים לחשבון 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 and Cloud Build APIs.
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.-
התקינו את ה-CLI של Google Cloud.
-
אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.
-
כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:
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 and Cloud Build APIs.
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.-
התקינו את ה-CLI של Google Cloud.
-
אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.
-
כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:
gcloud init -
מכינים את סביבת הפיתוח.
Node.js
Python
המשך
Java
Ruby
אם כבר התקנתם את ה-CLI של gcloud, מריצים את הפקודה הבאה כדי לעדכן אותו:
gcloud components update
הכנת הבקשה
משכפלים את מאגר האפליקציה לדוגמה ומעבירים אותו למכונה המקומית:
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
אפשרות נוספת היא להוריד את הדוגמה כקובץ ZIP ולחלץ אותה.
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
אפשרות נוספת היא להוריד את הדוגמה כקובץ ZIP ולחלץ אותה.
המשך
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
אפשרות נוספת היא להוריד את הדוגמה כקובץ ZIP ולחלץ אותה.
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
אפשרות נוספת היא להוריד את הדוגמה כקובץ ZIP ולחלץ אותה.
Ruby
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git
אפשרות נוספת היא להוריד את הדוגמה כקובץ ZIP ולחלץ אותה.
עוברים לספרייה שמכילה את הקוד לדוגמה של פונקציות Cloud Run:
Node.js
cd nodejs-docs-samples/functions/helloworld/
Python
cd python-docs-samples/functions/helloworld/
Go
cd golang-samples/functions/helloworld/
Java
cd java-docs-samples/functions/helloworld/helloworld/
Ruby
cd ruby-docs-samples/functions/helloworld/
כדאי לעיין בקוד לדוגמה:
Node.js
Python
המשך
Java
Ruby
פריסת הפונקציה
כדי לפרוס את הפונקציה עם טריגר HTTP, מריצים את הפקודה הבאה בספרייה שמכילה את קוד הדוגמה (או במקרה של Java, את הקובץ pom.xml):
Node.js
gcloud functions deploy helloGET \ --runtime nodejs22 --trigger-http
משתמשים בדגל --runtime כדי לציין את מזהה סביבת זמן הריצה של גרסת Node.js נתמכת להרצת הפונקציה.
Python
gcloud functions deploy hello_get \ --runtime python312 --trigger-http
משתמשים בדגל --runtime כדי לציין את מזהה זמן הריצה של גרסת Python נתמכת להרצת הפונקציה.
המשך
gcloud functions deploy HelloGet \ --runtime go121 --trigger-http
כדי לציין את מזהה זמן הריצה של גרסת Go נתמכת להרצת הפונקציה, משתמשים בדגל --runtime.
Java
gcloud functions deploy java-http-function \ --entry-point functions.HelloWorld \ --runtime java17 \ --memory 512MB --trigger-http
משתמשים בדגל --runtime כדי לציין את מזהה זמן הריצה של גרסת Java נתמכת להרצת הפונקציה.
Ruby
gcloud functions deploy hello_get --runtime ruby33 --trigger-http
משתמשים בדגל --runtime כדי לציין את מזהה זמן הריצה של גרסת Ruby נתמכת להרצת הפונקציה.
אפשר גם להשתמש בדגל --allow-unauthenticated כדי להגיע לפונקציה ללא אימות.
ההגדרה הזו שימושית לבדיקות, אבל אנחנו לא ממליצים להשתמש בה בסביבת ייצור אלא אם אתם יוצרים API או אתר ציבוריים. בנוסף, יכול להיות שהיא לא תפעל אצלכם, בהתאם להגדרות המדיניות של החברה. במאמר אימות להפעלה מוסבר איך להפעיל פונקציה שדורשת אימות.
הפעלת הפונקציה
כדי לשלוח בקשת HTTP לפונקציה, מריצים את הפקודה הבאה:
Node.js
curl "https://REGION-PROJECT_ID.cloudfunctions.net/helloGET"
Python
curl "https://REGION-PROJECT_ID.cloudfunctions.net/hello_get"
Go
curl "https://REGION-PROJECT_ID.cloudfunctions.net/HelloGet"
Java
curl "https://REGION-PROJECT_ID.cloudfunctions.net/java-http-function"
Ruby
curl "https://REGION-PROJECT_ID.cloudfunctions.net/hello_get"
איפה
-
REGIONהוא האזור שבו הפונקציה נפרסת. המידע הזה מוצג במסוף כשפריסת הפונקציה מסתיימת. -
PROJECT_IDהוא מזהה הפרויקט שלכם ב-Cloud. המידע הזה מוצג במסוף כשפריסת הפונקציה מסתיימת.
אפשר גם להיכנס לנקודת הקצה של הפונקציה שהופעלה בדפדפן כדי לראות את ההודעה Hello World!.
הסרת המשאבים
כדי להימנע מחיובים בחשבון Google Cloud בגלל השימוש במשאבים שנעשה במסגרת המדריך הזה, אפשר למחוק את הפרויקט שמכיל את המשאבים, או להשאיר את הפרויקט ולמחוק את המשאבים בנפרד.
מחיקת הפרויקט
הדרך הקלה ביותר לבטל את החיוב היא למחוק את הפרויקט שיצרתם בשביל המדריך.
כדי למחוק את הפרויקט:
- במסוף Google Cloud , נכנסים לדף Manage resources.
- ברשימת הפרויקטים, בוחרים את הפרויקט שרוצים למחוק ולוחצים על Delete.
- כדי למחוק את הפרויקט, כותבים את מזהה הפרויקט בתיבת הדו-שיח ולוחצים על Shut down.
מחיקת הפונקציה
מחיקה של פונקציות Cloud Run לא מסירה משאבים שמאוחסנים ב-Cloud Storage.
כדי למחוק את פונקציית Cloud Run שיצרתם במדריך הזה, מריצים את הפקודה הבאה:
Node.js
gcloud functions delete helloGET
Python
gcloud functions delete hello_get
Go
gcloud functions delete HelloGet
Java
gcloud functions delete java-http-function
Ruby
gcloud functions delete hello_get
אפשר גם למחוק פונקציות Cloud Run ממסוףGoogle Cloud .