Echoshell
  • Selfservice book
  • MacBook Setup
  • Linux SystemAdmin
    • Linux useful commands
    • Analyze tools
      • Hardware overview
      • Vmstat
      • Top
      • Sar
      • ltrace
      • strace
      • Netstat/SS
      • lsof
      • iostat
      • iotop
    • Performance Tuning
      • CPU
      • Momory
      • Disk/Storage
      • Network
      • Process
  • SRE vs DevOps
    • Ansible
    • Git
    • Jenkins
    • HashiCorp-Terraform
      • Terraform VS. CloudFormation
      • Main elements
        • Provider
        • State Storage and Locking
        • Backends
      • Basic Elements
        • Install
        • Resource
        • Data
        • Output
        • Locals
      • Commands
        • terraform plan
        • terraform apply
        • terraform import
    • HashiCorp-Vault
    • HashiCorp-Consul
    • CI/CD
      • GoCD
  • Automation/Script Language
    • Shell&Bash
      • Useful links
    • Python
      • 基础
        • py2 vs py3
        • 字符编码
        • 输入和输出
      • 数据类型Data Types
        • 字符串String
        • 列表List
        • 元组Tuple
        • 字典Dict
        • 集合Set
      • 函数Function
        • 定义函数
        • 函数参数
      • 函数式编程
        • 匿名函数lamba
        • map/reduce/filter
        • 闭包
        • 装饰器
      • 类Class
        • 类和实例
        • 继承和多态
        • 类方法和静态方法
        • 定制类和魔法方法
        • 元类-陌生的 metaclass
      • 高级特性
        • 迭代器iterator
        • 生成器generator
        • 上下文管理
      • 文件和目录
        • 读写文本文件
        • os 模块
      • 进程、线程和协程
        • 进程
        • 线程
        • ThreadLocal
        • 协程
      • 异常处理Exception
      • 正则表达式Regular-Expressions
        • re 模块
      • 标准模块
        • datetime
        • argparse
      • 第三方模块Three Parts
        • Click
        • JIRA
        • Email
        • Jenkins
      • 系统相关模块System modules
        • Subprocess
        • shutil
  • Learning notes from organization
  • IT Tech Team Leader/Senior System administrator
    • Helpdesk
    • Windows Servers
      • Radius Server
      • NFS
      • Munki
      • 802.1X
    • Networks
  • Personal views and ideas from my careen path
    • Team management
    • Career path change from IT to devops
Powered by GitBook
On this page
  • 執行流程
  • 使用 -out 避免 apply 與 plan 的差異

Was this helpful?

  1. SRE vs DevOps
  2. HashiCorp-Terraform
  3. Commands

terraform plan

PreviousCommandsNextterraform apply

Last updated 5 years ago

Was this helpful?

用於執行前進行差異比對,透過 plan 來檢查更新是否符合需求,確保 terraform 按計畫進行更新。

terraform 會拿現有的 state 與目前定義的 resource 進行比對,

執行流程

$ terraform plan
  1. plan 會檢查可以 refresh 的資源,像是 data 就會每次都檢查。

Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

data.aws_ami.ubuntu: Refreshing state...
  1. 接下來 plan 會印出這次執行會有的 動作

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create
  - destroy
  -/+ destroy and the create replacment
  -> change to data resource
  • 新增 resource 顯示 +

  • 修改 resource 顯示 ~ (僅異動 resource 內參數)

  • 刪除 resource 顯示 -

  • 先刪除後新增 resource 顯示 -/+

  • 異動 data 顯示 ->

  1. 再來就是詳細資源異動清單

Terraform will perform the following actions:

  # aws_elb.web-elb will be created
  + resource "aws_elb" "web-elb" {
      + arn                         = (known after apply)
      + availability_zones          = [
          + "us-west-2a",
          + "us-west-2b",
          ...
  1. 最後是此次異動的總結

Plan: 8 to add, 0 to change, 0 to destroy.
------------------------------------------------------------------------

使用 -out 避免 apply 與 plan 的差異

$ terraform plan -out=sample-deployment
$ terraform apply sample-deployment

使用自動化執行 terraform 時 -out 可以匯出這次 plan 的 異動清單,讓 執行時按照這個 異動清單 執行更新,確保 plan 和 apply 執行時是相同的結果。

plan
apply