本页面介绍了在 Cloud Shell 和本地 Shell 中为 Google Cloud 安装和配置 Terraform 的步骤。Cloud Shell 是 Google Cloud 的一种交互式 Shell 环境,可让您学习和试用 Google Cloud,以及通过网络浏览器管理项目和资源。
Cloud Shell
如需使用已设置 gcloud CLI 和 Terraform 的在线终端,请激活 Cloud Shell:
Cloud Shell 会话会在页面底部启动,并显示命令行提示符。该会话可能需要几秒钟来完成初始化。
-
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.
如需使用 Terraform 预配 Google Cloud 资源,您必须启用相应的 API:
gcloud services enable "API"
将 API 替换为您要启用的 API。
如需使用 Terraform 预配 Google Cloud 资源,您需要具有特定于这些资源的 Identity and Access Management 角色。
- 在 IAM 角色参考页面中确定所需的角色。
-
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:myemail@example.com
. - Replace
ROLE
with each individual role.
- Replace
运行以下命令以验证 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
-
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.
-
Install the Google Cloud CLI, then initialize it by running the following command:
gcloud init
启用必需的 API:
gcloud services enable "API"
其中 API 是您要启用的 API。
如需使用 Terraform 预配 Google Cloud 资源,您需要具有特定于这些资源的 Identity and Access Management 角色。
- 在 IAM 角色参考页面中确定所需的角色。
-
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:myemail@example.com
. - Replace
ROLE
with each individual role.
- Replace
安装 Terraform。
运行以下命令以验证 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