快速入門導覽課程:使用 Google Cloud 控制台部署 Cloud Run 函式

Cloud Run functions 可讓您根據 HTTP 要求執行程式碼,不必管理基礎架構。在本快速入門導覽課程中,您將使用 Google Cloud 控制台部署及測試範例函式。

事前準備

  1. 登入 Google Cloud 帳戶。如果您未曾使用過 Google Cloud,歡迎 建立帳戶,親自體驗實際使用我們產品的成效。新客戶還能獲得價值 $300 美元的免費抵免額,用於執行、測試及部署工作負載。
  2. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  4. Verify that billing is enabled for your Google Cloud project.

  5. Enable the Artifact Registry, Cloud Build, Cloud Run Admin API, and Cloud Logging APIs, if any are not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. 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.

    Enable the APIs

  6. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  7. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  8. Verify that billing is enabled for your Google Cloud project.

  9. Enable the Artifact Registry, Cloud Build, Cloud Run Admin API, and Cloud Logging APIs, if any are not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. 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.

    Enable the APIs

  10. 如果專案受限於限制未經驗證叫用的網域限制組織政策,您必須按照「測試私人服務」一節所述的方式存取已部署的服務。

  11. 查看 Cloud Run 定價,或使用 Pricing Calculator 估算費用。

必要的角色

如要取得完成本快速入門導覽課程所需的權限,請要求管理員授予您下列 IAM 角色:

如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。

您或許也能透過自訂角色或其他預先定義的角色,取得必要權限。

授予 Cloud Build 服務帳戶專案存取權

Cloud Build 會自動使用 Compute Engine 預設服務帳戶,做為建構原始碼和 Cloud Run 資源的預設 Cloud Build 服務帳戶,除非您覆寫這項行為。

如要讓 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 建構工具角色後,需要幾分鐘的時間才會生效。

部署函式

如要部署函式,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Cloud Run」頁面:

    前往 Cloud Run。

  2. 點選「編寫函式」。

  3. 在「Service name」(服務名稱) 欄位中,輸入函式的服務名稱,例如 my-nodejs-http-function。服務名稱開頭須為英文字母,且最多只能包含 49 個字元,包括英文字母、數字或連字號。服務名稱不得以連字號結尾,且每個區域和專案的服務名稱不得重複。服務名稱設定後即無法變更,且會公開顯示。

  4. 在「Region」(區域) 清單中,使用預設值 europe-west1,或選取要部署函式的區域。

  5. 在「Runtime」(執行階段) 清單中,使用預設值或選取執行階段版本。

  6. 在「Authentication」(驗證) 專區中,選取「Allow public access」(允許公開存取)。如果您沒有選取這項設定的權限 (Cloud Run 管理員角色),服務會部署並要求驗證。

  7. 按一下「Create」(建立),等待 Cloud Run 使用預留位置修訂版本建立服務。控制台會將您重新導向至「Source」(來源) 分頁,您可以在這裡查看函式的原始碼。

  8. 在「來源」分頁中,按一下「儲存並重新部署」。您也可以按一下「Show Payload」(顯示酬載),查看傳入的要求物件。

  9. 部署完成後,容器的網址會顯示在「URL:」(網址:) 文字旁。如要查看函式的輸出內容,請按一下「Copy to clipboard」(複製到剪貼簿) 複製網址,然後將網址貼到瀏覽器的網址列。

Cloud Run 位置

Cloud Run 具有區域性,這表示執行 Cloud Run 服務的基礎架構位於特定區域,並由 Google 負責管理,實現該區域內所有可用區之間的備援可用性。

選擇執行 Cloud Run 服務的區域時,主要考量重點在於延遲時間、可用性或耐用性需求。原則上可以選擇距離使用者最近的區域,但也應考慮 Cloud Run 服務所用其他 Google Cloud產品的位置。如果跨多個位置使用 Google Cloud 產品,可能會影響服務的延遲時間和費用。

Cloud Run 可在下列地區使用:

採用級別 1 定價

採用級別 2 定價

  • 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 (拉斯維加斯)

如果您已建立 Cloud Run 服務,可以在Google Cloud 控制台的 Cloud Run 資訊主頁中查看區域。

清除所用資源

為避免系統向您的 Google Cloud 帳戶收取額外費用,請刪除您透過本快速入門導覽課程部署的所有資源。

刪除存放區

Cloud Run 不會對未使用的已部署服務收費。不過,您可能仍須支付在 Artifact Registry 儲存容器映像檔的費用。如要刪除 Artifact Registry 存放區,請按照 Artifact Registry 說明文件中的「刪除存放區」步驟操作。

刪除服務

Cloud Run 服務在收到要求前不會產生費用。如要刪除 Cloud Run 服務,請按照下列任一步驟操作:

控制台

如要刪除服務:

  1. 前往 Google Cloud 控制台的 Cloud Run「Services」(服務) 頁面:

    前往 Cloud Run。

  2. 在服務清單中找出您要刪除的服務,然後按一下核取方塊來選取。

  3. 按一下「Delete」(刪除)。這項操作會刪除服務的所有修訂版本。

gcloud

如要刪除服務,請執行下列指令:

gcloud run services delete SERVICE --region REGION

更改下列內容:

  • SERVICE:服務名稱。
  • REGION:服務的 Google Cloud 區域。

刪除測試專案

刪除 Google Cloud 專案後,系統會停止對該專案中的所有資源計費。如要釋出專案中的所有 Google Cloud 資源,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。

    前往「Manage resources」(管理資源)

  2. 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)。
  3. 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。

後續步驟