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

本頁面說明如何使用 Google Cloud 控制台部署 HTTP Cloud Run 函式。

事前準備

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Artifact Registry, Cloud Build, Cloud Run Admin API, and Cloud Logging APIs.

    Enable the APIs

  5. Make sure that you have the following role or roles on the project: Cloud Run Developer, Cloud Run Admin, Service Account Admin, Service Account User, Project IAM Admin, Quota Admin

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      前往「IAM」頁面
    2. 選取專案。
    3. 按一下「授予存取權」
    4. 在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。

    5. 在「Select a role」(選取角色) 清單中,選取角色。
    6. 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
    7. 按一下 [Save]
  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  7. Make sure that billing is enabled for your Google Cloud project.

  8. Enable the Artifact Registry, Cloud Build, Cloud Run Admin API, and Cloud Logging APIs.

    Enable the APIs

  9. Make sure that you have the following role or roles on the project: Cloud Run Developer, Cloud Run Admin, Service Account Admin, Service Account User, Project IAM Admin, Quota Admin

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      前往「IAM」頁面
    2. 選取專案。
    3. 按一下「授予存取權」
    4. 在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。

    5. 在「Select a role」(選取角色) 清單中,選取角色。
    6. 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
    7. 按一下 [Save]
  10. 如果您受到網域限制組織政策限制,專案無法進行未經驗證的呼叫,則必須按照「測試私人服務」一節的說明存取已部署的服務。

  11. 將下列 IAM 角色授予 Cloud Build 服務帳戶。

    按一下即可查看 Cloud Build 服務帳戶的必要角色

    除非您覆寫這項行為,否則 Cloud Build 會自動使用 Compute Engine 預設服務帳戶做為預設的 Cloud Build 服務帳戶,建構您的原始碼和 Cloud Run 資源。如要讓 Cloud Build 建構來源,請要求管理員將 Cloud Run Builder (roles/run.builder) 授予專案中的 Compute Engine 預設服務帳戶:

      gcloud projects add-iam-policy-binding PROJECT_ID \
          --member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com \
          --role=roles/run.builder
      

    請將 PROJECT_NUMBER 替換為專案編號,並將 PROJECT_ID 替換為專案 ID。 Google CloudGoogle Cloud如需如何找出專案 ID 和專案編號的詳細操作說明,請參閱「建立及管理專案」。

    將 Cloud Run 建構者角色授予 Compute Engine 預設服務帳戶後,需要幾分鐘才能傳播

部署函式

重要事項:本快速入門導覽課程假設您在用於快速入門導覽課程的專案中,具備擁有者或編輯者角色。否則,請參閱 Cloud Run Source Developer 角色,瞭解從來源部署 Cloud Run 資源所需的權限。

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

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

    前往 Cloud Run

  2. 按一下「編寫函式」

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

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

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

  6. 在「Authentication」(驗證) 部分中,選取「Allow unauthenticated invocations」(允許未經驗證的叫用)。 如果沒有選取這個選項的權限 (Cloud Run 管理員角色),服務會部署並要求驗證。

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

  8. 在「來源」分頁中,按一下「儲存並重新部署」。視需要按一下「顯示酬載」,查看傳入要求物件。

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

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 資訊主頁中查看地區。

清除所用資源

雖然 Cloud Run 在服務未用時不會產生費用,但將容器映像檔儲存於 Artifact Registry 仍可能會產生費用。您可以刪除容器映像檔或刪除 Google Cloud 專案,以免產生費用。刪除 Google Cloud 專案後,系統就會停止對專案使用的所有資源收取費用。

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

後續步驟