Skip to content

Terraform Provider をビルドする

Terraform の thousandeyes 用 Provider をローカルでビルドしてみたので手順をメモしておきます。

検証環境

対象 バージョン
Ubuntu 22.04.3LTS
Go 1.21.5

事前準備

Provider をビルドする為には golang が必要になります。 また、ビルドした Provider の動作確認には Terraform が必要です。 今回は両方とも asdf でインストールしました。 具体的な手順は下記などを参考にします。

ビルドする

GitHub の terraform-provider-thousandeyes からソースコードをダウンロードします。

1
git clone https://github.com/thousandeyes/terraform-provider-thousandeyes.git

make build を実行してビルドします。

1
2
cd terraform-provider-thousandeyes
make build

バイナリの配置と設定ファイルの用意

ビルドしたバイナリをコピーします。 現時点の ThousandEyes Provider は最新が 2.0.8 だった為、バージョン番号は 2.0.9 にしてみました (深い意味はありません…)。

1
2
mkdir -p ~/.terraform.d/plugins/localhost/thousandeyes/thousandeyes/2.0.9/linux_amd64/
cp terraform-provider-thousandeyes ~/.terraform.d/plugins/localhost/thousandeyes/thousandeyes/2.0.9/linux_amd64/

ビルドしたバイナリが参照されるよう、設定ファイルを作成します。 ~/.terraformrc を以下の内容で新規作成します。

1
2
3
4
5
provider_installation {
  filesystem_mirror {
    path    = "/root/.terraform.d/plugins"
  }
}

Terraform を実行する

以下のような .tf ファイルを用意します。

1
2
3
4
5
6
7
8
terraform {
  required_providers {
    thousandeyes = {
      source = "localhost/thousandeyes/thousandeyes"
      version = ">= 2.0.9"
    }
  }
}

これで terraform init を実行すると以下のようになります。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# terraform init

Initializing the backend...

Initializing provider plugins...
- Finding localhost/thousandeyes/thousandeyes versions matching ">= 2.0.9"...
- Installing localhost/thousandeyes/thousandeyes v2.0.9...
- Installed localhost/thousandeyes/thousandeyes v2.0.9 (unauthenticated)

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.

╷
│ Warning: Incomplete lock file information for providers
│
│ Due to your customized provider installation methods, Terraform was forced to calculate lock file
│ checksums locally for the following providers:
│   - localhost/thousandeyes/thousandeyes
│
│ The current .terraform.lock.hcl file only includes checksums for linux_amd64, so Terraform running on
│ another platform will fail to install these providers.
│
│ To calculate additional checksums for another platform, run:
│   terraform providers lock -platform=linux_amd64
│ (where linux_amd64 is the platform to generate)
╵

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.