Terraform 的驗證

本文說明如何在使用 Terraform 時驗證 Google Cloud 。

使用 Terraform 時,建議您使用應用程式預設憑證 (ADC) 驗證Google Cloud 。ADC 是驗證程式庫使用的策略,可根據應用程式環境自動尋找憑證。使用 ADC 時,Terraform 可以在開發或實際環境中執行,無須變更對Google Cloud 服務和 API 進行驗證的方式。如要瞭解 ADC 會在哪裡尋找憑證,以及尋找順序,請參閱「應用程式預設憑證的運作方式」。

在本機開發環境中使用 Terraform 時進行驗證

在本機開發環境 (例如開發工作站) 中使用 Terraform 時,您可以使用與使用者帳戶服務帳戶相關聯的憑證進行驗證。

使用使用者帳戶驗證

如要使用使用者帳戶設定 ADC,請使用 Google Cloud CLI:

  1. After installing the Google Cloud CLI, initialize it 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.

  2. 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 會自動使用這些憑證。

  1. 請務必在要冒用服務帳戶時,具備服務帳戶符記建立者 (roles/iam.serviceAccountTokenCreator) IAM 角色。詳情請參閱「必要角色」。

  2. 執行下列指令,使用服務帳戶冒用功能建立本機 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 與 Google Cloud Compute Engine、App Engine 和 Cloud Run 函式等服務搭配使用時,可以將使用者管理的服務帳戶附加至資源。一般來說,如果服務的資源可以執行或包含應用程式程式碼,系統就會支援附加服務帳戶。將服務帳戶連結至資源後,在該資源上執行的程式碼就能使用該服務帳戶做為身分。

為在 Google Cloud上執行的正式版程式碼,為 ADC 提供憑證,建議您採用附加使用者管理服務帳戶的方式。

如要瞭解如何判斷需要為服務帳戶提供哪些角色,請參閱「選擇預先定義的角色」。

如要瞭解可將服務帳戶連結至哪些資源,以及如何將服務帳戶連結至資源,請參閱 IAM 說明文件 (關於連結服務帳戶)。

Set up authentication:

  1. Create the service account:

    gcloud iam service-accounts create SERVICE_ACCOUNT_NAME

    Replace SERVICE_ACCOUNT_NAME with a name for the service account.

  2. 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 account
    • PROJECT_ID: the project ID where you created the service account
    • ROLE: the role to grant
  3. To grant another role to the service account, run the command as you did in the previous step.
  4. 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 account
    • PROJECT_ID: the project ID where you created the service account
    • USER_EMAIL: the email address for a Google Account

在地端部署環境或其他雲端供應商環境中執行 Terraform 時進行驗證

如果您在 Google Cloud以外執行應用程式,就必須提供 Google Cloud 可辨識的憑證,才能使用Google Cloud 服務。

使用 Workload Identity 聯盟驗證

使用外部 IdP 的憑證驗證 Google Cloud 的首選方式是使用Workload Identity 聯盟。您可以建立憑證設定檔,並將 GOOGLE_APPLICATION_CREDENTIALS 環境變數設為指向該檔案。這個方法比建立服務帳戶金鑰更安全。如要瞭解如何為 ADC 設定 Workload Identity Federation,請參閱「與其他雲端服務的 Workload Identity Federation」。

使用服務帳戶金鑰驗證

在本機開發環境、內部部署或其他雲端服務供應商中執行 Terraform 時,您可以建立服務帳戶,授予該帳戶應用程式所需的 IAM 角色,並為服務帳戶建立金鑰。

如要建立服務帳戶金鑰並提供給 ADC,請按照下列步驟操作:

  1. 按照「建立服務帳戶金鑰」一文中的操作說明,建立具有應用程式所需角色的服務帳戶,以及該服務帳戶的金鑰。

    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 後端頁面

後續步驟