本文說明使用 Terraform 時,如何向 Google Cloud 進行驗證。
使用 Terraform 時,建議使用應用程式預設憑證 (ADC) 向Google Cloud 進行驗證。ADC 是一種策略,驗證程式庫會使用這種策略,根據應用程式環境自動尋找憑證。使用 ADC 時,Terraform 可以在開發或生產環境中執行,不必變更向Google Cloud 服務和 API 進行驗證的方式。如要瞭解 ADC 搜尋憑證的位置和順序,請參閱「應用程式預設憑證的運作方式」。
在本機開發環境中使用 Terraform 時進行驗證
在本機開發環境 (例如開發工作站) 中使用 Terraform 時,您可以透過與使用者帳戶或服務帳戶相關聯的憑證進行驗證。
使用使用者帳戶進行驗證
如要使用使用者帳戶設定 ADC,請使用 Google Cloud CLI:
-
Install the Google Cloud CLI. After installation, initialize the Google Cloud CLI by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
畫面上會顯示登入畫面。登入後,您的憑證會儲存在 ADC 使用的 本機憑證檔案中。
使用服務帳戶模擬功能進行驗證
您可以使用服務帳戶模擬功能設定本機 ADC 檔案。Terraform 會自動使用這些憑證。
請務必在要模擬的服務帳戶中,擁有服務帳戶憑證建立者 (
roles/iam.serviceAccountTokenCreator
) IAM 角色。詳情請參閱「必要角色」。執行下列指令,使用服務帳戶模擬功能建立本機 ADC 檔案:
gcloud auth application-default login --impersonate-service-account SERVICE_ACCT_EMAIL
如要允許使用者使用共用的主要驗證來源,以及每個環境的變數服務帳戶,請在 Terraform 設定檔中設定 impersonate_service_account
欄位:
provider "google" {
impersonate_service_account = "SERVICE_ACCT_EMAIL"
}
在 Google Cloud上執行 Terraform 時進行驗證
在 Cloud Shell 等 Google Cloud 雲端開發環境中執行 Terraform 時,工具會使用您登入時提供的憑證進行驗證。
搭配使用 Terraform 與 Compute Engine、App Engine 和 Cloud Run 函式等 Google Cloud 服務時,您可以將使用者管理的服務帳戶附加至資源。一般來說,如果服務的資源可以執行或包含應用程式碼,系統就會支援附加服務帳戶。將服務帳戶附加至資源後,在資源上執行的程式碼就能使用該服務帳戶做為身分。
將使用者代管服務帳戶附加至 ADC,是為 Google Cloud上執行的正式版程式碼提供憑證的建議做法。
如需協助判斷要提供給服務帳戶的角色,請參閱「選擇預先定義的角色」。
如要瞭解可附加服務帳戶的資源,以及如何將服務帳戶附加至資源,請參閱這篇 IAM 說明文件。
Set up authentication:
-
Create the service account:
gcloud iam service-accounts create SERVICE_ACCOUNT_NAME
Replace
SERVICE_ACCOUNT_NAME
with a name for the service account. -
To provide access to your project and your resources, grant a role to the service account:
gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" --role=ROLE
Replace the following:
SERVICE_ACCOUNT_NAME
: the name of the service accountPROJECT_ID
: the project ID where you created the service accountROLE
: the role to grant
- To grant another role to the service account, run the command as you did in the previous step.
-
Grant the required role to the principal that will attach the service account to other resources.
gcloud iam service-accounts add-iam-policy-binding SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com --member="user:USER_EMAIL" --role=roles/iam.serviceAccountUser
Replace the following:
SERVICE_ACCOUNT_NAME
: the name of the service accountPROJECT_ID
: the project ID where you created the service accountUSER_EMAIL
: the email address for a Google Account
在地端或不同雲端供應商執行 Terraform 時進行驗證
如果您在 Google Cloud以外的位置執行應用程式,則必須提供 Google Cloud 可辨識的憑證,才能使用Google Cloud 服務。
使用 Workload Identity 聯盟驗證
如要使用外部 IdP 的憑證進行驗證,建議使用 Workload Identity 聯盟。 Google Cloud 您可以建立憑證設定檔,並設定 GOOGLE_APPLICATION_CREDENTIALS
環境變數來指向該檔案。相較於建立服務帳戶金鑰,這種做法更安全。如需為 ADC 設定 Workload Identity 聯盟的操作說明,請參閱「Workload Identity 聯盟與其他雲端」。
使用服務帳戶金鑰驗證
在本地開發環境、內部部署或不同雲端供應商中執行 Terraform 時,您可以建立服務帳戶、授予應用程式所需的 IAM 角色,並為服務帳戶建立金鑰。
如要建立服務帳戶金鑰並提供給 ADC 使用,請按照下列步驟操作:
按照「建立服務帳戶金鑰」一文中的操作說明,建立具有應用程式所需角色的服務帳戶,並取得該服務帳戶的金鑰。
Set the environment variable
GOOGLE_APPLICATION_CREDENTIALS
to the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again.
驗證 Cloud Storage 後端
Terraform 可讓您將 Cloud Storage 設定為後端,用來儲存 Terraform 狀態檔案。如要驗證 Cloud Storage 後端,請使用本頁面所述的任一方法。如要瞭解與 Cloud Storage 後端驗證相關的設定變數,請參閱 Cloud Storage 的 Terraform 後端頁面。