1
2

More than 1 year has passed since last update.

terraform importを使ってTerrafom管理化する

Last updated at Posted at 2023-06-04

はじめに

業務でGCPを使っているのですが、
すでに作成済みのリソースをTerraform管理化した際の流れをまとめます。

terraform importの使い方

terraform import [リソースタイプ.リソース名] [取り込み対象のID]

IDに関しては公式ドキュメントの対象リソースのページの一番下に書かれています。

手順

以下のような手順でインポートすることができます。
1. 取り込み用のTerraformファイルの作成 
2. terraform importコマンドでインポート
3. terraform planで差分確認
4. 差分があったらTerraformファイルを修正
5. 差分がなくなるまでterraform planをする
6. 差分がなくなったらtarraform applyする
7. stateファイルを確認して取り込まれたことを確認する

具体例として画面上から作成したmy_bucket_nameという名前のgcsのバケットを
Terrafom管理化する方法を以下にまとめます。

取り込み用のTerraformファイルの作成 

gcs.tf
resource "google_storage_bucket" "my_bucket" {
  name          = "my_bucket_name"
  location      = "asia-northeast1"
  force_destroy = true
}

terraform importコマンドでインポート

terraform import google_storage_bucket.my_bucket my_bucket_name

参照:

terraform planで差分確認

terraform plan

terraform applyで反映

terraform apply

sate確認

stateファイルの中身を確認して対象のリソースがあるかを確認します。
また、terraform state listを使うとTerrafom管理されているリソースの一覧を取得することができます。

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