Terraform安装
用10分钟时间完成Terraform的安装和配置,为通过Terraform生产基础设施做好准备。
在了解阿里云Terraform的介绍中提到,Terraform是以二进制包的形式分发。所以您可以使用常见的包管理器安装Terraform。
安装Terraform
手动安装
使用预编译包
登录Terraform官网,找到对应操作系统的zip包,并下载。
在下载完成后,请将程序包解压到/usr/local/bin。其余文件在复制完成后均可删除,这并不会影响Terraform正常运行。
最后,请确保Terraform目录在PATH变量中已正确配置,不同操作系统会有所不同。
Mac或Linux
打印您的PATH配置
echo $PATH
将Terraform二进制文件移动到列出的位置之一。此命令假定二进制文件当前位于您的Downloads文件夹中并且您的PATH包含/usr/local/bin,但如果您的位置不同,您可以自定义命令中的目录。
mv ~/Downloads/terraform /usr/local/bin/
Windows
-
进入控制面板 -> 系统 -> 系统设置 -> 环境变量。
-
在系统变量中向下滚动,直到找到 PATH。
-
单击编辑并进行相应更改。
-
请务必在前一个末尾添加分号,因为这是分隔符,即 c:\path;c:\path2
-
启动新控制台以使设置生效。
使用源
如果您期望从源代码编译二进制文件,可以克隆HashiCorp Terraform代码库
git clone https://github.com/hashicorp/terraform.git
您将会看到如下进度提示信息,并等待其执行完成。
tiankai@U-QDY6MW2N-0211 0203-Quickstart % git clone https://github.com/hashicorp/terraform.git
Cloning into 'terraform'...
remote: Enumerating objects: 298204, done.
remote: Counting objects: 100% (3099/3099), done.
remote: Compressing objects: 100% (1576/1576), done.
remote: Total 298204 (delta 1962), reused 2369 (delta 1459), pack-reused 295105
Receiving objects: 100% (298204/298204), 302.63 MiB | 3.62 MiB/s, done.
Resolving deltas: 100% (187677/187677), done.
Updating files: 100% (4517/4517), done.
执行完成后,您执行命令的目录下会新增一个terraform名称的目录。通过cd指令进入该目录。
cd terraform
然后,执行install 指令,这将会编译目录并将编译后的包移动到$GOPATH/bin/terraform目录中
go install
当您看到如下提示信息,则说明正在编译中。等待完成后即可进行下一步操作。
tiankai@U-ODY6MW2N-0211 terraform % go install
go: downloading github.com/hashicorp/go-checkpoint v0.5.0
go: downloading github.com/apparentlymart/go-shquot v0.0.1
go: downloading github.com/apparentlymart/go-userdirs v0.0.0-20200915174352-b0c018a67c13
go: downloading github.com/hashicorp/cli v1.1.6
go: downloading github.com/hashicorp/go-plugin v1.6.0
go: downloading github.com/hashicorp/terraform-svchost v0.1.1
go: downloading github.com/mattn/go-shellwords v1.0.12
go: downloading github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db
go: downloading go.opentelemetry.io/contrib/exporters/autoexport v0.45.0
go: downloading go.opentelemetry.io/otel v1.24.0
go: downloading go.opentelemetry.io/otel/sdk v1.24.0
go: downloading go.opentelemetry.io/otel/trace v1.24.0
go: downloading github.com/hashicorp/go-cleanhttp v0.5.2
go: downloading github.com/hashicorp/go-uuid v1.0.3
go: downloading github.com/zclconf/go-cty v1.14.4
go: downloading github.com/hashicorp/hcl/v2 v2.20.0
go: downloading github.com/hashicorp/terraform-registry-address v0.2.3
go: downloading golang.org/x/text v0.14.0
注意:如果提示zsh: command not found: go,则您需要先安装go的环境。
最终,确保terraform目录在PATH中定义并可用。 PATH定义取决于您所使用的操作系统。
Mac或Linux
打印您的PATH配置
echo $PATH
将 Terraform 二进制文件移动到列出的位置之一。此命令假定二进制文件当前位于您的Downloads文件夹中并且您的PATH包含/usr/local/bin,但如果您的位置不同,您可以自定义命令中的目录。
mv ~/Downloads/terraform /usr/local/bin/
Windows
-
进入控制面板 -> 系统 -> 系统设置 -> 环境变量。
-
在系统变量中向下滚动,直到找到 PATH。
-
单击编辑并进行相应更改。
-
请务必在前一个末尾添加分号,如:c:\path;c:\path2
-
启动新控制台以使设置生效。
macOS Homebrew
Homebrew 是一款在Mac系统上经常使用的包安装工具。借助Homebrew可以通过简单的指令安装Terraform。
第一步,安装HashiCorp的tap,用来定义包在homebrew的位置。
brew tap hashicorp/tap
第二步,执行安装指令,安装Terraform
brew install hashicorp/tap/terraform
安装指令将索引最新的版本并进行安装,如果在安装一段时间后希望更新到最新版本。可以通过重新执行upgrade指令进行。
更新最新版本的Terraform,首先需要更新Homebrew。
brew update
然后,运行upgrade指令更新到最新版本。
brew upgrade hashicorp/tap/terraform
Windows Chocolatey
Chocolatey是一款在Windows系统上经常使用的包安装工具。借助Chocolatey可以通过简单的指令安装Terraform。
choco install terraform
CloudShell
阿里云Cloud Shell是一款帮助您运维的免费产品,预装了Terraform的组件,您可直接在Cloud Shell中运行Terraform的命令。
首先,确保您有一个合法的、有相关权限的阿里云账号。
其次,打开浏览器,访问Cloud Shell的地址https://shell.aliyun.com。
登录成功后,执行如下指令
terraform
您将看到,Terraform组件已经内置在CloudShell中,并且可以直接使用。
shell@Alicloud:~$ 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
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
login Obtain and save credentials for a remote host
logout Remove locally-stored credentials for a remote host
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 Remove the 'tainted' state from a resource instance
validate Validates the Terraform files
version Prints the Terraform version
workspace Workspace management
All other commands:
debug Debug output management (experimental)
force-unlock Manually unlock the terraform state
push Obsolete command for Terraform Enterprise legacy (v1)
state Advanced state management
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
shell@Alicloud:~$
更多Cloud Shell使用方式,请参见使用云命令行。
Linux
Alibaba Cloud Linux
yum install -y dnf-plugin-releasever-adapter
yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
yum install terraform
[root@iZbp17mtyk1tw8mmto999uZ ~]# yum install -y dnf-plugin-releasever-adapter
Last metadata expiration check: 0:04:07 ago on Tue 16 Jul 2024 02:45:35 PM CST.
Package dnf-plugin-releasever-adapter-1.0-3.al8.noarch is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[root@iZbp17mtyk1tw8mmto999uZ ~]# yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
Adding repo from: https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
[root@iZbp17mtyk1tw8mmto999uZ ~]# yum install terraform
Hashicorp Stable - x86_64 527 kB/s | 1.4 MB 00:02
Last metadata expiration check: 0:00:01 ago on Tue 16 Jul 2024 02:45:35 PM CST.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Installing:
terraform x86_64 1.9.2-1 hashicorp 27 M
Installing dependencies:
git x86_64 2.43.5-1.0.1.al8 alinux3-updates 92 K
git-core x86_64 2.43.5-1.0.1.al8 alinux3-updates 11 M
git-core-doc noarch 2.43.5-1.0.1.al8 alinux3-updates 3.1 M
perl-Error noarch 1:0.17025-2.1.al8 alinux3-os 46 K
Transaction Summary
================================================================================
Install 5 Packages
Total download size: 41 M
Installed size: 120 M
Is this ok [y/N]: y
...
Complete!
perl-Git noarch 2.43.5-1.0.1.al8 alinux3-updates 79 K
perl-TermReadKey x86_64 2.37-7.2.al8 alinux3-os 41 K
Transaction Summary
================================================================================
Install 7 Packages
Total download size: 41 M
Installed size: 131 M
Is this ok [y/N]: y
Downloading Packages:
验证安装
运行terraform验证路径配置。
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
All other commands:
console Try Terraform expressions at an interactive command prompt
fmt Reformat your configuration in the standard style
force-unlock Release a stuck lock on the current workspace
get Install or upgrade remote Terraform modules
graph Generate a Graphviz graph of the steps in an operation
import Associate existing infrastructure with a Terraform resource
login Obtain and save credentials for a remote host
logout Remove locally-stored credentials for a remote host
metadata Metadata related commands
output Show output values from your root module
providers Show the providers required for this configuration
refresh Update the state to match remote systems
show Inspect Terraform state or plan
state Advanced state management
taint Mark a resource instance as not fully functional
test Experimental support for module integration testing
untaint Remove the 'tainted' state from a resource instance
version Show the current Terraform version
workspace Manage workspaces
Global options (use these before the subcommand, if any):
-chdir=DIR Switch to a different working directory before executing the
given subcommand.
-help Show this message.
-version An alias for the "version" subcommand.
show Show the current state or a saved plan
state Advanced state management
taint Mark a resource instance as not fully functional
test Experimental support for module integration testing
untaint Remove the 'tainted' state from a resource instance
version Show the current Terraform version
workspace Workspace management
Global options (use these before the subcommand, if any):
-chdir=DIR Switch to a different working directory before executing the
given subcommand.
-help Show this help output, or the help for a specified subcommand.
-version An alias for the "version" subcommand.
常见问题
由于网络延迟等原因造成的 terraform init 超时,导致无法正常下载terraform。 可参见Terraform Init 加速方案配置