1
1

More than 1 year has passed since last update.

ひさびさのterraformerでハマる

Posted at

OverView

terraformerでAWSリソース取得を実施したところ、少々ハマったので記事にします。
背景として、PCをMac M1に変えてから初めてのterraformerの実行となります。
また、terraformerのインストールは今回割愛します。

Environment

  • Mac M1
  • tfenvでterraform管理

エラーについて

エラー出力

$ terraformer import aws --resources=s3 --regions=ap-northeast-1 --profile=【自分のプロファイル】
2022/04/13 17:20:43 aws importing region ap-northeast-1
2022/04/13 17:20:43 open /[自分の環境]/.terraform.d/plugins/darwin_arm64: no such file or directory

エラーの内容

最終的にエラーは2点ありました。

  • terraformerは、terraform0.13.6での対応(1系はサポートしていない)
  • aws pluginを指定の箇所に設置

エラー解決方法

terraform 0.13.6のインストール

tfenvでインストール (これではできません)

tfenvでterraformバージョンを管理しているので以下を実行します。

$ tfenv install 0.13.6
Installing Terraform v0.13.6
Downloading release tarball from https://releases.hashicorp.com/terraform/0.13.6/terraform_0.13.6_darwin_arm64.zip
##O=#    #                                                                                                                                          
curl: (22) The requested URL returned error: 403

terraform 0.13.6を見てみると、terraform_0.13.6_darwin_arm64.zipがありません。

そこで手動でインストールします。

M1 MacでtfenvからTerraform1.0.2未満のダウンロードに失敗する問題を無理矢理解決する | public memo
こちらやクラメソさんの記事にもありますが、arm環境でもamdで動作するようです。

手動インストール

tfenvのversionsの場所を探索します。
私の場合は、/opt/homebrew/Cellar/tfenv/2.2.3/versionsにありました。
調査方法は、tfenv listで調べるとヒントになると思います。

$ tfenv list
* 1.1.7 (set by /opt/homebrew/Cellar/tfenv/2.2.3/version)
  1.1.6
  1.1.2
  1.0.10
  1.0.9
  1.0.7
~ $ ls /opt/homebrew/opt/tfenv/versions
1.0.10 1.0.7  1.0.9  1.1.2  1.1.6  1.1.7

wgetでzipをダウンロードして解凍します。

$ cd /opt/homebrew/Cellar/tfenv/2.2.3/versions
$ mkdir 0.13.6
$ wget https://releases.hashicorp.com/terraform/0.13.6/terraform_0.13.6_darwin_amd64.zip
--2022-04-14 09:35:34--  https://releases.hashicorp.com/terraform/0.13.6/terraform_0.13.6_darwin_amd64.zip
releases.hashicorp.com (releases.hashicorp.com) をDNSに問いあわせています... 151.101.109.183
releases.hashicorp.com (releases.hashicorp.com)|151.101.109.183|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 35671246 (34M) [application/zip]
`terraform_0.13.6_darwin_amd64.zip' に保存中

terraform_0.13.6_darwin_amd64.zip 100%[============================================================>]  34.02M  18.2MB/s 時間 1.9s

2022-04-14 09:35:36 (18.2 MB/s) - `terraform_0.13.6_darwin_amd64.zip' へ保存完了 [35671246/35671246]

$ ls
terraform_0.13.6_darwin_amd64.zip

$ unzip terraform_0.13.6_darwin_amd64.zip
Archive:  terraform_0.13.6_darwin_amd64.zip
  inflating: terraform
$ ls
terraform                         terraform_0.13.6_darwin_amd64.zip
$ rm terraform_0.13.6_darwin_amd64.zip
$ ls 
terraform
$ tfenv use 0.13.6                               ✘ 1
Switching default version to v0.13.6
Switching completed
$ tfenv list
  1.1.7
  1.1.6
  1.1.2
  1.0.10
  1.0.9
  1.0.7
* 0.13.6 (set by /opt/homebrew/Cellar/tfenv/2.2.3/version)

完了したので実行してみると、また同じエラーが出ます。。。

$ terraformer import aws --resources=s3 --regions=ap-northeast-1 --profile=【自分のプロファイル】
2022/04/14 09:45:53 aws importing region ap-northeast-1
2022/04/14 09:45:53 open /【自分の環境】/.terraform.d/plugins/darwin_arm64: no such file or directory

率直にReadmeをみてみると以下の説明がありました。

• Copy your Terraform provider's plugin(s) to folder ~/.terraform.d/plugins/{darwin,linux}_amd64/, as appropriate.

要はこのディレクトリに AWSのプラグインを入れろとのことです。

AWSのプラグインは、以下にあります。
https://releases.hashicorp.com/terraform-provider-aws/3.49.0/

今回は特に意識せずに4系ではない、3.49.0を使いました。
(4系はハマりそう、、、という直感)

~/.terraform.d/はすでに存在していたので以下のコマンドで実行しました。
エラー出力の内容に従いarm64をインストールします。

$ cd ~/.terraform.d/
$ ls
checkpoint_cache     checkpoint_signature
$ mkdir -p plugins/darwin_arm64
$ ls
checkpoint_cache     checkpoint_signature plugins
$ cd plugins/darwin_arm64/
$ wget https://releases.hashicorp.com/terraform-provider-aws/3.49.0/terraform-provider-aws_3.49.0_darwin_arm64.zip
--2022-04-14 12:27:39--  https://releases.hashicorp.com/terraform-provider-aws/3.49.0/terraform-provider-aws_3.49.0_darwin_arm64.zip
releases.hashicorp.com (releases.hashicorp.com) をDNSに問いあわせています... 151.101.65.183, 151.101.129.183, 151.101.193.183, ...
releases.hashicorp.com (releases.hashicorp.com)|151.101.65.183|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 44046217 (42M) [application/zip]
`terraform-provider-aws_3.49.0_darwin_arm64.zip' に保存中

terraform-provider-aws_3. 100%[==================================>]  42.00M  13.3MB/s 時間 3.1s

2022-04-14 12:27:42 (13.3 MB/s) - `terraform-provider-aws_3.49.0_darwin_arm64.zip' へ保存完了 [44046217/44046217]

$ unzip terraform-provider-aws_3.49.0_darwin_arm64.zip
Archive:  terraform-provider-aws_3.49.0_darwin_arm64.zip
  inflating: terraform-provider-aws_v3.49.0_x5
$ rm terraform-provider-aws_3.49.0_darwin_arm64.zip
$ ls
terraform-provider-aws_3.49.0_darwin_arm64.zip terraform-provider-aws_v3.49.0_x5

実行

エラーが出ている方はすでに実行されていると思いますが、init.tfの作成も記載しておきます。

$ echo 'provider "aws" {}' > init.tf
$ terraformer import aws --resources=s3 --regions=ap-northeast-1 --profile=【個人のプロファイル】
2022/04/14 12:28:50 aws importing region ap-northeast-1
2022/04/14 12:28:53 aws importing... 
・・・

できました。

まとめ

terraformerは、terraformバージョンが固定されているので気を付けたほうが良いです。
エラー内容ではよく分からないので、同じ事象に遭遇した方の参考になればと思います。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1