使用之前首先必须将 Terraform 安装在你的机器上。Terraform 是以二进制安装包发布的,它可以支持所有的平台和架构。在本章节我们将不会涉及如何通过源代码编译 Terraform,但是对于那些需要通过源代码编译来构建可信任的二进制的用户,我们在指南的其他章节会有阐述。
- 安装 Terraform
Terraform 安装包是zip格式的压缩文件,安装之前需首先根据系统类型下载合适的安装包。
Terraform 安装包下载完成后进行解压。Terraform 是一个名称为 terraform 的可执行的二进制文件,包中的其他任何文件都可以删除也不影响 Terraform 正常工作。
最后,确保 terraform 二进制文件路径添加到 PATH 环境变量中。请参见本指南中关于在Linux、Mac、Windows上如何设置 PATH。
- 验证安装
Terraform 安装完成之后,打开终端检查 terraform 是否可用,执行 terraform 命令可以看到如下类似的帮助提示:
$ terraform
Usage: terraform [--version] [--help] <command> [args]
The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you're just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.
Common commands:
apply Builds or changes infrastructure
console Interactive console for Terraform interpolations
# ...
如果看到 terrform 无法找到的错误信息,那么就是 PATH 环境变量没有设置正确。请检查确保 PATH 变量包含 Terrform 的安装路径。
- 获取帮助
Terraform CLI 内置了在线帮助,所有命令都可以使用 --help 参数来获得帮助信息,例如:
$ terraform -help
Usage: terraform [-version] [-help] <command> [args]
The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you are just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.
Common commands:
apply Builds or changes infrastructure
console Interactive console for Terraform interpolations
destroy Destroy Terraform-managed infrastructure
env Workspace management
fmt Rewrites config files to canonical format
get Download and install modules for the configuration
graph Create a visual graph of Terraform resources
import Import existing infrastructure into Terraform
init Initialize a Terraform working directory
output Read an output from a state file
plan Generate and show an execution plan
providers Prints a tree of the providers used in the configuration
refresh Update local state file against real resources
show Inspect Terraform state or plan
taint Manually mark a resource for recreation
untaint Manually unmark a resource as tainted
validate Validates the Terraform files
version Prints the Terraform version
workspace Workspace management
All other commands:
0.12upgrade Rewrites pre-0.12 module source code for v0.12
debug Debug output management (experimental)
force-unlock Manually unlock the terraform state
push Obsolete command for Terraform Enterprise legacy (v1)
state Advanced state management
任意这些子命令都可以加上 --help 参数获取更多的帮助信息,例如:
$ terraform --help plan
Usage: terraform plan [options] [DIR]
Generates an execution plan for Terraform.
This execution plan can be reviewed prior to running apply to get a
sense for what Terraform will do. Optionally, the plan can be saved to
a Terraform plan file, and apply can take this plan file to execute
this plan exactly.
Options:
-destroy If set, a plan will be generated to destroy all resources
managed by the given configuration and state.
-detailed-exitcode Return detailed exit codes when the command exits. This
will change the meaning of exit codes to:
0 - Succeeded, diff is empty (no changes)
1 - Errored
2 - Succeeded, there is a diff
-input=true Ask for input for variables if not directly set.
-lock=true Lock the state file when locking is supported.
-lock-timeout=0s Duration to retry a state lock.
-no-color If specified, output will not contain any color.
-out=path Write a plan file to the given path. This can be used as
input to the "apply" command.
-parallelism=n Limit the number of concurrent operations. Defaults to 10.
-refresh=true Update state prior to checking for differences.
-state=statefile Path to a Terraform state file to use to look
up Terraform-managed resources. By default it will
use the state "terraform.tfstate" if it exists.
-target=resource Resource to target. Operation will be limited to this
resource and its dependencies. This flag can be used
multiple times.
-var "foo=bar" Set a variable in the Terraform configuration. This
flag can be set multiple times.
-var-file=foo Set variables in the Terraform configuration from
a file. If "terraform.tfvars" or any ".auto.tfvars"
files are present, they will be automatically loaded.