快速入門導覽課程:使用 Google Cloud 控制台部署 Cloud Run 函式
本頁面說明如何使用 Google Cloud 控制台部署 HTTP Cloud Run 函式。
事前準備
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
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 Artifact Registry, Cloud Build, Cloud Run Admin API, and Cloud Logging APIs.
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. -
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 Artifact Registry, Cloud Build, Cloud Run Admin API, and Cloud Logging APIs.
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. - 查看 Cloud Run 定價,或使用 Pricing Calculator 估算費用。
-
專案的「Cloud Run 原始碼開發人員」 (
roles/run.sourceDeveloper) -
服務帳戶使用者 (
roles/iam.serviceAccountUser) 服務身分 -
專案的記錄檢視器 (
roles/logging.viewer) 前往 Google Cloud 控制台的 Cloud Run 頁面:
按一下「編寫函式」。
在「Service name」(服務名稱) 欄位中,輸入服務名稱來描述函式,例如
my-nodejs-http-function。服務名稱開頭須為英文字母,且最多只能包含 49 個字元,包括英文字母、數字或連字號。服務名稱結尾不得為連字號,且每個區域和專案的服務名稱不得重複。 服務名稱一經設定即無法變更,而且會公開顯示。在「Region」(區域) 清單中,使用預設值
europe-west1,或選取要部署函式的區域。在「執行階段」清單中,使用預設值或選取執行階段版本。
在「Authentication」(驗證) 部分中,選取「Allow public access」(允許公開存取)。 如果沒有權限 (Cloud Run 管理員角色) 可選取這個選項,服務就會部署並要求驗證。
按一下「建立」,然後等待 Cloud Run 使用預留位置修訂版本建立服務。主控台會將您重新導向至「來源」分頁,您可以在這裡查看函式的原始碼。
在「來源」分頁中,按一下「儲存並重新部署」。視需要按一下「顯示酬載」,查看傳入要求物件。
部署完成後,容器的網址會顯示在「網址:」文字旁。 如要查看函式的輸出內容,請按一下「複製到剪貼簿」複製網址,然後將網址貼到瀏覽器的網址列。
asia-east1(臺灣)asia-northeast1(東京)asia-northeast2(大阪)asia-south1(印度孟買)europe-north1(芬蘭)二氧化碳排放量低
europe-north2(斯德哥爾摩)二氧化碳排放量低
europe-southwest1(馬德里)二氧化碳排放量低
europe-west1(比利時)二氧化碳排放量低
europe-west4(荷蘭)二氧化碳排放量低
europe-west8(米蘭)europe-west9(巴黎)二氧化碳排放量低
me-west1(特拉維夫)northamerica-south1(墨西哥)us-central1(愛荷華州)二氧化碳排放量低
us-east1(南卡羅來納州)us-east4(北維吉尼亞州)us-east5(哥倫布)us-south1(達拉斯)二氧化碳排放量低
us-west1(奧勒岡州)二氧化碳排放量低
africa-south1(約翰尼斯堡)asia-east2(香港)asia-northeast3(韓國首爾)asia-southeast1(新加坡)asia-southeast2(雅加達)asia-south2(印度德里)australia-southeast1(雪梨)australia-southeast2(墨爾本)europe-central2(波蘭華沙)europe-west10(柏林)europe-west12(杜林)europe-west2(英國倫敦)二氧化碳排放量低
europe-west3(德國法蘭克福)europe-west6(瑞士蘇黎世)二氧化碳排放量低
me-central1(杜哈)me-central2(達曼)northamerica-northeast1(蒙特婁)二氧化碳排放量低
northamerica-northeast2(多倫多)二氧化碳排放量低
southamerica-east1(巴西聖保羅)二氧化碳排放量低
southamerica-west1(智利聖地牙哥)二氧化碳排放量低
us-west2(洛杉磯)us-west3(鹽湖城)us-west4(拉斯維加斯)前往 Google Cloud 控制台的 Cloud Run「服務」頁面:
在服務清單中找出您要刪除的服務,然後按一下核取方塊來選取。
按一下「Delete」(刪除)。如此便會刪除服務的所有修訂版本。
- SERVICE:服務名稱。
- REGION:服務的 Google Cloud 區域。
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
如要使用 Google Cloud CLI 將範例函式部署至 Cloud Run,請參閱「快速入門:使用 gcloud CLI 將函式部署至 Cloud Run」。
如要使用 Google Cloud 控制台和 Google Cloud CLI 部署函式,請參閱「部署函式」。
如要查看及刪除現有函式,請參閱「管理服務修訂版本」。
如要在自己的工具鍊中建構函式容器,並將其部署至 Cloud Run,請參閱「建構函式」。
如要使用 Eventarc 建立觸發條件,請參閱「使用 Eventarc 建立觸發條件」。
必要的角色
如要取得完成本快速入門導覽課程所需的權限,請要求管理員授予您下列 IAM 角色:
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
授予 Cloud Build 服務帳戶專案存取權
除非您覆寫這項行為,否則 Cloud Build 會自動使用 Compute Engine 預設服務帳戶做為預設的 Cloud Build 服務帳戶,建構您的原始碼和 Cloud Run 資源。
如要讓 Cloud Build 建構來源,請在專案中將 Cloud Run 建構者 (roles/run.builder) 角色授予 Cloud Build 服務帳戶:
gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:SERVICE_ACCOUNT_EMAIL_ADDRESS \ --role=roles/run.builder
將 PROJECT_ID 替換為您的 Google Cloud專案 ID,並將 SERVICE_ACCOUNT_EMAIL_ADDRESS 替換為 Cloud Build 服務帳戶的電子郵件地址。如果您使用 Compute Engine 預設服務帳戶做為 Cloud Build 服務帳戶,請使用下列格式的服務帳戶電子郵件地址:
PROJECT_NUMBER-compute@developer.gserviceaccount.com
將 PROJECT_NUMBER 替換為您的專案編號。 Google Cloud
如需如何找出專案 ID 和專案編號的詳細操作說明,請參閱「建立與管理專案」。
授予 Cloud Run 建構工具角色需要幾分鐘才能傳播。
部署函式
如要部署函式,請按照下列步驟操作:
Cloud Run 位置
Cloud Run 具有「地區性」,這表示執行 Cloud Run 服務的基礎架構位於特定地區,並由 Google 代管,可為該地區內所有區域提供備援功能。
選擇 Cloud Run 服務的執行地區時,請將延遲時間、可用性或耐用性需求做為主要考量。一般而言,您可以選擇最靠近使用者的地區,但您應考量 Cloud Run 服務所使用的其他 Google Cloud
產品位置。使用分散在不同位置的 Google Cloud 產品,可能會影響服務的延遲時間和費用。
Cloud Run 可在下列地區使用:
採用級別 1 定價
採用級別 2 定價
如果您已建立 Cloud Run 服務,即可在 Google Cloud 控制台的 Cloud Run 資訊主頁中查看地區。
清除所用資源
為避免系統向您的 Google Cloud 帳戶收取額外費用,請刪除您透過本快速入門導覽課程部署的所有資源。
刪除存放區
部署的服務閒置時,Cloud Run 不會向您收費。不過,您可能仍須支付在 Artifact Registry 中儲存容器映像檔的費用。如要刪除 Artifact Registry 存放區,請按照 Artifact Registry 說明文件中的「刪除存放區」一節操作。
刪除服務
Cloud Run 服務收到要求後才會開始計費。如要刪除 Cloud Run 服務,請按照下列步驟操作:
控制台
如要刪除服務:
gcloud
如要刪除服務,請執行下列指令:
gcloud run services delete SERVICE --region REGION
更改下列內容:
刪除測試專案
刪除專案後,系統就會停止對該專案中的所有資源收取費用。 Google Cloud 如要釋出專案中的所有 Google Cloud 資源,請按照下列步驟操作: