devlog

Terraform Plugin Framework でカスタムプロバイダーを作る【メモ】

2023-07-03

Terraform Plugin Framework で Custom Provider を作ってみました。 チュートリアルを進める上でハマったところをメモ書きです。

※雑記です。不正確な箇所があるかもしれません。ご容赦ください

SDK と Framework

SDK と Framework がありますが Framework が推奨されてます。

Terraform Plugin SDK

Terraform Plugin Framework

私は Framework のチュートリアルを進めました。 quick start 的なリポジトリも公開されておりスムーズに実装できます。

hashicorp/local などシンプルな Provider もあるので、そのソースコードも参考になるかと思います。  


以降、Provider を作るにあたってハマったエラーを残します。

Invalid provider registry host というエラー

Provider を作成し終えたので使ってみようとした時でした。

まず依存に入れて、、

terraform { required_providers { sample = { source = "example.com/me/sample" } } }

Provider はローカルにしか無いので dev_overrides の設定をし、、(詳しくはチュートリアルをご覧ください)

provider_installation { dev_overrides { "example.com/me/sample" = "/Users/me/go/1.20.4/bin" } direct {} }

この状態で terraform init を実行するとエラーになりました。

$ terraform init │ Warning: Provider development overrides are in effect │ The following provider development overrides are set in the CLI configuration: │ - example.com/me/sample in /Users/me/go/1.20.4/bin │ Skip terraform init when using provider development overrides. It is not necessary and may │ error unexpectedly. │ Error: Invalid provider registry host │ The host "example.com" given in in provider source address "example.com/me/sample" does │ not offer a Terraform provider registry.

example.com は Terraform Provider Registry ではないと言われました。

... ええ、正しいです。まだ Publish してないので適当なURLを入れたのですが、エラーになってしまいました。

これをどう解決するか、ですが、、そもそも dev_overrides の時は terraform init を実行する必要がないようです。 Warningにあります。

Skip terraform init when using provider development overrides. It is not necessary and may error unexpectedly.

 
あるいは terraform init のときに -plugin-dir を指定する手もあります。 作成した Provider のバイナリを置き plugin-dir を指定すると、エラーメッセージが消えます。

置く場所ですが厳密に定義されてます。またCPUのアーキテクチャによって違うので plugin-dir で調べてみてください。

Failed to load plugin schemas というエラー

続いて terraform plan を実行するとエラーが発生しました。

$ terraform plan Error: Failed to load plugin schemas │ Error while loading schemas for plugin components: Failed to obtain provider schema: Could not │ load the schema for provider example.com/me/sample: failed to instantiate provider "example.com/me/sample" to obtain schema: could not find executable file starting with │ terraform-provider-sample..

terraform-provider-<name> という名前のファイルが見つからないと言われてます。

どうやら terraform plan で Provider のバイナリファイルを探すらしいんですが、そのファイル名は terraform-provider-<name> である必要があるようです。 hashicorp のブログにも書いてありました。

Writing Custom Terraform Providers より。

The output name (-o) is very important. Terraform searches for plugins in the format of:
terraform-<TYPE>-<NAME>
In the case above, the plugin is of type "provider" and of name "example".

go build -o でも良いですが、
Git のリポジトリ名を terraform-provider-<name> にするのが自然かと思います。

Links

  • 作成日
    2023-07-03
  • 更新日
    2023-07-06