使用 Go (第 1 代) 建立及部署 HTTP Cloud Run 函式
本指南將逐步說明如何使用 Go 執行階段編寫 Cloud Run 函式。Cloud Run 函式分為兩種類型:
- HTTP 函式,可從標準 HTTP 要求叫用。
- 事件驅動函式,用於處理 Cloud 基礎架構的事件,例如 Pub/Sub 主題上的訊息,或 Cloud Storage 值區的變更。
這個範例說明如何建立簡單的 HTTP 函式。
事前準備
- 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 Cloud Functions and Cloud Build 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 Cloud Functions and Cloud Build 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. - 安裝並初始化 gcloud CLI。
- 更新並安裝
gcloud元件:gcloud components update
- 準備開發環境。
在本機系統上為函式程式碼建立目錄:
Linux 或 Mac OS X
mkdir ~/helloworld cd ~/helloworldWindows
mkdir %HOMEPATH%\helloworld cd %HOMEPATH%\helloworld在
helloworld目錄中建立名為hello_http.go的檔案,並加入以下內容:此範例函式採用在 HTTP 要求中提供的名稱,並傳回問候語,如未提供任何名稱,系統會傳回「Hello, World!」。
當函式完成部署時,記下
httpsTrigger.url屬性,或使用下列指令找到這個屬性:gcloud functions describe HelloHTTP內容應該會類似這樣:
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/HelloHTTP
在瀏覽器中前往這個網址,或執行下列指令來使用 cURL:
curl https://GCP_REGION-PROJECT_ID.cloudfunctions.net/HelloHTTP
您應該會看到「Hello, World!」訊息。請執行下列指令,嘗試透過 HTTP 要求傳送名稱:
curl -X POST https://GCP_REGION-PROJECT_ID.cloudfunctions.net/HelloHTTP -H "Content-Type:application/json" -d '{"name":"NAME"}'您應該會看到「Hello,
NAME!」訊息
建立函式
指定依附元件
這個範例函式只使用 Go 標準程式庫套件,因此除了匯入套件之外,您不需要宣告任何依附元件。
如果函式需要標準程式庫以外的依附元件,您必須透過 go.mod 檔案或 vendor 目錄提供依附元件。詳情請參閱「在 Go 中指定依附元件」。
部署函式
如要使用 HTTP 觸發條件部署函式,請在 helloworld 目錄中執行下列指令,並根據您使用的版本,將 go113 或 go111 指定為 --runtime 標記的值:
gcloud functions deploy HelloHTTP --no-gen2 --runtime go121 --trigger-http --allow-unauthenticated
--allow-unauthenticated 旗標可讓您不必驗證就能使用函式。如要要求驗證,請略過該旗標。
測試函式
查看記錄
您可以使用 Google Cloud CLI,以及在 Cloud Logging UI 中查看 Cloud Run 函式的記錄。
使用指令列工具
如要透過 gcloud CLI 查看函式的記錄檔,請使用 logs read 指令加上函式的名稱:
gcloud functions logs read HelloHTTP
輸出應會如下所示:
LEVEL NAME EXECUTION_ID TIME_UTC LOG D HelloHTTP buv9ej2k1a7r 2019-09-20 13:23:18.910 Function execution started D HelloHTTP buv9ej2k1a7r 2019-09-20 13:23:18.913 Function execution took 4 ms, finished with status code: 200
使用記錄資訊主頁
您也可以從Google Cloud 控制台查看 Cloud Run 函式的記錄。