9
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

tf-import-genでplanからterraformのimport blockを生成する

Last updated at Posted at 2025-06-03

tf-import-genを利用して、terraformをimportする際に planから
import blockの生成を行う方法を記載します。

tf-import-genとは

terraform showの結果(json)をterraform の import blockで出力してくれるコマンドラインツールです。

# 公式リポジトリより引用
$ terraform show -json | tf-import-gen

import {
  to = module.example.aws_glue_catalog_database.example_db
  id = "123456789012:example_db"
}
...

planの結果をimport blockに出力したい

planので同様のことができれば、未インポートのresourceに関しても、
import blockを書かずとも自動生成可能になります。(新規のリソース扱いになるので、resourceのIDは別途書き換えが必要です)

planの結果をそのままjsonに出力するだけでは構造が異なるのでエラーとなりますが、
json内の下記を変更することで、tf-import-genでも利用可能です。

  • .planned_values のキー名を .values に変更
...
  },
  "planned_values": {
...    

...
  },
  "values": {
...    

実行イメージ

# planをjson出力
terraform show -json > plan.json

# 前述のようにjsonを書き換え
.....

# tf-import-genで、import blockを生成
cat plan.json | tf-import-gen

gcloud beta resource-config bulk-exportなどで生成したリソースをimportしたい際に使えるかも?

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?