github.com/eliastor/durgaform@v0.0.0-20220816172711-d0ab2d17673e/website/docs/cli/commands/plan.mdx (about)

     1  ---
     2  page_title: 'Command: plan'
     3  description: >-
     4    The terraform plan command creates an execution plan with a preview of the
     5    changes that Terraform will make to your infrastructure.
     6  ---
     7  
     8  # Command: plan
     9  
    10  The `terraform plan` command creates an execution plan, which lets you preview
    11  the changes that Terraform plans to make to your infrastructure. By default,
    12  when Terraform creates a plan it:
    13  
    14  * Reads the current state of any already-existing remote objects to make sure
    15    that the Terraform state is up-to-date.
    16  * Compares the current configuration to the prior state and noting any
    17    differences.
    18  * Proposes a set of change actions that should, if applied, make the remote
    19    objects match the configuration.
    20  
    21  > **Hands-on:** Try the [Terraform: Get Started](https://learn.hashicorp.com/collections/terraform/aws-get-started?utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS) collection on HashiCorp Learn. For more in-depth details on the `plan` command, check out the [Create a Terraform Plan tutorial](https://learn.hashicorp.com/tutorials/terraform/plan?utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS).
    22  
    23  The plan command alone will not actually carry out the proposed changes, and
    24  so you can use this command to check whether the proposed changes match what
    25  you expected before you apply the changes or share your changes with your
    26  team for broader review.
    27  
    28  If Terraform detects that no changes are needed to resource instances or to
    29  root module output values, `terraform plan` will report that no actions need
    30  to be taken.
    31  
    32  If you are using Terraform directly in an interactive terminal and you expect
    33  to apply the changes Terraform proposes, you can alternatively run
    34  [`terraform apply`](/cli/commands/apply) directly. By default, the "apply" command
    35  automatically generates a new plan and prompts for you to approve it.
    36  
    37  You can use the optional `-out=FILE` option to save the generated plan to a
    38  file on disk, which you can later execute by passing the file to
    39  [`terraform apply`](/cli/commands/apply) as an extra argument. This two-step workflow
    40  is primarily intended for when
    41  [running Terraform in automation](https://learn.hashicorp.com/tutorials/terraform/automate-terraform?in=terraform/automation&utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS).
    42  
    43  If you run `terraform plan` without the `-out=FILE` option then it will create
    44  a _speculative plan_, which is a description of the effect of the plan but
    45  without any intent to actually apply it.
    46  
    47  In teams that use a version control and code review workflow for making changes
    48  to real infrastructure, developers can use speculative plans to verify the
    49  effect of their changes before submitting them for code review. However, it's
    50  important to consider that other changes made to the target system in the
    51  meantime might cause the final effect of a configuration change to be different
    52  than what an earlier speculative plan indicated, so you should always re-check
    53  the final non-speculative plan before applying to make sure that it still
    54  matches your intent.
    55  
    56  ## Usage
    57  
    58  Usage: `terraform plan [options]`
    59  
    60  The `plan` subcommand looks in the current working directory for the root module
    61  configuration.
    62  
    63  Because the plan command is one of the main commands of Terraform, it has
    64  a variety of different options, described in the following sections. However,
    65  most of the time you should not need to set any of these options, because
    66  a Terraform configuration should typically be designed to work with no special
    67  additional options for routine work.
    68  
    69  The remaining sections on this page describe the various options:
    70  
    71  * **[Planning Modes](#planning-modes)**: There are some special alternative
    72    planning modes that you can use for some special situations where your goal
    73    is not just to change the remote system to match your configuration.
    74  * **[Planning Options](#planning-options)**: Alongside the special planning
    75    modes, there are also some options you can set in order to customize the
    76    planning process for unusual needs.
    77    * **[Resource Targeting](#resource-targeting)** is one particular
    78      special planning option that has some important caveats associated
    79      with it.
    80  * **[Other Options](#other-options)**: These change the behavior of the planning
    81    command itself, rather than customizing the content of the generated plan.
    82  
    83  ## Planning Modes
    84  
    85  The previous section describes Terraform's default planning behavior, which
    86  changes the remote system to match the changes you make to
    87  your configuration. Terraform has two alternative planning modes, each of which creates a plan with a different intended outcome. These options are available for  both `terraform plan` and [`terraform apply`](/cli/commands/apply).
    88  
    89  * **Destroy mode:** creates a plan whose goal is to destroy all remote objects
    90    that currently exist, leaving an empty Terraform state. It is the same as running [`terraform destroy`](/cli/commands/destroy). Destroy mode can be useful for situations like transient development environments, where the managed objects cease to be useful once the development task is complete.
    91  
    92    Activate destroy mode using the `-destroy` command line option.
    93  
    94  * **Refresh-only mode:** creates a plan whose goal is only to update the
    95    Terraform state and any root module output values to match changes made to
    96    remote objects outside of Terraform. This can be useful if you've
    97    intentionally changed one or more remote objects outside of the usual
    98    workflow (e.g. while responding to an incident) and you now need to reconcile
    99    Terraform's records with those changes.
   100  
   101    Activate refresh-only mode using the `-refresh-only` command line option.
   102  
   103  In situations where we need to discuss the default planning mode that Terraform
   104  uses when none of the alternative modes are selected, we refer to it as
   105  "Normal mode". Because these alternative modes are for specialized situations
   106  only, some other Terraform documentation only discusses the normal planning
   107  mode.
   108  
   109  The planning modes are all mutually-exclusive, so activating any non-default
   110  planning mode disables the "normal" planning mode, and you can't use more than
   111  one alternative mode at the same time.
   112  
   113  -> **Note:** In Terraform v0.15 and earlier, the `-destroy` option is
   114  supported only by the `terraform plan` command, and not by the
   115  `terraform apply` command. To create and apply a plan in destroy mode in
   116  earlier versions you must run [`terraform destroy`](/cli/commands/destroy).
   117  
   118  -> **Note:** The `-refresh-only` option is available only in Terraform v0.15.4
   119  and later.
   120  
   121  > **Hands-on:** Try the [Use Refresh-Only Mode to Sync Terraform State](https://learn.hashicorp.com/tutorials/terraform/refresh) tutorial on HashiCorp Learn.
   122  
   123  ## Planning Options
   124  
   125  In addition to alternate [planning modes](#planning-modes), there are several options that can modify planning behavior. These options are available for  both `terraform plan` and [`terraform apply`](/cli/commands/apply).
   126  
   127  - `-refresh=false` - Disables the default behavior of synchronizing the
   128    Terraform state with remote objects before checking for configuration changes. This can make the planning operation faster by reducing the number of remote API requests. However, setting `refresh=false` causes Terraform to ignore external changes, which could result in an incomplete or incorrect plan. You cannot use `refresh=false` in refresh-only planning mode because it would effectively disable the entirety of the planning operation.
   129  
   130  - `-replace=ADDRESS` - Instructs Terraform to plan to replace the
   131    resource instance with the given address. This is helpful when one or more remote objects have become degraded, and you can use replacement objects with the same configuratation to align with immutable infrastructure patterns. Terraform will use a "replace" action if the specified resource would normally cause an "update" action or no action at all. Include this option multiple times to replace several objects at once. You cannot use `-replace` with the `-destroy` option, and it is only available from Terraform v0.15.2 onwards. For earlier versions, use [`terraform taint`](/cli/commands/taint) to achieve a similar result.
   132  
   133  - `-target=ADDRESS` - Instructs Terraform to focus its planning efforts only
   134    on resource instances which match the given address and on any objects that
   135    those instances depend on.
   136  
   137    -> **Note:** Use `-target=ADDRESS` in exceptional circumstances only, such as recovering from mistakes or working around Terraform limitations. Refer to [Resource Targeting](#resource-targeting) for more details.
   138  
   139  - `-var 'NAME=VALUE'` - Sets a value for a single
   140    [input variable](/language/values/variables) declared in the
   141    root module of the configuration. Use this option multiple times to set
   142    more than one variable. Refer to
   143    [Input Variables on the Command Line](#input-variables-on-the-command-line) for more information.
   144  
   145  - `-var-file=FILENAME` - Sets values for potentially many
   146    [input variables](/language/values/variables) declared in the
   147    root module of the configuration, using definitions from a
   148    ["tfvars" file](/language/values/variables#variable-definitions-tfvars-files).
   149    Use this option multiple times to include values from more than one file.
   150  
   151  There are several other ways to set values for input variables in the root
   152  module, aside from the `-var` and `-var-file` options. Refer to
   153  [Assigning Values to Root Module Variables](/language/values/variables#assigning-values-to-root-module-variables) for more information.
   154  
   155  ### Input Variables on the Command Line
   156  
   157  You can use the `-var` command line option to specify values for
   158  [input variables](/language/values/variables) declared in your
   159  root module.
   160  
   161  However, to do so will require writing a command line that is parsable both
   162  by your chosen command line shell _and_ Terraform, which can be complicated
   163  for expressions involving lots of quotes and escape sequences. In most cases
   164  we recommend using the `-var-file` option instead, and write your actual values
   165  in a separate file so that Terraform can parse them directly, rather than
   166  interpreting the result of your shell's parsing.
   167  
   168  ~> **Warning:** Terraform will error if you include a space before or after the equals sign (e.g., `-var "length = 2"`).
   169  
   170  To use `-var` on a Unix-style shell on a system like Linux or macOS we
   171  recommend writing the option argument in single quotes `'` to ensure the
   172  shell will interpret the value literally:
   173  
   174  ```
   175  terraform plan -var 'name=value'
   176  ```
   177  
   178  If your intended value also includes a single quote then you'll still need to
   179  escape that for correct interpretation by your shell, which also requires
   180  temporarily ending the quoted sequence so that the backslash escape character
   181  will be significant:
   182  
   183  ```
   184  terraform plan -var 'name=va'\''lue'
   185  ```
   186  
   187  When using Terraform on Windows, we recommend using the Windows Command Prompt
   188  (`cmd.exe`). When you pass a variable value to Terraform from the Windows
   189  Command Prompt, use double quotes `"` around the argument:
   190  
   191  ```
   192  terraform plan -var "name=value"
   193  ```
   194  
   195  If your intended value includes literal double quotes then you'll need to
   196  escape those with a backslash:
   197  
   198  ```
   199  terraform plan -var "name=va\"lue"
   200  ```
   201  
   202  PowerShell on Windows cannot correctly pass literal quotes to external programs,
   203  so we do not recommend using Terraform with PowerShell when you are on Windows.
   204  Use Windows Command Prompt instead.
   205  
   206  The appropriate syntax for writing the variable value is different depending
   207  on the variable's [type constraint](/language/expressions/type-constraints).
   208  The primitive types `string`, `number`, and `bool` all expect a direct string
   209  value with no special punctuation except that required by your shell, as
   210  shown in the above examples. For all other type constraints, including list,
   211  map, and set types and the special `any` keyword, you must write a valid
   212  Terraform language expression representing the value, and write any necessary
   213  quoting or escape characters to ensure it will pass through your shell
   214  literally to Terraform. For example, for a `list(string)` type constraint:
   215  
   216  ```
   217  # Unix-style shell
   218  terraform plan -var 'name=["a", "b", "c"]'
   219  
   220  # Windows Command Prompt (do not use PowerShell on Windows)
   221  terraform plan -var "name=[\"a\", \"b\", \"c\"]"
   222  ```
   223  
   224  Similar constraints apply when setting input variables using environment
   225  variables. For more information on the various methods for setting root module
   226  input variables, see
   227  [Assigning Values to Root Module Variables](/language/values/variables#assigning-values-to-root-module-variables).
   228  
   229  ### Resource Targeting
   230  
   231  > **Hands-on:** Try the [Target resources](https://learn.hashicorp.com/tutorials/terraform/resource-targeting?in=terraform/state&utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS) tutorial on HashiCorp Learn.
   232  
   233  You can use the `-target` option to focus Terraform's attention on only a
   234  subset of resources.
   235  You can use [resource address syntax](/cli/state/resource-addressing)
   236  to specify the constraint. Terraform interprets the resource address as follows:
   237  
   238  * If the given address identifies one specific resource instance, Terraform
   239    will select that instance alone. For resources with either `count` or
   240    `for_each` set, a resource instance address must include the instance index
   241    part, like `aws_instance.example[0]`.
   242  
   243  * If the given address identifies a resource as a whole, Terraform will select
   244    all of the instances of that resource. For resources with either `count`
   245    or `for_each` set, this means selecting _all_ instance indexes currently
   246    associated with that resource. For single-instance resources (without
   247    either `count` or `for_each`), the resource address and the resource instance
   248    address are identical, so this possibility does not apply.
   249  
   250  * If the given address identifies an entire module instance, Terraform will
   251    select all instances of all resources that belong to that module instance
   252    and all of its child module instances.
   253  
   254  Once Terraform has selected one or more resource instances that you've directly
   255  targeted, it will also then extend the selection to include all other objects
   256  that those selections depend on either directly or indirectly.
   257  
   258  This targeting capability is provided for exceptional circumstances, such
   259  as recovering from mistakes or working around Terraform limitations. It
   260  is _not recommended_ to use `-target` for routine operations, since this can
   261  lead to undetected configuration drift and confusion about how the true state
   262  of resources relates to configuration.
   263  
   264  Instead of using `-target` as a means to operate on isolated portions of very
   265  large configurations, prefer instead to break large configurations into
   266  several smaller configurations that can each be independently applied.
   267  [Data sources](/language/data-sources) can be used to access
   268  information about resources created in other configurations, allowing
   269  a complex system architecture to be broken down into more manageable parts
   270  that can be updated independently.
   271  
   272  ## Other Options
   273  
   274  The `terraform plan` command also has some other options that are related to
   275  the input and output of the planning command, rather than customizing what
   276  sort of plan Terraform will create. These commands are not necessarily also
   277  available on `terraform apply`, unless otherwise stated in the documentation
   278  for that command.
   279  
   280  The available options are:
   281  
   282  * `-compact-warnings` - Shows any warning messages in a compact form which
   283    includes only the summary messages, unless the warnings are accompanied by
   284    at least one error and thus the warning text might be useful context for
   285    the errors.
   286  
   287  * `-detailed-exitcode` - Returns a detailed exit code when the command exits.
   288    When provided, this argument changes the exit codes and their meanings to
   289    provide more granular information about what the resulting plan contains:
   290    * 0 = Succeeded with empty diff (no changes)
   291    * 1 = Error
   292    * 2 = Succeeded with non-empty diff (changes present)
   293  
   294  * `-input=false` - Disables Terraform's default behavior of prompting for
   295    input for root module input variables that have not otherwise been assigned
   296    a value. This option is particularly useful when running Terraform in
   297    non-interactive automation systems.
   298  
   299  * `-json` - Enables the [machine readable JSON UI][machine-readable-ui] output.
   300    This implies `-input=false`, so the configuration must have no unassigned
   301    variable values to continue.
   302  
   303    [machine-readable-ui]: /internals/machine-readable-ui
   304  
   305  * `-lock=false` - Don't hold a state lock during the operation. This is
   306    dangerous if others might concurrently run commands against the same
   307    workspace.
   308  
   309  * `-lock-timeout=DURATION` - Unless locking is disabled with `-lock=false`,
   310    instructs Terraform to retry acquiring a lock for a period of time before
   311    returning an error. The duration syntax is a number followed by a time
   312    unit letter, such as "3s" for three seconds.
   313  
   314  * `-no-color` - Disables terminal formatting sequences in the output. Use this
   315    if you are running Terraform in a context where its output will be
   316    rendered by a system that cannot interpret terminal formatting.
   317  
   318  * `-out=FILENAME` - Writes the generated plan to the given filename in an
   319    opaque file format that you can later pass to `terraform apply` to execute
   320    the planned changes, and to some other Terraform commands that can work with
   321    saved plan files.
   322  
   323    Terraform will allow any filename for the plan file, but a typical
   324    convention is to name it `tfplan`. **Do not** name the file with a suffix
   325    that Terraform recognizes as another file format; if you use a `.tf` suffix
   326    then Terraform will try to interpret the file as a configuration source
   327    file, which will then cause syntax errors for subsequent commands.
   328  
   329    The generated file is not in any standard format intended for consumption
   330    by other software, but the file _does_ contain your full configuration,
   331    all of the values associated with planned changes, and all of the plan
   332    options including the input variables. If your plan includes any sort of
   333    sensitive data, even if obscured in Terraform's terminal output, it will
   334    be saved in cleartext in the plan file. You should therefore treat any
   335    saved plan files as potentially-sensitive artifacts.
   336  
   337  * `-parallelism=n` - Limit the number of concurrent operations as Terraform
   338    [walks the graph](/internals/graph#walking-the-graph). Defaults
   339    to 10.
   340  
   341  For configurations using
   342  [the `local` backend](/language/settings/backends/local) only,
   343  `terraform plan` accepts the legacy command line option
   344  [`-state`](/language/settings/backends/local#command-line-arguments).
   345  
   346  ### Passing a Different Configuration Directory
   347  
   348  Terraform v0.13 and earlier accepted an additional positional argument giving
   349  a directory path, in which case Terraform would use that directory as the root
   350  module instead of the current working directory.
   351  
   352  That usage was deprecated in Terraform v0.14 and removed in Terraform v0.15.
   353  If your workflow relies on overriding the root module directory, use
   354  [the `-chdir` global option](/cli/commands/#switching-working-directory-with-chdir)
   355  instead, which works across all commands and makes Terraform consistently look
   356  in the given directory for all files it would normally read or write in the
   357  current working directory.
   358  
   359  If your previous use of this legacy pattern was also relying on Terraform
   360  writing the `.terraform` subdirectory into the current working directory even
   361  though the root module directory was overridden, use
   362  [the `TF_DATA_DIR` environment variable](/cli/config/environment-variables#tf_data_dir)
   363  to direct Terraform to write the `.terraform` directory to a location other
   364  than the current working directory.