約 10 分で Terraform をインストールして設定し、コードとしてのインフラストラクチャ (IaC) の管理を開始できます。
Terraform は、「Terraform の概要」に記載されているように、バイナリパッケージとして配布されています。手動でインストールするか、パッケージマネージャーでインストールできます。
Terraform のインストール
手動インストール
コンパイル済みパッケージの使用
Terraform 公式ウェブサイトにアクセスし、お使いのオペレーティングシステム用の ZIP パッケージを見つけてダウンロードします。
ダウンロード後、パッケージを /usr/local/bin に解凍します。残りのファイルは安全に削除できます。
Terraform バイナリのパスを PATH 環境変数に追加します。手順はオペレーティングシステムによって異なります。
Mac または Linux
PATH の設定を確認します。
echo $PATH
Terraform バイナリを PATH 内のディレクトリに移動します。この例では、バイナリが 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 terraform
バイナリをコンパイルして $GOPATH/bin/ にインストールします。
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 に追加します。手順はオペレーティングシステムによって異なります。
Mac または Linux
go install コマンドは、コンパイルされたバイナリを $GOPATH/bin ディレクトリにインストールします。terraform コマンドを実行するには、このディレクトリが PATH 環境変数に含まれていることを確認してください。
echo $PATH
Terraform バイナリを、PATH に含まれるディレクトリに移動します。この例では、バイナリが Downloads フォルダーにあり、PATH に /usr/local/bin が含まれていることを前提としています。必要に応じてパスを調整してください。
export PATH=$PATH:$(go env GOPATH)/bin
Windows
-
[コントロールパネル] > [システム] > [システムの設定] > [環境変数] に移動します。
-
[システム環境変数] で [PATH] を見つけます。
-
[編集] をクリックします。
-
パスエントリをセミコロンで区切ります。例:c:\path;c:\path2
-
設定を有効にするために新しいコンソールを開きます。
macOS Homebrew
Homebrew は macOS 用のパッケージマネージャーです。次のコマンドで Terraform をインストールします。
HashiCorp tap をインストールします。
brew tap hashicorp/tap
Terraform をインストールします。
brew install hashicorp/tap/terraform
このコマンドは最新バージョンをインストールします。後でアップデートするには、アップグレードコマンドを実行します。
Terraform をアップデートするには、まず Homebrew をアップデートします。
brew update
次に Terraform をアップグレードします。
brew upgrade hashicorp/tap/terraform
Windows Chocolatey
Chocolatey は Windows 用のパッケージマネージャーです。Chocolatey を使い、1 つのコマンドで Terraform をインストールします。
choco install terraform
Cloud Shell
Alibaba Cloud Shell は、Terraform がプリインストールされた無料の運用管理ツールです。Cloud Shell で直接 Terraform コマンドを実行できます。
必要な権限を持つ Alibaba Cloud アカウントがあることを確認してください。
または https://shell.alibabacloud.com/ で Cloud Shell を開きます。
ログイン後、次のコマンドを実行します。
terraform
出力から、Cloud Shell で Terraform が利用可能であることが確認できます。
shell@Alicloud:~$ 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.
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
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: 120 M
Is this ok [y/N]: y
...
Complete!
インストールの確認
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.