Terraform必须首先安装在您的计算机上。Terraform作为二进制包分发给所有受支持的平台和架构。本页将不介绍如何从源代码编译Terraform,但对于那些希望确保将信任的源代码编译成最终二进制文件的人,文档中将介绍如何从源代码编译Terraform。
原文地址:https://learn.hashicorp.com/terraform/getting-started/install
安装Terraform
要安装Terraform,请找到适合你系统的软件包并下载它。Terraform被打包成一个zip文件。
下载Terraform后并解压。Terraform作为一个名为Terraform的二进制文件运行。包中的任何其他文件都可以安全删除,Terraform仍将正常工作。
最后一步是确保Terraform二进制文件在路径上可用。有关在Linux和Mac上设置路径的说明,请参阅本页。此页包含有关在Windows上设置路径的说明。
验证安装
安装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
# ...
如果出现找不到Terraform的错误,则说明PATH环境变量设置不正确。请返回并确保PATH变量包含安装Terraform的目录。
获取帮助信息
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.