Terraform指南:安裝

使用之前首先必須將 Terraform 安裝在你的機器上。Terraform 是以二進制安裝包發佈的,它可以支持所有的平臺和架構。在本章節我們將不會涉及如何通過源代碼編譯 Terraform,但是對於那些需要通過源代碼編譯來構建可信任的二進制的用戶,我們在指南的其他章節會有闡述。

  • 安裝 Terraform

Terraform 安裝包是zip格式的壓縮文件,安裝之前需首先根據系統類型下載合適的安裝包。

Terraform 安裝包下載完成後進行解壓。Terraform 是一個名稱爲 terraform 的可執行的二進制文件,包中的其他任何文件都可以刪除也不影響 Terraform 正常工作。

最後,確保 terraform 二進制文件路徑添加到 PATH 環境變量中。請參見本指南中關於在Linux、Mac、Windows上如何設置 PATH。

  • 驗證安裝

Terraform 安裝完成之後,打開終端檢查 terraform 是否可用,執行 terraform 命令可以看到如下類似的幫助提示:

$ terraform
Usage: terraform [--version] [--help] <command> [args]

The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you're just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.

Common commands:
    apply              Builds or changes infrastructure
    console            Interactive console for Terraform interpolations
# ...

如果看到 terrform 無法找到的錯誤信息,那麼就是 PATH 環境變量沒有設置正確。請檢查確保 PATH 變量包含 Terrform 的安裝路徑。

  • 獲取幫助

Terraform CLI 內置了在線幫助,所有命令都可以使用 --help 參數來獲得幫助信息,例如: 

$ terraform -help
Usage: terraform [-version] [-help] <command> [args]

The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you are just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.

Common commands:
    apply              Builds or changes infrastructure
    console            Interactive console for Terraform interpolations
    destroy            Destroy Terraform-managed infrastructure
    env                Workspace management
    fmt                Rewrites config files to canonical format
    get                Download and install modules for the configuration
    graph              Create a visual graph of Terraform resources
    import             Import existing infrastructure into Terraform
    init               Initialize a Terraform working directory
    output             Read an output from a state file
    plan               Generate and show an execution plan
    providers          Prints a tree of the providers used in the configuration
    refresh            Update local state file against real resources
    show               Inspect Terraform state or plan
    taint              Manually mark a resource for recreation
    untaint            Manually unmark a resource as tainted
    validate           Validates the Terraform files
    version            Prints the Terraform version
    workspace          Workspace management

All other commands:
    0.12upgrade        Rewrites pre-0.12 module source code for v0.12
    debug              Debug output management (experimental)
    force-unlock       Manually unlock the terraform state
    push               Obsolete command for Terraform Enterprise legacy (v1)
    state              Advanced state management

任意這些子命令都可以加上 --help 參數獲取更多的幫助信息,例如:

$ terraform --help plan

Usage: terraform plan [options] [DIR]

  Generates an execution plan for Terraform.

  This execution plan can be reviewed prior to running apply to get a
  sense for what Terraform will do. Optionally, the plan can be saved to
  a Terraform plan file, and apply can take this plan file to execute
  this plan exactly.

Options:

  -destroy            If set, a plan will be generated to destroy all resources
                      managed by the given configuration and state.

  -detailed-exitcode  Return detailed exit codes when the command exits. This
                      will change the meaning of exit codes to:
                      0 - Succeeded, diff is empty (no changes)
                      1 - Errored
                      2 - Succeeded, there is a diff

  -input=true         Ask for input for variables if not directly set.

  -lock=true          Lock the state file when locking is supported.

  -lock-timeout=0s    Duration to retry a state lock.

  -no-color           If specified, output will not contain any color.

  -out=path           Write a plan file to the given path. This can be used as
                      input to the "apply" command.

  -parallelism=n      Limit the number of concurrent operations. Defaults to 10.

  -refresh=true       Update state prior to checking for differences.

  -state=statefile    Path to a Terraform state file to use to look
                      up Terraform-managed resources. By default it will
                      use the state "terraform.tfstate" if it exists.

  -target=resource    Resource to target. Operation will be limited to this
                      resource and its dependencies. This flag can be used
                      multiple times.

  -var "foo=bar"      Set a variable in the Terraform configuration. This
                      flag can be set multiple times.

  -var-file=foo       Set variables in the Terraform configuration from
                      a file. If "terraform.tfvars" or any ".auto.tfvars"
                      files are present, they will be automatically loaded.
發佈了3 篇原創文章 · 獲贊 1 · 訪問量 5682
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章