dshimizu/blog/alpha

とりとめのないITブログ

OpenTofu 1.6 が GA になったので使ってみた

はじめに

Linux Foundation のプロジェクトの1つである OpenTofu が、2024年1月10日に「OpenTofu 1.6.0」をリリースしました。2023年9月にTerraformからフォークしてから約4ヵ月でのリリースとなります。

www.linuxfoundation.org

OpenTofu をインストールして、個人で使っている Terraform を OpenTofu へ置き換えてみました。

Terraform と OpenTofu の違いとか

機能面の違い

FAQ に記載がありました。

opentofu.org

掻い摘んで記載すると以下のようなことが書かれているようでした。

  • OpenTofu 1.6 と Terraform 1.6 は機能の差はほぼない。ただ、将来的には機能上の差が出てくる。現状では、OpenTofuを導入するためのTerraformとの学習コストのギャップが少ない。
  • OpenTofu は大規模なコミュニティであるため、問題の解決も速くなる
  • 急なライセンス変更となるリスクが低い

...など。

コード面の違い

OpentTofu v1.6.0 と Terraform v1.6.6 でのコード上の違いは全然見れてませんが、ディレクトリ構造は変わってました。 また Terraform 等の文字を含むコメントやメッセージ出力も OpenTofu のものに変わっているようでした。

コマンド体系は terraform コマンドから tofu コマンドに変わった以外は違いはなさそうです。

OpenTofuセットアップ

公式ドキュメントに沿ってインストールします。

環境

環境は下記で、Debian 12 になります。

% uname -srvmpio
Linux 6.1.0-12-cloud-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.52-1 (2023-09-07) x86_64 unknown unknown GNU/Linux

Terraform は v1.6.6 になります。

% terraform -v
Terraform v1.6.6
on linux_amd64

インストール

ドキュメントにある通りインストールスクリプトが公開されているのでこれを取得します。

% curl --proto '=https' --tlsv1.2 -fsSL https://get.opentofu.org/install-opentofu.sh -o install-opentofu.sh

実行します。

% ./install-opentofu.sh --install-method deb

問題なくインストールされました。

% which tofu
/usr/bin/tofu

バージョンは 1.6.0 です。

% tofu -v
OpenTofu v1.6.0
on linux_amd64

help をみてみます。コマンド体系はほぼ Terraform と同じように思いました。

% tofu -help
Usage: tofu [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 OpenTofu 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 OpenTofu modules
  graph         Generate a Graphviz graph of the steps in an operation
  import        Associate existing infrastructure with a OpenTofu 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          Show the current state or a saved plan
  state         Advanced state management
  taint         Mark a resource instance as not fully functional
  test          Execute integration tests for OpenTofu modules
  untaint       Remove the 'tainted' state from a resource instance
  version       Show the current OpenTofu 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 からの移行

Terraform からの移行についてもドキュメントが公開されていました。

上述の通り、Terraform v1.6.6 を使っています。

tofu コマンドを実行してみたところ、以下のようなエラーになりました。 ※手元の環境ではスイッチロールを実行しているため sandbox という名称の AWS_PROFILE を設定しています。

% AWS_PROFILE=sandbox tofu plan
╷
│ Error: Inconsistent dependency lock file
│
│ The following dependency selections recorded in the lock file are inconsistent with the current configuration:
│   - provider registry.opentofu.org/hashicorp/aws: required by this configuration but no version is selected
│
│ To update the locked dependency selections to match a changed configuration, run:
│   tofu init -upgrade
╵

出力にある通り、 tofu init -upgrade を実行してみます。

% AWS_PROFILE=sandbox tofu init -upgrade

Initializing the backend...
Upgrading modules...
- hoge-module in ../../modules/hoge

Initializing provider plugins...
- terraform.io/builtin/terraform is built in to OpenTofu
- Finding hashicorp/aws versions matching "~> 5.0"...
- Installing hashicorp/aws v5.32.1...
- Installed hashicorp/aws v5.32.1 (signed, key ID 0C0AF313E5FD9F80)

Providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://opentofu.org/docs/cli/plugins/signing/

OpenTofu has made some changes to the provider dependency selections recorded
in the .terraform.lock.hcl file. Review those changes and commit them to your
version control system if they represent changes you intended to make.

OpenTofu has been successfully initialized!

You may now begin working with OpenTofu. Try running "tofu plan" to see
any changes that are required for your infrastructure. All OpenTofu commands
should now work.

If you ever set or change modules or backend configuration for OpenTofu,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

正常終了したようです。

改めて tofu plan を実行してみます。

% AWS_PROFILE=sandbox tofu plan
  :
  :

No changes. Your infrastructure matches the configuration.

OpenTofu has compared your real infrastructure against your configuration and found no differences, so no changes are needed.

正常終了しました。 変更箇所はないのですが、tofu apply を実行してみます。

% AWS_PROFILE=sandbox tofu apply
  :
  :

No changes. Your infrastructure matches the configuration.

OpenTofu has compared your real infrastructure against your configuration and found no differences, so no changes are needed.

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

正常終了しました。

とりあえず大丈夫そうです。

まとめ

OpenTofu 1.6 が GA になったのでインストールして少し使ってみました。 現時点では、Terraform をユーザーとして(terraformを使ってシステムを構築するような用途で)利用している場合はライセンスによる影響はないと思いますので、プロダクション用途で使っているTerraformを置き換える必要はないのではと思いました。 とりあえず個人利用でしばらく触ってみようかと思います。

参考