[小ネタ] Terraformer で /.terraform.d/plugins/darwin_amd64: no such file or directory が出力されてハマった話

Terraformerでインポート実行時に /.terraform.d/plugins/darwin_amd64: no such file or directory というエラーにハマりました。READMEを読むのは大事ですね。
2021.01.21

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは!コンサル部のinomaso(@inomasosan)です。

既存環境からtfsttateファイルを取得したかったので、Terraformerを試したのですが/.terraform.d/plugins/darwin_amd64: no such file or directoryというエラーにハマってしまいました。

環境

  • macOS Catalina 10.15.7
  • Homebrew 2.7.3
  • Terraform 0.14.4
  • Terraformer 0.8.10

先に結論から

Terraformerが、Terraformのバージョン0.14.xをサポートしていませんでした。
READMEにもバッチリ記載されてました;;READMEチャントヨムノダイジ

Support terraform 0.13 (for terraform 0.11 use v0.7.9).

Terraformerインストール

HomebrewでTerraformerをインストールします。

% brew install terraformer

インストール後に、バージョン確認してみましょう。

% terraformer version
Terraformer v0.8.10

事象

AWS既存環境からVPCをインポートするために、terraformerコマンドを実行すると、以下のエラーが出力されてしまいます。

% terraformer import aws --regions=ap-northeast-1 --resources=vpc
2021/01/18 00:51:26 aws importing region ap-northeast-1
2021/01/18 00:51:26 open /Users/hoge.hoge/.terraform.d/plugins/darwin_amd64: no such file or directory

調査

1. terraform init漏れでは?

エラー内容をググってみると、terraform initによる初期化が必要とのことでした。
しかし下記の様に実行してみましたが、事象は解決しませんでした。

% echo 'provider "aws" {}' > init.tf

% terraform init

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Installing hashicorp/aws v3.24.1...
- Installed hashicorp/aws v3.24.1 (signed by HashiCorp)

Terraform 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 Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

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

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

% terraformer import aws --regions=ap-northeast-1 --resources=vpc
2021/01/18 00:53:45 aws importing region ap-northeast-1
2021/01/18 00:53:45 open /Users/hoge.hoge/.terraform.d/plugins/darwin_amd64: no such file or directory

2. バージョン依存を疑う

もしかしたらバージョン依存かもと思い、TerraformerのREADMEを確認したところ、Terraformのバージョン0.14.xがサポートしていないことがわかりました。

tfenvでバージョンを0.13.6に切り替え再実行したら、インポートが無事に成功しました。

% tfenv use 0.13.6
Switching default version to v0.13.6
Switching completed

% terraform init

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Installing hashicorp/aws v3.24.1...
- Installed hashicorp/aws v3.24.1 (signed by HashiCorp)

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, we recommend adding version constraints in a required_providers block
in your configuration, with the constraint strings suggested below.

* hashicorp/aws: version = "~> 3.24.1"

Terraform has been successfully initialized!

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

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

% terraformer import aws --regions=ap-northeast-1 --resources=vpc
2021/01/18 01:02:33 aws importing region ap-northeast-1
2021/01/18 01:02:40 aws importing... vpc
2021/01/18 01:02:42 Refreshing state... aws_vpc.tfer--vpc-xxxx-xxxxxxxx
2021/01/18 01:02:42 Refreshing state... aws_vpc.tfer--vpc-xxxx-xxxxxxxxxxxxxxxxx
2021/01/18 01:02:55 aws Connecting....
2021/01/18 01:02:55 aws save vpc
2021/01/18 01:02:55 aws save tfstate for vpc

まとめ

READMEを読むのは大事ですね。
エラーが発生すると、楽に解決する手段を求めがちになってしまうので、これからは一呼吸置こうと思います。

この記事が、どなたかのお役に立てば幸いです。それでは!