天天看點

騰訊雲Terraform應用指南(四)摘要一、Commands Introduction Episode 3二、寫在最後

摘要

本文将銜接《騰訊雲Terraform應用指南(三)》,将繼續向大家介紹Terraform CLI中最後幾個常用指令,助力騰訊雲Terraform應用。

一、Commands Introduction Episode 3

回顧之前的指令使用指南請點選這裡

1、refresh

terraform refresh

通過狀态檔案協調目前狀态與實際的基礎設施狀态, 用于檢測不一緻的内容,并更新狀态檔案。

标準文法:

terraform refresh [options] [dir]

  • options

    用來填寫

    refresh

    的flags
  • dir

    用來指定要重新整理的目錄,預設為目前目錄

預設情況下

refresh

不需要任何标志,并在目前目錄中查找要重新整理的配置和狀态檔案

使用

terraform refresh

指令,重新整理已有資源狀态

// Refresh the resource
    $ terraform refresh           

複制

騰訊雲Terraform應用指南(四)摘要一、Commands Introduction Episode 3二、寫在最後

重新整理已有資源

options

  • -backup=path

    - 設定備份檔案的路徑,取值

    -

    時,不備份
  • -state=path

    - 設定狀态檔案的路徑

有關

refresh

指令的更多資訊,請點選這裡

2、show

terraform show

用于檢視已經部署的資源。

标準文法:

terraform show [options] [path]

  • options

    用來填寫

    show

    的flags
  • path

    用來指定要檢視的目錄,預設為目前目錄

檢視目前目錄下部署的資源資訊

// Show the resource
    $ terraform show           

複制

騰訊雲Terraform應用指南(四)摘要一、Commands Introduction Episode 3二、寫在最後

檢視目前目錄下部署的資源資訊

options

  • -json

    - 顯示狀态的

    JSON

    表示,有關

    JSON

    的輸出格式請點選這裡

3、taint

terraform taint

用于對資源進行标記,強制被标記的資源在下一次應用中被銷毀并重新建立。

标準文法:

terraform taint [options] address

  • options

    用來填寫

    taint

    的flags
  • address

    用來指定被标記資源的位址

對已建立的伺服器進行标記,使其在下一次應用中銷毀并重新建立。注意:這裡的

address

填寫的是資源的引用格式,如果資源中有

count

參數,需要加上索引數

// Taint the resource
    $ terraform taint tencentcloud_instance.cvm[0]           

複制

騰訊雲Terraform應用指南(四)摘要一、Commands Introduction Episode 3二、寫在最後

标記伺服器資源

再次執行

terraform apply

時,會提示被标記的資源銷毀後,重新建立

騰訊雲Terraform應用指南(四)摘要一、Commands Introduction Episode 3二、寫在最後

提示重新建立被标記資源

options

  • -allow-missing

    - 允許标記丢失的資源

标記不存在的資源

// Taint the missing resource
    $ terraform taint -allow-missing tencentcloud_instance.cvm[1]           

複制

使用

-allow-missing

的前後對比

騰訊雲Terraform應用指南(四)摘要一、Commands Introduction Episode 3二、寫在最後

标記不存在資源時不同的提示資訊

  • -backup=path

    - 設定備份檔案的路徑,取值

    -

    時,不備份
  • -state=path

    - 設定狀态檔案的路徑

有關

taint

指令的更多資訊,請點選這裡

4、validate

terraform validate

用于驗證

.tf

檔案的文法。

标準文法:

terraform validate [options] [dir]

  • options

    用來填寫

    validate

    的flags
  • dir

    用來指定要驗證的目錄,預設為目前目錄

預設情況下,

validate

不需要任何标志,并在目前目錄中查找配置

// Validate the resource
    $ terraform validate           

複制

騰訊雲Terraform應用指南(四)摘要一、Commands Introduction Episode 3二、寫在最後

驗證目錄下的.tf檔案

options

  • -check-variables=true

    - 檢查是否制定了所有必需的變量,預設是

    true

有關

validate

指令的更多資訊,請點選這裡

5、untaint

terraform untaint

取消對已有資源的标記。

标準文法:

terraform untaint [options] name

  • options

    用來填寫

    untaint

    的flags
  • name

    用來指定資源,同

    taint

    address

    參數

options

  • -backup=path

    - 設定備份檔案的路徑,取值

    -

    時,不備份
  • -state=path

    - 設定狀态檔案的路徑

有關

untaint

指令的更多資訊,請點選這裡

6、workspace

terraform workspace

用于管理工作區,包含一些子指令。

标準文法:

terraform workspace <subcommand> [options] [args]

  • subcommand

    用來填寫

    workspace

    的子指令
  • options

    用來填寫

    workspace

    的flags
  • args

    用來填寫參數

terraform workspace new [NAME]

建立一個新的工作區,

NAME

用來指定新工作區的名字

建立命名為

test

的新工作區

//Create a new workspace
    $ terraform workspace new test           

複制

騰訊雲Terraform應用指南(四)摘要一、Commands Introduction Episode 3二、寫在最後

建立新工作區

terraform workspace list

列出現有的工作區,使用星号

*

标記訓示目前工作區

// List the workspace
    $ terraform workspace list           

複制

騰訊雲Terraform應用指南(四)摘要一、Commands Introduction Episode 3二、寫在最後

檢視所有工作區

terraform workspace select [NAME]

選擇一個進一步操作的工作區,

NAME

用來指定新工作區的名字

// Select the workspace
    $ terraform workspace select           

複制

将workspace從

test

變成

default

騰訊雲Terraform應用指南(四)摘要一、Commands Introduction Episode 3二、寫在最後

進入default工作區

terraform workspace delete [NAME]

删除已有的工作區

删除

test

工作區

// Delete the workspace
    $ terraform workspace delete test           

複制

騰訊雲Terraform應用指南(四)摘要一、Commands Introduction Episode 3二、寫在最後

删除test工作區

terraform workspace show

輸出目前工作區的名字

// Show the workspace
    $ terraform workspace show           

複制

騰訊雲Terraform應用指南(四)摘要一、Commands Introduction Episode 3二、寫在最後

列印目前工作區

二、寫在最後

至此,Terraform可用的指令已經全部介紹完,更多的應用情景還需要使用者在實踐中體驗,後續我們将介紹一些Terraform的高階應用指南,如子產品化等,幫助使用者掌握更進階的應用手段。請持續關注騰訊雲+社群,生态産品專欄《騰訊雲Terraform應用指南》系列,生态産品團隊将持續幫助使用者快速入門,熟練掌握Terraform應用技巧。

“Write, Plan, and create Infrastructure as Code" 讓每一個騰訊雲使用者高效、快捷的部署資源。