gokigenmaruのブログ

40から始めるクラウドエンジニア

OpenTofuでネットワークリソースを作成する

先日入れたOpenTofuでAWSのネットワークリソースを作成してみます。

OpenTofuでリソース作成

作成するリソース

作成するリソースは以下の通りとします。
ここで作成するリソースのコードは既存のTerraformのコードをそのまま使用します。

 - PublicSubnet(2つ)
 - PrivateSubnet(2つ)
 - DBSubnet(2つ)

  • InternetGateway
  • NatGateway(2つ)

 - EIP(2つ) 

  • Route Table

 - Public用
 - Private用(2つ)
 - DB用

  • SecurityGroup
  • VPC Endpoint

 - Dynamo
 - Kinesis
 - S3用

tofu initの実行

Terraformと同様に、初回実行前にinitを実行。

$ tofu init

Initializing the backend...

Initializing provider plugins...
- Finding hashicorp/aws versions matching "~> 5.0"...
- Installing hashicorp/aws v5.35.0...
- Installed hashicorp/aws v5.35.0 (signed, key ID 0Cxxxxxxxxxxxx)

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 created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that OpenTofu can guarantee to make the same selections by default when
you run "tofu init" in the future.

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.

Terraformで作成したコードそのままでinitは通りました。

tofu planの実行

ではplanを実行してみます。

$ tofu plan

OpenTofu used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

OpenTofu will perform the following actions:

~~~中略~~~

────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so OpenTofu can't guarantee to take exactly these actions if you run "tofu apply" now.

問題なく通りました。
特にコード変更なしで行けるんだなぁと感心。特にprovider周りはエラーが起きるかなと思っていたので、tofu用にコード変更なしで通るのは意外でした。

tofu applyの実行

ではapplyの実行です。
正直、planがすんなり通っているのでapplyは普通に通ると思いました。

$ tofu apply
~~~中略~~~
Do you want to perform these actions?
  OpenTofu will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: 
~~~中略~~~

あっけなくネットワークのリソースが作成できました。

.terraformディレクトリと.terraform.lock.hcl、terraform.tfstateファイル

tofu initを実行するとTerraformのproviderのプラグインなどがダウンロードされてくる.terraformディレクトリとprovider情報とかが記録される.terraform.lock.hclファイルが作成されます。
中を見てみると、.terraformディレクトリにはopentofu.orgから落としてきたproviderが入っています。
terraform initした際に作成されるディレクトリ構成とほぼ一緒でした。

$ ls .terraform/providers/registry.opentofu.org/hashicorp/aws/5.35.0/linux_amd64/
CHANGELOG.md  LICENSE  README.md  terraform-provider-aws

.terraform.lock.hclファイルは以下の内容。terraformでinitした際に作成される.terraform.lock.hclファイルと内容はほぼ一緒。異なるのはterraformがtofuになっているのと、providerのレジストリがopentofuのURLになっている。

# This file is maintained automatically by "tofu init".
# Manual edits may be lost in future updates.

provider "registry.opentofu.org/hashicorp/aws" {
  version     = "5.35.0"
  constraints = "~> 5.0"
  hashes = [
~~~中略~~~
  ]
}

tofu apply時に作成されるterraform.tfstateファイルですが、中をざっと見たところterraform applyした際に作成されるterraform.tfstateファイルとほぼ同じ構成でした。
これなら今までTerraformで構成管理していたものをOpenTofuに変更するのは簡単に出来そうな気がします。

OpenTofuを動かしてみて

これは思っていたよりも楽にOpenTofuが使えますね。正直、TerraformからOpenTofuに移行するとなった場合にTerraformで作成したcodeに何かしらの変更が入ると思っていましたが、
特にそんなことはなく何も修正しなくても新規でリソースが作成できました。
動かしてみてちょっと思ったのは、tofu initなどのコマンドを実行した直後にちょっと待ちが発生するなぁくらいです。
そのほかは特に違和感などもありませんでした。

今回は新規リソースの作成でしたが、既存をterraformで作ってそれをtofuでリソース追加・削除するみたいなのも確認してみようと思います。