使用 Terraform 建立工作流程
本快速入門導覽課程說明如何使用 Terraform 建立、部署及執行第一個工作流程。Terraform 是一種基礎架構即程式碼工具,可讓您透過程式碼,以可預測的方式建立、變更及改善雲端基礎架構。瞭解如何使用 Terraform 在 Google Cloud上佈建基礎架構。
在本快速入門中,範例工作流程會傳送要求至公用 API,然後傳回 API 的回應。
您將完成下列步驟:
- 使用 Terraform 啟用 Workflows API。
- 使用 Terraform 為工作流程建立服務帳戶。
- 使用 Terraform 定義及部署工作流程。
- 使用 Google Cloud CLI 執行工作流程。
事前準備
貴機構定義的安全性限制,可能會導致您無法完成下列步驟。如需疑難排解資訊,請參閱「在受限的 Google Cloud 環境中開發應用程式」。
請注意,Cloud Shell 已整合 Terraform。如果您需要安裝 Terraform,請參閱 HashiCorp Terraform 說明文件。
- 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.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Resource Manager and Identity and Access Management (IAM) APIs:
gcloud services enable cloudresourcemanager.googleapis.com<wbr> iam.googleapis.com
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Resource Manager and Identity and Access Management (IAM) APIs:
gcloud services enable cloudresourcemanager.googleapis.com<wbr> iam.googleapis.com
建立 Terraform 設定檔
建立名為 main.tf
的 Terraform 設定檔,並加入這個快速入門課程中使用的 Terraform 適用的 Google 供應器 資源。
請注意,您可以使用插補功能進行替換,例如參照變數、資源屬性和呼叫函式。
建立目錄:
mkdir terraform
前往
terraform
目錄:cd terraform
在目錄中新增檔案
main.tf
:nano main.tf
將下列資源新增至
main.tf
檔案:指派專案 ID:
provider "google" { project = "PROJECT_ID" }
將
PROJECT_ID
替換為專案 ID。啟用 Workflows API:
為工作流程建立服務帳戶:
使用
google_workflows_workflow
資源定義工作流程:範例工作流程中使用的引數如下:
name
:工作流程名稱。region
:工作流程的位置。description
:工作流程的說明。service_account
:與最新工作流程版本相關聯的服務帳戶電子郵件地址或專屬 ID。這個服務帳戶代表工作流程的身分,並決定工作流程擁有哪些權限。如果您在建立工作流程時未指定服務帳戶,工作流程會使用預設 Compute Engine 服務帳戶做為身分。詳情請參閱「授予工作流程權限,以便存取 Google Cloud 資源」。labels
:鍵/值標籤組合清單,可指派給這個工作流程,協助您整理 Google Cloud 例項。詳情請參閱「什麼是標籤?」user_env_vars
:與此工作流程修訂版本相關聯的使用者定義環境變數。詳情請參閱「使用環境變數」。source_contents
:要執行的 Workflows 程式碼。如要瞭解檔案大小限制,請參閱「資源限制」一節。
其他選用引數包括:
crypto_key_name
:Cloud Key Management Service 金鑰的資源 ID,格式如下:projects/PROJECT_NAME/locations/LOCATION/keyRings/KEY_RING/cryptoKeys/KEY_NAME
詳情請參閱「使用客戶管理的加密金鑰」。
call_log_level
:在執行此工作流程時,要套用至呼叫和呼叫回應的記錄層級。可能的值包括:CALL_LOG_LEVEL_UNSPECIFIED
LOG_ALL_CALLS
LOG_ERRORS_ONLY
LOG_NONE
詳情請參閱「通話記錄」。
project
:資源所屬專案的 ID。如未提供,系統會使用供應者專案。name_prefix
:建立以指定前置字串開頭的專屬名稱。如果未指定這個值和name
,系統會隨機選擇名稱。
建立及執行工作流程
部署 Terraform 資源來建立工作流程,然後執行工作流程。
在目錄中初始化 Terraform:
terraform init
確認您透過 Terraform 提出的變更是否符合預期的計畫:
terraform plan
您可以忽略有關不使用
-out
選項的註解。建立工作流程:
terraform apply
在「輸入值」提示中,輸入
yes
即可繼續建立資源。確認已建立工作流程:
gcloud workflows list --location us-central1
畫面會顯示如下的輸出內容:
NAME STATE REVISION_ID UPDATE_TIME projects/project-name/locations/us-central1/workflows/sample-workflow ACTIVE 000001-f9a 2024-02-24T13:38:58.353765906Z
您也可以選擇執行工作流程:
gcloud workflows execute sample-workflow
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取您在本頁面使用資源的費用,請刪除含有這些資源的 Google Cloud 專案。
- 刪除您使用 Terraform 建立的所有資源:
terraform destroy
- 刪除您建立的工作流程:
系統詢問是否要繼續時,請輸入gcloud workflows delete sample-workflow
y
。 - 您也可以刪除 Google Cloud 專案,避免產生費用。刪除 Google Cloud 專案後,系統就會停止對專案使用的所有資源收取費用。
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-09 (世界標準時間)。