tfenv を使って Terraform をインストールする

Terraform のお勉強がしたくなり、インストールしたので手順を記録しておく。

環境

  • macOS 13.0.1
  • Homebrew 3.6.17

tfenv

Homebrew でインストールする。

$ brew install tfenv

バージョン確認する。

$ tfenv --version            
tfenv 3.0.0

インストール可能な Terraform のバージョンをリストで取得する。

$ tfenv list-remote
1.4.0-alpha20221207
1.4.0-alpha20221109
1.3.6
()

alpha版を除いて一番新しい1.3.6をインストールする。

$ tfenv install 1.3.6

Terraform

とりあえず terraform コマンド実行してみる。

$ terraform
cat: /usr/local/Cellar/tfenv/3.0.0/version: No such file or directory
Version could not be resolved (set by /usr/local/Cellar/tfenv/3.0.0/version or tfenv use <version>)

使用するバージョンの指定が必要らしい。 使用できるバージョンを確認する。

$ tfenv list
  1.3.6
No default set. Set with 'tfenv use <version>'

先にインストールした 1.3.6 のみ表示された。 そちらを使用するよう設定する。

$ tfenv use 1.3.6

再度 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
  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          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.

Usage が表示されたので問題なさそう!

まとめ

tfenv を使用して Terraform をインストールする手順を記録した。

Terraform 使っていくのが楽しみ。Go で書かれているし、詰まったらソース読めるのは大きい...!(CDK はソース読むのが辛かった)

参考