Terraform 的身份验证

本文档介绍了如何在使用 Terraform 时向 Google Cloud 进行身份验证。

使用 Terraform 时,建议使用应用默认凭据 (ADC) 向 Google Cloud 进行身份验证。ADC 是身份验证库使用的一种策略,可根据应用环境自动查找凭据。使用 ADC 时,Terraform 可以在开发或生产环境中运行,而无需更改它向 Google Cloud 服务和 API 进行身份验证的方式。如需了解 ADC 查找凭据的位置和顺序,请参阅应用默认凭据的工作原理

在本地开发环境中使用 Terraform 时进行身份验证

在本地开发环境(例如开发工作站)中使用 Terraform 时,您可以使用与您的用户账号服务账号关联的凭据进行身份验证。

使用 Google 账号进行身份验证

如需使用 Google 账号配置 ADC,您可以使用 Google Cloud CLI:

  1. Install the Google Cloud CLI, then initialize it by running the following command:

    gcloud init
  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.

    登录屏幕随即出现。在您登录后,您的凭据会存储在 ADC 使用的本地凭据文件中。

使用外部账号进行身份验证

如需为由外部身份提供商管理的用户账号配置 ADC,请执行以下操作:

  1. 配置员工身份联合

  2. 配置 gcloud CLI 以使用员工身份联合

  3. 通过运行以下命令来配置 ADC:

    gcloud auth application-default login

    登录屏幕随即出现。在您登录后,您的凭据会存储在 ADC 使用的本地凭据文件中。

使用服务账号模拟进行身份验证

您可以使用服务账号模拟来设置本地 ADC 文件。Terraform 会自动使用这些凭据。

  1. 确保您在要模拟的服务账号上具有 Service Account Token Creator (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 与 Compute Engine、App Engine 和 Cloud Run 函数等 Google Cloud 服务搭配使用时,您可以将用户管理的服务账号与资源相关联。通常,当服务的资源可以运行或包含应用代码时支持关联服务账号。将服务账号关联到资源后,该资源上运行的代码可以使用该服务账号作为其身份。

关联用户管理的服务账号是针对 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 服务。

使用工作负载身份联合进行身份验证

使用外部 IdP 的凭据进行 Google Cloud 身份验证的首选方法是使用工作负载身份联合。您可以创建凭据配置文件,并将 GOOGLE_APPLICATION_CREDENTIALS 环境变量设置为指向该文件。此方法比创建服务账号密钥更安全。 如需了解如何设置适用于 ADC 的工作负载身份联合,请参阅与其他云服务的工作负载身份联合

使用服务账号密钥进行身份验证

在本地开发环境、本地环境或其他云服务商中运行 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 后端页面

后续步骤