Terraform チートシート
Terraform CLIコマンドのクイックリファレンス。init、plan、apply、state管理、workspace、import、output、フォーマットを網羅
57 件のコマンド
terraform init作業ディレクトリを初期化
terraform initterraform init -upgradeプロバイダーをアップグレードして初期化
terraform init -upgradeterraform init -backend-configバックエンド設定を指定して初期化
terraform init -backend-config=backend.hclterraform init -migrate-stateState移行を伴う初期化
terraform init -migrate-stateterraform init -reconfigureバックエンドを再設定して初期化
terraform init -reconfigureterraform providers使用中のプロバイダーを表示
terraform providersterraform providers lockプロバイダーのロックファイルを更新
terraform providers lock -platform=linux_amd64terraform versionTerraformのバージョンを表示
terraform versionterraform plan実行計画を表示
terraform planterraform plan -out実行計画をファイルに保存
terraform plan -out=tfplanterraform plan -var変数を指定して実行計画を表示
terraform plan -var='region=us-east-1'terraform plan -var-file変数ファイルを指定して計画を表示
terraform plan -var-file=prod.tfvarsterraform plan -target特定リソースのみの計画を表示
terraform plan -target=aws_instance.webterraform plan -destroy削除計画を表示
terraform plan -destroyterraform apply変更を適用
terraform applyterraform apply -auto-approve確認なしで変更を適用
terraform apply -auto-approveterraform apply tfplan保存した計画を適用
terraform apply tfplanterraform destroy管理下の全リソースを削除
terraform destroyterraform destroy -target特定リソースのみを削除
terraform destroy -target=aws_instance.webterraform refreshStateを実際のリソースと同期
terraform refreshterraform state listState内のリソース一覧を表示
terraform state listterraform state showState内のリソース詳細を表示
terraform state show aws_instance.webterraform state mvState内のリソースを移動・リネーム
terraform state mv aws_instance.old aws_instance.newterraform state rmState からリソースを除外
terraform state rm aws_instance.webterraform state pullリモートStateを取得して表示
terraform state pullterraform state pushローカルStateをリモートにプッシュ
terraform state push terraform.tfstateterraform state replace-providerState内のプロバイダーを置換
terraform state replace-provider hashicorp/aws registry.example.com/awsterraform force-unlockStateロックを強制解除
terraform force-unlock LOCK_IDterraform workspace listワークスペース一覧を表示
terraform workspace listterraform workspace new新しいワークスペースを作成
terraform workspace new stagingterraform workspace selectワークスペースを切り替え
terraform workspace select productionterraform workspace show現在のワークスペースを表示
terraform workspace showterraform workspace deleteワークスペースを削除
terraform workspace delete stagingterraform import既存リソースをStateにインポート
terraform import aws_instance.web i-1234567890terraform import (module)モジュール内のリソースをインポート
terraform import module.vpc.aws_vpc.main vpc-abc123terraform import (for_each)for_eachリソースをインポート
terraform import 'aws_iam_user.users["admin"]' adminterraform import (count)countリソースをインポート
terraform import 'aws_instance.servers[0]' i-1234567890terraform plan -generate-config-outインポート時に設定ファイルを自動生成
terraform plan -generate-config-out=generated.tfterraform output全出力値を表示
terraform outputterraform output (name)特定の出力値を表示
terraform output instance_ipterraform output -json出力値をJSON形式で表示
terraform output -jsonterraform output -raw出力値を生のテキストで表示
terraform output -raw instance_ipterraform console対話式コンソールを起動
terraform consoleterraform graphリソースの依存関係グラフを生成
terraform graph | dot -Tpng > graph.pngterraform showStateまたは計画を人間が読める形式で表示
terraform showterraform show -jsonStateをJSON形式で表示
terraform show -jsonterraform getモジュールをダウンロード
terraform getterraform get -updateモジュールを最新に更新
terraform get -updateterraform init (module source)Gitリポジトリからモジュールを取得
terraform initterraform testTerraformテストを実行
terraform testterraform providers mirrorプロバイダーをローカルミラーに保存
terraform providers mirror /path/to/mirrorterraform fmt設定ファイルをフォーマット
terraform fmtterraform fmt -checkフォーマットの差分を確認(変更なし)
terraform fmt -checkterraform fmt -recursiveサブディレクトリも含めてフォーマット
terraform fmt -recursiveterraform validate設定ファイルの構文を検証
terraform validateterraform fmt -diffフォーマット差分を表示
terraform fmt -diffterraform loginTerraform Cloudにログイン
terraform login