Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Created April 12, 2023 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/af8307f07c4c63f77a67a2d813ff7499 to your computer and use it in GitHub Desktop.
Save guitarrapc/af8307f07c4c63f77a67a2d813ff7499 to your computer and use it in GitHub Desktop.
Terraform Graph generator and visualizer comparison

tl;dr;

terraform graph ではグラフ表示が厳しすぎる。 もっといい感じにリソースを構成図に落とし込んだり確認したい。

できれば、Pulumi の Graph View がよい。

候補1. terraform-graph-beautifier

pcasteran/terraform-graph-beautifier: Terraform graph beautifier https://github.com/pcasteran/terraform-graph-beautifier

terraform graph | docker run --rm -i \
    --name terraform-graph-beautifier \
    ghcr.io/pcasteran/terraform-graph-beautifier:latest-linux \
    --output-type=cyto-html \
    > config1.html

一番いい感じ。

候補2. rover

im2nguyen/rover: Interactive Terraform visualization. State and configuration explorer. https://github.com/im2nguyen/rover

Warning Terraform Cloud のリモートバックエンドで利用できない。

terraform plan -out=plan.out が必要で、エラーが出る。

# rover
2023/04/12 17:52:16 Starting Rover...
2023/04/12 17:52:16 Initializing Terraform...
2023/04/12 17:52:24 Generating plan...
2023/04/12 17:52:27 Unable to parse Plan: Unable to run Plan: exit status 1

Error: Saving a generated plan is currently not supported

Terraform Cloud does not support saving the generated execution plan locally
at this time.

なお、いわゆるスタンダードモジュール構成の場合、docker で実行時に次のように sh でコンテナ入って実行が必要。

# pwd は terraform のフォルダ直下にして modules も environments もとれるようにする
$ docker run --rm -it -p 9000:9000 -e TF_API_TOKEN=<YOUR_TOKEN> -v $(pwd):/src --entrypoint sh im2nguyen/rover

# docker
wget -O terraform_1.3.7.zip https://releases.hashicorp.com/terraform/1.3.7/terraform_1.3.7_linux_amd64.zip
unzip terraform_1.3.7.zip
rm terraform_1.3.7.zip
mv terraform /usr/local/bin/.
terraform --version
cat >~/.terraformrc <<EOL
credentials "app.terraform.io" {
  token = "${TF_API_TOKEN}"
}
EOL

cd environments/aws_dev
rover

候補3. terraform-visual

hieven/terraform-visual: Terraform Visual is an interactive way of visualizing your Terraform plan https://github.com/hieven/terraform-visual

Warning Terraform Cloud のリモートバックエンドで利用できない。

terraform plan -out=plan.out が必要で、エラーが出る。

$ terraform plan -out=plan.out
Unable to parse Plan: Unable to run Plan: exit status 1

Error: Saving a generated plan is currently not supported

Terraform Cloud does not support saving the generated execution plan locally
at this time.

候補4. inframap

cycloidio/inframap: Read your tfstate or HCL to generate a graph specific for each provider, showing only the resources that are most important/relevant. https://github.com/cycloidio/inframap

生成はされるが、いい感じのグラフにはならないので難しい。

state.tfstate を リモートバックエンドからダウンロードする。

TFC_TOKEN=<YOUR_TERRAFORM_CLOUD_API_TOKEN>
WORKSPACE_ID=<YOUR_TERRAFORM_CLOUD_WORKSPACE_ID>
HTTP_RESPONSE=$(curl \
 --header "Authorization: Bearer "$TFC_TOKEN"" \
 --header "Content-Type: application/vnd.api+json" \
 "https://app.terraform.io/api/v2/workspaces/$WORKSPACE_ID/current-state-version" | jq -r '.data | .attributes | ."hosted-state-download-url"') 

curl -o state.tfstate $HTTP_RESPONSE

で実行。

docker run --rm -v <TFSTATE_FOLDER>:/opt cycloid/inframap generate /opt/state.tfstate | dot -Tpng > graph.png

候補5. terraform graph

Command: graph | Terraform | HashiCorp Developer https://developer.hashicorp.com/terraform/cli/commands/graph

グラフが細かすぎて無理。

terraform graph | dot -Tsvg > graph.svg

参考

https://gkzz.dev/posts/alternative-terraform-graph/ が同じことしてた。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment