0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【作業メモ】GCPリソースをTerraformコードとしてエクスポートする

0
Posted at

Cloud ShellからリソースをTerraformコードにエクスポートできる。コマンドは2023/7/25時点ではbeta版。ベータ版のコマンドはgcloud beta (コマンド)になるのね

とりあえずプロジェクト配下のリソースを全てエクスポートする
(プロジェクトにはハンズオンで試しに作ったリソースがちょろっと残っている)

1. Config ConnectorのCLIをインストール

CloudShellで以下を実行

sudo apt-get install google-cloud-sdk-config-connector

2. Cloud Asset API を有効にする

GoogleCloudリソースの資産管理をするAPIなのかな

gcloud services enable cloudasset.googleapis.com

3. エクスポート

このコマンドだとproject指定しているのにprojectがemptyだよって言われた

gcloud beta resource-config bulk-export --path=<エクスポート先ディレクトリ> --project=<projectID> --resource-format=terraform

のでgcloud config set project <プロジェクトID>でデフォルトプロジェクトを設定して以下コマンドを実行したらエクスポートできた
gcloud beta resource-config bulk-export  --path=<エクスポート先ディレクトリ>  --resource-format=terraform

エクスポート先のフォルダ内はこんな感じ。それぞれのフォルダの中に.tfファイルが入っている
image.png

IAMServceAccountの情報なんかもエクスポートされてた

4. 感想

実行するたびにプロジェクト配下のリソース全て.tfにするのも時間かかるので、
リソースを指定してエクスポートする方法とか、エクスポートがサポートされていないリソースを今後整理したい

5. 参考サイト

エクスポート方法の詳細

ベータコマンドについて

デフォルトのプロジェクト設定について

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?