LoginSignup
8
2

More than 1 year has passed since last update.

【Terraformer】panic: interface conversion: interface {} is nil, not stringの回避策

Last updated at Posted at 2021-11-30

簡単に解説Terraformerとは

terraformerとは、既存リソースの情報をTerraformのコードにしてくれる優れものです!
既存リソースをterraform管理にしたいとなれば、terraform importが思いつきますが、各種リソースIDの取得であったり、resourceブロック内でrequired指定されている設定値の取得など少し辛い部分もあります。。

また、既存インフラを0からTerraform管理にするとなれば、多くのリソースをimportする必要があります。そんな時に使えるのが、terraformerです。

今回使用した環境

  • Amazon Linuxであること(他のOSは検証できていません。)
  • terraformer実行用のEC2インスタンスは適切な権限を持っていること

インストール方法

この記事を見ている方は、エラーが起きた時に見ていることだと思うので詳しいことは割愛します。

export PROVIDER=aws
curl -LO https://github.com/GoogleCloudPlatform/terraformer/releases/download/$(curl -s https://api.github.com/repos/GoogleCloudPlatform/terraformer/releases/latest | grep tag_name | cut -d '"' -f 4)/terraformer-${PROVIDER}-linux-amd64
chmod +x terraformer-${PROVIDER}-linux-amd64
sudo mv terraformer-${PROVIDER}-linux-amd64 /usr/local/bin/terraformer

本題

zennで解説が少しありましたが、大きな規模(どれくらいかはわかりません)のインフラを一気にインポートすると以下のようなエラーが起きます。

$ terraformer import aws --resources "*" --regions "ap-northeast-1"

panic: interface conversion: interface {} is nil, not string

goroutine 1 [running]:
# githubのurlが列挙される。

非プログラマーな私も当時、panicというエラーが出て、リソース壊しちゃった!?と冷や汗かきましたが、Go言語のエラーのようでした。

回避策(importを分割する)

私は、以下のシェルスクリプトもどきを作って無事importすることに成功しました!

terraformer.sh
#!/bin/bash
terraformer import aws list > ./resources.txt
while read resource
do
  terraformer import aws --resources $resource -regions "ap-northeast-1"
done < ./resources.txt
$ sh ./terraformer.sh

※-regionsの部分をベタ書きしちゃっていますが、ご愛嬌でお願いします。。。

終わりに

terrafomerで今回のようなエラー(panic)が出て、冷や汗かいている方の助けになれば幸いです!

8
2
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
8
2