Terraform 常用指令
terraform init 初始化環境
terraform init
- 依據組態檔,下載需要的 provider 放到 .terraform/ 位置裡面,並快取一份在 ~/.terraform.d/
terraform validate 檢查組態檔語法
terraform validate
- 檢查程式碼的語法是否合法且一致,不管輸入變數以及現存狀態
terraform plan 產生執行計畫
terraform plan
- 跟據組態檔與基礎架構的狀態來預計執行操作
- terraform plan -destroy 預覽 terraform destroy 接下來的執行操作
terraform apply 產生執行計畫,並詢問是否執行
terraform apply [options] [dir]
- 輸入 "yes" 就會開始執行,並能看到 terraform 印出處理過程的資訊
terraform destroy 刪除資源
terraform destroy
- 會刪除經由 terraform 控制的基礎架構,並詢問是否要執行
-target
參數指定某項資源,那麼不但會銷毀該資源,同時也會銷毀一切依賴於該資源的資源
terraform console 啟動互動式控制台
terraform console [options] [dir]
- 可以用來測試各種表達式語法以及內建函數,或是作為偵錯工具
terraform fmt 格式化程式碼的格式和規格
terraform fmt [options] [dir]
- 用來格式化 Terraform 程式碼檔案的格式和規格
terraform force-unlock 手動解除工作區狀態鎖定
terraform force-unlock LOCK_ID [DIR]
- 指令不會修改你的基礎設施,它只會 刪除目前工作區對應的狀態鎖定
terraform get 下載與更新模組
terraform get [options] [dir]
- 下載以及更新根模組中使用的模組
terraform graph 建立架構圖
terraform graph [options] [DIR]
- 用來產生程式碼描述的基礎架構或是執行計畫的視覺化圖形
- 它的輸出是 DOT 格式,可以使用 GraphViz 來產生圖片
terraform graph | dot -Tsvg > graph.svg
terraform import 將既有資源物件匯入 Terraform
terraform import [options] [ADDRESS ID]
- 用來將已經存在的資源物件匯入 Terraform
terraform output 提取狀態檔案中輸出值
terraform output [options] [NAME]
- 展示根模組內定義的所有輸出值
- 若指定
NAME
,只會輸出相關輸出值
terraform providers 展示 Provider 的資訊
terraform providers [config-path]
- 可以透過明確傳遞
config-path
參數來指定根模組路徑,預設為目前工作目錄 terraform providers mirror
下載目前程式碼所需的 Provider 並且將其複製到本機目錄下terraform providers schema
展示目前程式碼所使用的 Provider 的架構與參數
terraform show 記錄變更計畫、配置以及目前狀態
terraform show [options] [path]
- 從狀態檔或是變更排程檔展示輸出資訊
terraform state 進行複雜的狀態管理操作
terraform state \[options] [args]
list
指令可以列出狀態檔案中記錄的資源物件mv
在狀態檔中移動資源pull
從遠端 Backend 下載狀態 並輸出push
上傳本機狀態檔案到遠端 Backendreplace-provider
指令可以取代狀態檔案中資源物件所使用的 Provider 的來源rm
從狀態檔中刪除物件show
指令可以展示狀態檔案中單一資源的屬性
terraform taint 標記資源,下次執行 apply 重建
terrform taint
- 指令可以手動標記某個 Terraform 管理的資源有"污點",強迫在下次執行 apply 時刪除並重建之