This new option is intended to address the previous inconsistencies where some older subcommands supported partially changing the target directory (where Terraform would use the new directory inconsistently) where newer commands did not support that override at all. Instead, now Terraform will accept a -chdir command at the start of the command line (before the subcommand) and will interpret it as a request to direct all actions that would normally be taken in the current working directory into the target directory instead. This is similar to options offered by some other similar tools, such as the -C option in "make". The new option is only accepted at the start of the command line (before the subcommand) as a way to reflect that it is a global command (not specific to a particular subcommand) and that it takes effect _before_ executing the subcommand. This also means it'll be forced to appear before any other command-specific arguments that take file paths, which hopefully communicates that those other arguments are interpreted relative to the overridden path. As a measure of pragmatism for existing uses, the path.cwd object in the Terraform language will continue to return the _original_ working directory (ignoring -chdir), in case that is important in some exceptional workflows. The path.root object gives the root module directory, which will always match the overriden working directory unless the user simultaneously uses one of the legacy directory override arguments, which is not a pattern we intend to support in the long run. As a first step down the deprecation path, this commit adjusts the documentation to de-emphasize the inconsistent old command line arguments, including specific guidance on what to use instead for the main three workflow commands, but all of those options remain supported in the same way as they were before. In a later commit we'll make those arguments produce a visible deprecation warning in Terraform's output, and then in an even later commit we'll remove them entirely so that -chdir is the single supported way to run Terraform from a directory other than the one containing the root module configuration.
4.9 KiB
layout, page_title, sidebar_current, description
| layout | page_title | sidebar_current | description |
|---|---|---|---|
| docs | Command: apply | docs-commands-apply | The `terraform apply` command is used to apply the changes required to reach the desired state of the configuration, or the pre-determined set of actions generated by a `terraform plan` execution plan. |
Command: apply
For a hands-on tutorial, try the Get Started track on HashiCorp Learn.
The terraform apply command is used to apply the changes required
to reach the desired state of the configuration, or the pre-determined
set of actions generated by a terraform plan execution plan.
Usage
Usage: terraform apply [options] [plan]
By default, apply scans the current directory for the configuration
and applies the changes appropriately. However, you can optionally give the
path to a saved plan file that was previously created with
terraform plan.
If you don't give a plan file on the command line, terraform apply will
create a new plan automatically and then prompt for approval to apply it. If the
created plan does not include any changes to resources or to root module
output values then terraform apply will exit immediately, without prompting.
The command-line flags are all optional. The list of available flags are:
-
-backup=path- Path to the backup file. Defaults to-state-outwith the ".backup" extension. Disabled by setting to "-". -
-compact-warnings- If Terraform produces any warnings that are not accompanied by errors, show them in a more compact form that includes only the summary messages. -
-lock=true- Lock the state file when locking is supported. -
-lock-timeout=0s- Duration to retry a state lock. -
-input=true- Ask for input for variables if not directly set. -
-auto-approve- Skip interactive approval of plan before applying. -
-no-color- Disables output with coloring. -
-parallelism=n- Limit the number of concurrent operation as Terraform walks the graph. Defaults to 10. -
-refresh=true- Update the state for each resource prior to planning and applying. This has no effect if a plan file is given directly to apply. -
-state=path- Path to the state file. Defaults to "terraform.tfstate". Ignored when remote state is used. This setting does not persist and other commands, such as init, may not be aware of the alternate statefile. To configure an alternate statefile path which is available to all terraform commands, use the local backend. -
-state-out=path- Path to write updated state file. By default, the-statepath will be used. Ignored when remote state is used. -
-target=resource- A Resource Address to target. For more information, see the targeting docs fromterraform plan. -
-var 'foo=bar'- Set a variable in the Terraform configuration. This flag can be set multiple times. Variable values are interpreted as HCL, so list and map values can be specified via this flag. -
-var-file=foo- Set variables in the Terraform configuration from a variable file. If aterraform.tfvarsor any.auto.tfvarsfiles are present in the current directory, they will be automatically loaded.terraform.tfvarsis loaded first and the.auto.tfvarsfiles after in alphabetical order. Any files specified by-var-fileoverride any values set automatically from files in the working directory. This flag can be used multiple times.
Passing a Different Configuration Directory
Terraform v0.13 and earlier also accepted a directory path in place of the
plan file argument to terraform apply, in which case Terraform would use
that directory as the root module instead of the current working directory.
That usage is still supported in Terraform v0.14, but is now deprecated and we
plan to remove it in Terraform v0.15. If your workflow relies on overriding
the root module directory, use
the -chdir global option
instead, which works across all commands and makes Terraform consistently look
in the given directory for all files it would normaly read or write in the
current working directory.
If your previous use of this legacy pattern was also relying on Terraform
writing the .terraform subdirectory into the current working directory even
though the root module directory was overridden, use
the TF_DATA_DIR environment variable
to direct Terraform to write the .terraform directory to a location other
than the current working directory.