安装和配置 Terraform

本页面介绍了在 Cloud Shell 和本地 Shell 中为 Google Cloud 安装和配置 Terraform 的步骤。Cloud Shell 是 Google Cloud 的一种交互式 Shell 环境,可让您学习和试用 Google Cloud,以及通过网络浏览器管理项目和资源。

Cloud Shell

  1. 如需使用已设置 gcloud CLI 和 Terraform 的在线终端,请激活 Cloud Shell:

    Cloud Shell 会话会在页面底部启动,并显示命令行提示符。该会话可能需要几秒钟来完成初始化。

  2. 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.

  3. 确保您的 Google Cloud 项目已启用结算功能

  4. 如需使用 Terraform 预配 Google Cloud 资源,您必须启用相应的 API:

    gcloud services enable "API"
    

    API 替换为您要启用的 API

  5. 如需使用 Terraform 预配 Google Cloud 资源,您需要具有特定于这些资源的 Identity and Access Management 角色。

    1. IAM 角色参考页面中确定所需的角色。
    2. Grant roles to your user account. Run the following command once for each of the following IAM roles:

      gcloud projects add-iam-policy-binding PROJECT_ID --member="USER_IDENTIFIER" --role=ROLE
      • Replace PROJECT_ID with your project ID.
      • Replace USER_IDENTIFIER with the identifier for your user account. For example, user:[email protected].

      • Replace ROLE with each individual role.
  6. 运行以下命令以验证 Terraform 是否可用:

    terraform
    

    输出应类似如下所示:

    
    Usage: terraform [global options] <subcommand> [args]
    
    The available commands for execution are listed below.
    The primary workflow commands are given first, followed by
    less common or more advanced commands.
    
    Main commands:
      init          Prepare your working directory for other commands
      validate      Check whether the configuration is valid
      plan          Show changes required by the current configuration
      apply         Create or update infrastructure
      destroy       Destroy previously-created infrastructure
    
    

本地 shell

  1. 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.

  2. 确保您的 Google Cloud 项目已启用结算功能

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

    gcloud init
  4. 启用必需的 API:

    gcloud services enable "API"
    

    其中 API 是您要启用的 API

  5. 如需使用 Terraform 预配 Google Cloud 资源,您需要具有特定于这些资源的 Identity and Access Management 角色。

    1. IAM 角色参考页面中确定所需的角色。
    2. Grant roles to your user account. Run the following command once for each of the following IAM roles:

      gcloud projects add-iam-policy-binding PROJECT_ID --member="USER_IDENTIFIER" --role=ROLE
      • Replace PROJECT_ID with your project ID.
      • Replace USER_IDENTIFIER with the identifier for your user account. For example, user:[email protected].

      • Replace ROLE with each individual role.
  6. 安装 Terraform。

  7. 运行以下命令以验证 Terraform 是否可用:

    terraform
    

    输出应类似如下所示:

    
     Usage: terraform [global options] <subcommand> [args]
    
    The available commands for execution are listed below.
    The primary workflow commands are given first, followed by
    less common or more advanced commands.
    
    Main commands:
      init          Prepare your working directory for other commands
      validate      Check whether the configuration is valid
      plan          Show changes required by the current configuration
      apply         Create or update infrastructure
      destroy       Destroy previously-created infrastructure
    

后续步骤